population.rb

Path: lib/charlie/population.rb
Last Update: Sat Apr 05 14:45:26 +0200 2008

Contains the Population class

Methods

GP  

Public Instance methods

Uses GP-style evolution (i.e. crossover OR mutation instead of crossover AND mutation) with a selection method.

  • Works by discarding and replacing the block given to next_generation in Population#evolve_block
  • Is not mandatory for GP, and can just as easily be used for GA or not used in GP.
  • Bit of a strange place to do this, but works nicely with the benchmarking

[Source]

     # File lib/charlie/population.rb, line 131
131: def GP(sel_module,crossover_probability=0.75)
132:  ng_name = sel_module.to_s.gsub(/[^A-Za-z0-9]/,'_')
133:  Module.new{
134:   include sel_module.dup
135:   alias_method ng_name, :next_generation
136:   define_method(:next_generation){|population|
137:     send(ng_name,population){|*parents|
138:       if rand < crossover_probability
139:         [*parents[0].class.cross(*parents)]
140:       else
141:         parents.map{|c| c.mutate }
142:       end
143:     }
144:   }
145:   self.name= "GP(#{sel_module.to_s},#{crossover_probability})"
146:  }
147: end

[Validate]