Class: EvoSynth::Population
- Inherits:
-
ArrayGenome
- Object
- Array
- ArrayGenome
- EvoSynth::Population
- Defined in:
- lib/evosynth/core/population.rb
Overview
This class is used to create and maintain a population. In this case a population is simply a Array of individuals that keeps track of changes.
Constant Summary
Constants inherited from ArrayGenome
ArrayGenome::METHODS_THAT_CHANGE_ARRAY
Instance Method Summary collapse
-
#add(individual) ⇒ Object
Adds a individual to the population.
-
#best(count = 1) ⇒ Object
:call-seq: p = Population.new p.best #=> Returns the best individual p.best(3) #=> Returns the 3 best individuals.
-
#deep_clone ⇒ Object
Returns a clone of the population (deep_clone).
-
#initialize(*args) ⇒ Population
constructor
:call-seq: Population.new -> Population Population.new(size) -> Population (with given size) Population.new(size) { block } -> Population (with given size and block to intialize each individual).
-
#remove(individual) ⇒ Object
Removes a give individual (first occurence) from the population.
-
#to_s ⇒ Object
:call-seq: to_s -> string.
-
#worst(count = 1) ⇒ Object
:call-seq: p = Population.new p.worst #=> Returns the worst individual p.worst(3) #=> Returns the 3 worst individuals.
Methods inherited from ArrayGenome
Constructor Details
#initialize(*args) ⇒ Population
:call-seq: Population.new -> Population Population.new(size) -> Population (with given size) Population.new(size) { block } -> Population (with given size and block to intialize each individual)
Creates a population
39 40 41 42 |
# File 'lib/evosynth/core/population.rb', line 39 def initialize(*args) super(*args) self.map! { |individual| yield } if block_given? end |
Instance Method Details
#add(individual) ⇒ Object
Adds a individual to the population.
54 55 56 |
# File 'lib/evosynth/core/population.rb', line 54 def add(individual) self << individual end |
#best(count = 1) ⇒ Object
:call-seq: p = Population.new p.best #=> Returns the best individual p.best(3) #=> Returns the 3 best individuals
Returns the best N individuals (by comparing with <=>) of the population.
83 84 85 86 87 |
# File 'lib/evosynth/core/population.rb', line 83 def best(count = 1) self.sort! if self.changed? self.changed = false count == 1 ? self.last : self.last(count).reverse end |
#deep_clone ⇒ Object
Returns a clone of the population (deep_clone)
46 47 48 49 50 |
# File 'lib/evosynth/core/population.rb', line 46 def deep_clone my_clone = self.clone self.each_index { |index| my_clone[index] = self[index].deep_clone } my_clone end |
#remove(individual) ⇒ Object
Removes a give individual (first occurence) from the population.
60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/evosynth/core/population.rb', line 60 def remove(individual) # FIXME: this is a rather ugly hack # -> should be replaced with a cool 1.9 function found = nil self.each_index do |index| if self[index] == individual found = index break end end self.delete_at(found) if found != nil end |
#to_s ⇒ Object
:call-seq: to_s -> string
Returns description of this individual
p = Population.new p.to_s #=> “Population <size=0>”
110 111 112 113 114 115 116 |
# File 'lib/evosynth/core/population.rb', line 110 def to_s begin "Population <size=#{self.size}, best.fitness=#{best.fitness}, worst.fitness=#{worst.fitness}>" rescue "Population <size=#{self.size}>" end end |
#worst(count = 1) ⇒ Object
:call-seq: p = Population.new p.worst #=> Returns the worst individual p.worst(3) #=> Returns the 3 worst individuals
Returns the worst N individuals (by comparing with <=>) of the population.
96 97 98 99 100 |
# File 'lib/evosynth/core/population.rb', line 96 def worst(count = 1) self.sort! if self.changed? self.changed = false count == 1 ? self.first : self.first(count).reverse end |