cassioalmeidas / micro_ga

Genetic Algorithm in Ruby in < 30 lines

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Micro Microbial Genetic Algorithm (MMGA)

A Microbial Genetic Algorithm written in minimal number of Ruby lines

Usage

ga = MGA.new(:generations => 1000, :gene_length => 20, :fitness => Proc.new{|genome|
  #puts fitness evaluation logic here. should return numerical value.
  #ie; simple max-ones fitness (or largest sum with mutation_type => :decimal)
  puts(genome.inspect)
  genome.inject{|i,j| i+j}
})

ga.evolve

Once run the resultant population can be accessed with

ga.population

Args can be passed as a hash. The following shows valid keys for args and their default values if not given;

:popsize => 30,
:gene_length => 10,
:cross_over_rate => 0.7,
:mutation_rate => 0.1,
:mutation_type => :decimal, #can also be :binary.  decimal mutates with small +/- value, binary flips 0->1, 1->0
:generations => 400,
:fitness =>  Proc.new{|genome| genome.inject{|i,j| i+j} }

Microbial Design

Microbial GAs are a slight twist on a typical GAs. Instead of creating a new genome via recombination of two other genomes, the microbial idea is to insert and overwrite some genes of a weaker member with genes from a fitter one. The idea is based on viral life forms which insert sections of DNA into a host, thus altering the hosts DNA.

Evolution will work with any >0 cross over rate, but a cross over rate of >0.5 (50%) greatly increases the pace.

A Note on Mutation rates

Mutation rates in GAs can be in terms of genes or genomes. The mutation rate used here is a per genome value which means you get the same per gene mutation rate regardless of gene-length.

per_gene_rate = per_genome_rate/gene_length

With a per genome rate of 0.1 the probability of mutation for each gene in a 10bit genome is 0.01. If the genome was 100 bits probability of mutation for each gene would be 0.001. If a per gene mutation rate of 0.1 is used then 1 gene in a 10 bit genome will mutate, however the same per gene rate will cause 10 mutations in a 100 bit genome.

About

Genetic Algorithm in Ruby in < 30 lines


Languages

Language:Ruby 100.0%