Class: Wallace::Population

Inherits:
Object
  • Object
show all
Defined in:
lib/core/population.rb

Overview

In this current version of Wallace each population may only hold individuals of a single species. For future versions supporting co-operative and competitive evolution it should be able to adapt the population model to fit.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(breeder, subpopulations) ⇒ Population

Constructs a new population.

Parameters:

  • breeder, the breeder used to generate individuals for successive generations.

  • subpopulations, a list of subpopulations contained within this population.



13
14
15
16
# File 'lib/core/population.rb', line 13

def initialize(breeder, subpopulations)
  @breeder = breeder
  @subpopulations = subpopulations
end

Instance Attribute Details

#subpopulationsObject (readonly)

Returns the value of attribute subpopulations.



6
7
8
# File 'lib/core/population.rb', line 6

def subpopulations
  @subpopulations
end

Instance Method Details

#bestObject

Selects the best individual from the current population.



41
42
43
# File 'lib/core/population.rb', line 41

def best
  @subpopulations.map { |s| s.best }.min
end

#breed!(rng) ⇒ Object

Breeds the next generation of individuals for this population.

Parameters:

  • rng, random number generator to use during breeding.



36
37
38
# File 'lib/core/population.rb', line 36

def breed!(rng)
  @subpopulations.each { |s| @breeder.breed!(rng, s) }
end

#clear!Object

Clears the contents of each sub-population within this population and resets all attached components.



20
21
22
# File 'lib/core/population.rb', line 20

def clear!
  @subpopulations.each { |s| s.clear }
end

#fresh!(rng) ⇒ Object

Initialises a fresh population of individuals.

Parameters:

  • rng, the random number generator to use.



28
29
30
# File 'lib/core/population.rb', line 28

def fresh!(rng)
  @subpopulations.each { |s| s.fresh!(rng) }
end

#sizeObject Also known as: length

Calculates the combined size (number of individuals) of this population.



51
52
53
# File 'lib/core/population.rb', line 51

def size
  @subpopulations.reduce(0) { |sum, sp| sum += sp.length }
end

#worstObject

Selects the worst individual from the current population.



46
47
48
# File 'lib/core/population.rb', line 46

def worst
  @subpopulations.map { |s| s.worst }.max
end