Class: Wallace::Subpopulation

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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

#capacityObject (readonly)

Returns the value of attribute capacity.



7
8
9
# File 'lib/core/subpopulation.rb', line 7

def capacity
  @capacity
end

#contentsObject

Returns the value of attribute contents.



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

def contents
  @contents
end

#speciesObject (readonly)

Returns the value of attribute species.



7
8
9
# File 'lib/core/subpopulation.rb', line 7

def species
  @species
end

Instance Method Details

#bestObject 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

#sizeObject 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

#worstObject 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