Class: Population
- Inherits:
-
Object
- Object
- Population
- Defined in:
- lib/charlie/population.rb
Overview
The population class represents an array of genotypes. Create an instance of this, and call one of the evolve functions to run the genetic algorithm.
Constant Summary collapse
- DEFAULT_MAX_GENS =
100
Instance Attribute Summary collapse
-
#genotype_class ⇒ Object
readonly
Returns the value of attribute genotype_class.
-
#population ⇒ Object
readonly
Returns the value of attribute population.
-
#size ⇒ Object
readonly
Returns the value of attribute size.
Instance Method Summary collapse
-
#evolve_block(max_generations = DEFAULT_MAX_GENS) {|@population, 0| ... } ⇒ Object
yields population and generation number to block each generation, for a maximum of max_generations.
-
#evolve_on_console(generations = DEFAULT_MAX_GENS) ⇒ Object
(also: #evolve)
Runs the genetic algorithm with some stats on the console.
-
#evolve_silent(generations = DEFAULT_MAX_GENS) ⇒ Object
Runs the genetic algorithm without any output.
-
#evolve_until_best(generations = DEFAULT_MAX_GENS, check_every = 10) ⇒ Object
(also: #evolve_until)
breaks if the block (which is passed the best individual each “check_every” generations) returns true.
-
#evolve_until_population(generations = DEFAULT_MAX_GENS, check_every = 10) ⇒ Object
breaks if the block (which is passed the population each “check_every” generations) returns true.
-
#initialize(genotype_class, population_size = 20) ⇒ Population
constructor
A new instance of Population.
Constructor Details
#initialize(genotype_class, population_size = 20) ⇒ Population
Returns a new instance of Population.
10 11 12 13 14 |
# File 'lib/charlie/population.rb', line 10 def initialize(genotype_class,population_size=20) @size = population_size @genotype_class = genotype_class @population = Array.new(population_size){ genotype_class.new } end |
Instance Attribute Details
#genotype_class ⇒ Object (readonly)
Returns the value of attribute genotype_class.
9 10 11 |
# File 'lib/charlie/population.rb', line 9 def genotype_class @genotype_class end |
#population ⇒ Object (readonly)
Returns the value of attribute population.
9 10 11 |
# File 'lib/charlie/population.rb', line 9 def population @population end |
#size ⇒ Object (readonly)
Returns the value of attribute size.
9 10 11 |
# File 'lib/charlie/population.rb', line 9 def size @size end |
Instance Method Details
#evolve_block(max_generations = DEFAULT_MAX_GENS) {|@population, 0| ... } ⇒ Object
yields population and generation number to block each generation, for a maximum of max_generations.
17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/charlie/population.rb', line 17 def evolve_block(max_generations=DEFAULT_MAX_GENS) yield @population, 0 (max_generations || DEFAULT_MAX_GENS).times {|generation| @population = @genotype_class.next_generation(@population){|*parents| ch = [*@genotype_class.cross(*parents)] ch.each{|c| c.mutate! } ch } yield @population, generation+1 } @population end |
#evolve_on_console(generations = DEFAULT_MAX_GENS) ⇒ Object Also known as: evolve
Runs the genetic algorithm with some stats on the console. Returns the population sorted by fitness.
37 38 39 40 41 42 43 44 45 46 |
# File 'lib/charlie/population.rb', line 37 def evolve_on_console(generations=DEFAULT_MAX_GENS) puts "Generation\tBest Fitness\t\tBest Individual" evolve_block(generations) {|p,g| best = p.max puts "#{g}\t\t#{best.fitness}\t\t#{best.to_s}" } @population = @population.sort_by{|x|x.fitness} puts "Finished: Best fitness = #{@population[-1].fitness}" @population end |
#evolve_silent(generations = DEFAULT_MAX_GENS) ⇒ Object
Runs the genetic algorithm without any output. Returns the population sorted by fitness (unsorted for co-evolution).
31 32 33 34 |
# File 'lib/charlie/population.rb', line 31 def evolve_silent(generations=DEFAULT_MAX_GENS) evolve_block(generations){} @population.sort_by{|x|x.fitness} rescue @population end |
#evolve_until_best(generations = DEFAULT_MAX_GENS, check_every = 10) ⇒ Object Also known as: evolve_until
breaks if the block (which is passed the best individual each “check_every” generations) returns true. returns an array [population, generations needed]. generations needed==nil for no convergence.
64 65 66 67 68 69 70 71 72 73 |
# File 'lib/charlie/population.rb', line 64 def evolve_until_best(generations=DEFAULT_MAX_GENS,check_every=10) tot_gens = nil evolve_block(generations) {|p,g| if (g % check_every).zero? && yield(p.max) tot_gens = g break end } [@population, tot_gens] end |
#evolve_until_population(generations = DEFAULT_MAX_GENS, check_every = 10) ⇒ Object
breaks if the block (which is passed the population each “check_every” generations) returns true. returns an array [population, generations needed]. generations needed==nil for no convergence.
51 52 53 54 55 56 57 58 59 60 |
# File 'lib/charlie/population.rb', line 51 def evolve_until_population(generations=DEFAULT_MAX_GENS,check_every=10) tot_gens = nil evolve_block(generations) {|p,g| if (g % check_every).zero? && yield(p) tot_gens = g break end } [@population, tot_gens] end |