Class: EvoSynth::Evaluator

Inherits:
Object
  • Object
show all
Includes:
Observable
Defined in:
lib/evosynth/core/evaluator.rb

Overview

Baseclass for all fitness evaluators. It also counts how often it was used to calculate the fitness of a given individual and how often it actually calculated the fitness.

For simple problems you just need to overwrite calculate_fitness(individual)

Obervers get notified each time calculate_and_set_fitness(individual) gets called, not on actual calculations

Direct Known Subclasses

Problems::GraphColouring, Problems::TSP

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeEvaluator

Returns a new Evaluator object



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

def initialize
  reset_counters
end

Instance Attribute Details

#calculatedObject (readonly)

How often did the Evaluator actually calculate a fitness value



46
47
48
# File 'lib/evosynth/core/evaluator.rb', line 46

def calculated
  @calculated
end

#calledObject (readonly)

How often did the Evaluator return a fitness value



42
43
44
# File 'lib/evosynth/core/evaluator.rb', line 42

def called
  @called
end

Instance Method Details

#calculate_and_set_fitness(individual) ⇒ Object

Calculates and sets the fitness of the given individual if the individual has changed, otherwise it just returns the cached fitness of the individual



57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/evosynth/core/evaluator.rb', line 57

def calculate_and_set_fitness(individual)
  @called += 1

  if individual.changed?
    @calculated += 1
    individual.fitness = calculate_fitness(individual)
  end

  changed
  notify_observers self, @called
  individual.fitness
end

#calculate_and_set_initial_fitness(individual) ⇒ Object

Calculates and sets the initial fitness of the given individual.



72
73
74
75
76
77
78
79
80
81
# File 'lib/evosynth/core/evaluator.rb', line 72

def calculate_and_set_initial_fitness(individual)
  @called += 1
  @calculated += 1

  individual.fitness = calculate_initial_fitness(individual)

  changed
  notify_observers self, @called
  individual.fitness
end

#calculate_fitness(individual) ⇒ Object

This function is actually used to calculate the fitness of a individual. It’s the “fitness function”.

Raises:

  • (NotImplementedError)


85
86
87
# File 'lib/evosynth/core/evaluator.rb', line 85

def calculate_fitness(individual)
  raise NotImplementedError, "please implement calculate_fitness!"
end

#calculate_initial_fitness(individual) ⇒ Object

This function is used to calculate an intitial fitness value for a individual. Calls calculate_fitness by default.



91
92
93
# File 'lib/evosynth/core/evaluator.rb', line 91

def calculate_initial_fitness(individual)
  calculate_fitness(individual)
end

#reset_countersObject

Reset the called/calculated counters of the Evaluator



97
98
99
100
# File 'lib/evosynth/core/evaluator.rb', line 97

def reset_counters
  @called = 0
  @calculated = 0
end

#to_sObject

Returns a human readable reprasentation of the Evaluator



104
105
106
# File 'lib/evosynth/core/evaluator.rb', line 104

def to_s
  "Evaluator <called: #{@called}, calculated: #{@calculated}>"
end