Class: Wallace::Individual
- Inherits:
-
Object
- Object
- Wallace::Individual
- Includes:
- Comparable
- Defined in:
- lib/core/individual.rb
Overview
This class is the base class used to represent individuals within the population. The data (e.g. list, tree, string) for an individual is held within its data attribute.
Instance Attribute Summary collapse
-
#data ⇒ Object
Neither the species nor the data of the individual may be modified once the individual has been created.
-
#fitness ⇒ Object
Allow the fitness of the individual to be both read and written to.
-
#species ⇒ Object
Neither the species nor the data of the individual may be modified once the individual has been created.
Instance Method Summary collapse
-
#<=>(other) ⇒ Object
Compares the fitness of this individual against that of another.
-
#clone(opts = {}) ⇒ Object
Creates a clone of this individual.
-
#evaluated? ⇒ Boolean
Checks whether this individual has been evaluated.
-
#finish! ⇒ Object
Post-processes this individual.
-
#initialize(species, data, opts = {}) ⇒ Individual
constructor
Constructs a new individual.
-
#to_s ⇒ Object
Produces a string representation of this individual.
Constructor Details
#initialize(species, data, opts = {}) ⇒ Individual
Constructs a new individual.
Parameters:
-
species, the species of the individual.
-
data, the data for this individual.
-
opts, keyword arguments for this constructor.
-
-> fitness, the fitness object for this individual (keyword).
22 23 24 25 26 27 |
# File 'lib/core/individual.rb', line 22 def initialize(species, data, opts = {}) @species = species @fitness = nil @data = data @fitness = opts[:fitness] end |
Instance Attribute Details
#data ⇒ Object
Neither the species nor the data of the individual may be modified once the individual has been created.
9 10 11 |
# File 'lib/core/individual.rb', line 9 def data @data end |
#fitness ⇒ Object
Allow the fitness of the individual to be both read and written to.
13 14 15 |
# File 'lib/core/individual.rb', line 13 def fitness @fitness end |
#species ⇒ Object
Neither the species nor the data of the individual may be modified once the individual has been created.
9 10 11 |
# File 'lib/core/individual.rb', line 9 def species @species end |
Instance Method Details
#<=>(other) ⇒ Object
Compares the fitness of this individual against that of another.
Parameters:
-
other, the individual to compare against.
45 46 47 |
# File 'lib/core/individual.rb', line 45 def <=>(other) @fitness <=> other.fitness end |
#clone(opts = {}) ⇒ Object
Creates a clone of this individual.
Parameters:
-
opts, keyword arguments for this method.
-
-> copy_fitness, flag indicating whether fitness information should be copied across to the cloned individual.
55 56 57 58 |
# File 'lib/core/individual.rb', line 55 def clone(opts = {}) copy_fitness = opts[:copy_fitness] return Wallace::Individual.new(species, data.clone, fitness: copy_fitness ? fitness : nil) end |
#evaluated? ⇒ Boolean
Checks whether this individual has been evaluated. Returns true if it has, false if otherwise.
31 32 33 |
# File 'lib/core/individual.rb', line 31 def evaluated? return (not @fitness.nil?) end |
#finish! ⇒ Object
Post-processes this individual. Returns the post-processed individual.
37 38 39 |
# File 'lib/core/individual.rb', line 37 def finish! @species.finish!(self) end |
#to_s ⇒ Object
Produces a string representation of this individual.
61 62 63 |
# File 'lib/core/individual.rb', line 61 def to_s return @data.to_s end |