lib/population.rb
# Evolve population with simple GA.
# @params fun [Method] evaluation function.
# @params selection [String] selection method.
# @params mutation_method [String] mutation method for Individual#mutation.

Evolve population with simple GA.
Evolve population with simple GA .
def simple_ga(fun, selection=nil, mutation_method=nil)
    new_units = []
    while (new_units.size < @units.size)
      case selection
      when "roulette"
        parent1 = roulette_selection(fun)
        parent2 = roulette_selection(fun)
      when "elite"
        parent1 = elite_selection(fun)
        parent2 = elite_selection(fun)
      when "tournament"
        parent1 = tournament_selection(fun)
        parent2 = tournament_selection(fun)
      when "rank" 
        parent1 = rank_selection(fun)
        parent2 = rank_selection(fun)
      else
        raise "selection method #{selection} is invalid!"
      end
      child1, child2 = parent1.crossover(parent2, @crossover)
      new_units << child1 << child2
    end
    new_units.each do |unit|
      if rand(100) < @mutationProbability * 100
        unit.mutation(@mutationProbability, mutation_method)
      end
    end
    @units = new_units
  end
def simple_ga ( fun , selection = nil , mutation_method = nil ) new_units = [ ] while ( new_units . size < @units . size ) case selection when "roulette" parent1 = roulette_selection ( fun ) parent2 = roulette_selection ( fun ) when "elite" parent1 = elite_selection ( fun ) parent2 = elite_selection ( fun ) when "tournament" parent1 = tournament_selection ( fun ) parent2 = tournament_selection ( fun ) when "rank" parent1 = rank_selection ( fun ) parent2 = rank_selection ( fun ) else raise "selection method #{selection} is invalid!" end child1 , child2 = parent1 . crossover ( parent2 , @crossover ) new_units << child1 << child2 end new_units . each do | unit | if rand ( 100 ) < @mutationProbability * 100 unit . mutation ( @mutationProbability , mutation_method ) end end @units = new_units end
Evolve population with simple GA.
Evolve population with simple GA .
# Crossover
 #if rand(100) > @crossoverProbability*100
 #@units.each{|unit| unit.get_older}
 #p child1.gene.uniq.size, child2.gene.uniq.size
 #@units << parent1 << parent2 << child1 << child2 
 #2.times do
 #  worst = drop_worst_unit(fun)
 #end
 # Mutation
 #@units.each do |unit|

fun
<not_specific>
selection
<not_specific>
mutation_method
<not_specific>
params
fun [Method] evaluation function.
fun [ Method ] evaluation function .
params
selection [String] selection method.
selection [ String ] selection method .
params
mutation_method [String] mutation method for Individual#mutation.
mutation_method [ String ] mutation method for Individual#mutation .