Class: Wallace::Subpopulation
- Inherits:
-
Object
- Object
- Wallace::Subpopulation
- Defined in:
- lib/core/subpopulation.rb
Overview
This class is used to hold sub-populations of individuals. For at least some length of time, subpopulations evolve independently, only interacting with the rest of the population (other subpopulations) during migration.
Instance Attribute Summary collapse
-
#capacity ⇒ Object
readonly
Returns the value of attribute capacity.
-
#contents ⇒ Object
Returns the value of attribute contents.
-
#species ⇒ Object
readonly
Returns the value of attribute species.
Instance Method Summary collapse
-
#best ⇒ Object
(also: #min)
Returns the best individual within this sub-population.
-
#clear! ⇒ Object
Clears the contents of this sub-population.
-
#fresh!(rng) ⇒ Object
Initialises a fresh sub-population of individuals.
-
#initialize(opts = {}) ⇒ Subpopulation
constructor
Constructs a new subpopulation.
-
#size ⇒ Object
(also: #length)
Returns the actual number of individuals within this population.
-
#worst ⇒ Object
(also: #max)
Returns the worst individual within this sub-population.
Constructor Details
#initialize(opts = {}) ⇒ Subpopulation
Constructs a new subpopulation.
Arguments
-
opts, a hash of keyword options for this method. -> size, the size of this subpopulation. -> species, the species of individuals within this sub-population.
16 17 18 19 20 |
# File 'lib/core/subpopulation.rb', line 16 def initialize(opts = {}) @capacity = opts[:size] @contents = [] @species = opts[:species] end |
Instance Attribute Details
#capacity ⇒ Object (readonly)
Returns the value of attribute capacity.
7 8 9 |
# File 'lib/core/subpopulation.rb', line 7 def capacity @capacity end |
#contents ⇒ Object
Returns the value of attribute contents.
6 7 8 |
# File 'lib/core/subpopulation.rb', line 6 def contents @contents end |
#species ⇒ Object (readonly)
Returns the value of attribute species.
7 8 9 |
# File 'lib/core/subpopulation.rb', line 7 def species @species end |
Instance Method Details
#best ⇒ Object Also known as: min
Returns the best individual within this sub-population.
36 37 38 |
# File 'lib/core/subpopulation.rb', line 36 def best @contents.min end |
#clear! ⇒ Object
Clears the contents of this sub-population.
31 32 33 |
# File 'lib/core/subpopulation.rb', line 31 def clear! @contents = [] end |
#fresh!(rng) ⇒ Object
Initialises a fresh sub-population of individuals.
Parameters:
-
rng, the random number generator to use.
26 27 28 |
# File 'lib/core/subpopulation.rb', line 26 def fresh!(rng) @contents = Array.new(@capacity) { @species.spawn(random: rng) } end |
#size ⇒ Object Also known as: length
Returns the actual number of individuals within this population.
48 49 50 |
# File 'lib/core/subpopulation.rb', line 48 def size @contents.size end |
#worst ⇒ Object Also known as: max
Returns the worst individual within this sub-population.
42 43 44 |
# File 'lib/core/subpopulation.rb', line 42 def worst @contents.max end |