Class: EvoSynth::Individual

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/evosynth/core/individual.rb

Overview

Base class for individuals

Direct Known Subclasses

MaximizingIndividual, MinimizingIndividual

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#fitnessObject

Returns the fitness of this individual



38
39
40
# File 'lib/evosynth/core/individual.rb', line 38

def fitness
  @fitness
end

#genomeObject

Genome of the Individual, it should provide a changed attribute



34
35
36
# File 'lib/evosynth/core/individual.rb', line 34

def genome
  @genome
end

Instance Method Details

#<=>(another) ⇒ Object

If you compare individuals the semantic of “<=>” is the following:

a > b -> individual a is a better solution than individual b a < b -> individual b is a better solution than individual a a == b -> a and b have a equal fitness value



68
69
70
# File 'lib/evosynth/core/individual.rb', line 68

def <=>(another)
  compare_fitness_values(fitness, another.fitness)
end

#changed?Boolean

Returns true, if this individual (most of the time its genome) has changed. Otherwise it will return false.

Returns:

  • (Boolean)


50
51
52
# File 'lib/evosynth/core/individual.rb', line 50

def changed?
  @genome.changed?
end

#compare_fitness_values(one, two) ⇒ Object

Should be implemented by subclasses

Raises:

  • (NotImplementedError)


74
75
76
# File 'lib/evosynth/core/individual.rb', line 74

def compare_fitness_values(one, two)
  raise NotImplementedError, "please implement compare_fitness_values!"
end

#deep_cloneObject

Clones and returns a individual and its genome (deep copy).



56
57
58
59
60
# File 'lib/evosynth/core/individual.rb', line 56

def deep_clone
  my_clone = self.clone
  my_clone.genome = Marshal.load( Marshal.dump( self.genome ) )
  my_clone
end

#maximizes?Boolean

Returns wether this Individual needs to be maximized.

Returns:

  • (Boolean)


86
87
88
# File 'lib/evosynth/core/individual.rb', line 86

def maximizes?
  compare_fitness_values(1,0) > 0
end

#minimizes?Boolean

Returns wether this Individual needs to be minimized.

Returns:

  • (Boolean)


80
81
82
# File 'lib/evosynth/core/individual.rb', line 80

def minimizes?
  compare_fitness_values(1,0) < 0
end