Class: Wallace::Individual

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

Instance Method Summary collapse

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

#dataObject

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

#fitnessObject

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

#speciesObject

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.

Returns:

  • (Boolean)


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_sObject

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