Class: Wallace::Evaluator

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

Overview

The evaluator is responsible for determining the fitness of given individuals within the population. An evaluation function is provided to each evaluator which produces a fitness measure object for a given individual.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}, &f) ⇒ Evaluator

Constructs a new evaluator. Attach the given fitness calculation method to the evaluate method of this evaluator.

Parameters:

  • opts, a hash of keyword options for this evaluator. -> threads, the number of threads that evaluation should be split across.

  • f, function to calculate and return the fitness of a given individual.



16
17
18
19
# File 'lib/core/evaluator.rb', line 16

def initialize(opts = {}, &f)
  @threads = opts[:threads] || 1
  define_singleton_method(:evaluate, &f)
end

Instance Attribute Details

#threadsObject (readonly)

Returns the value of attribute threads.



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

def threads
  @threads
end

Instance Method Details

#evaluate(rng, individual) ⇒ Object

Evaluates a given individual and returns their fitness score. Should not modify the providied individual.

Parameters:

  • rng, random number generator to use during evaluation.

  • individual, the individual whose fitness should be calculated.

Raises:

  • (NotImplementedError)


48
49
50
# File 'lib/core/evaluator.rb', line 48

def evaluate(rng, individual)
  raise NotImplementedError, 'No evaluation function was provided to this evaluator.'
end

#process!(rng, population) ⇒ Object

Evaluates all given individuals within a population. If multi-threading is enabled then individuals are split into equal (as possible) chunks. For now concurrency has not been implemented, and will be thought about in more detail in a future version.

Parameters:

  • rng, random number generator to use during evaluation.

  • population, the population of individuals to evaluate.



29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/core/evaluator.rb', line 29

def process!(rng, population)
  population.subpopulations.each do |s|
    if @threads > 1
      s.contents.peach(@threads) { |i| i.fitness = evaluate(rng, i) }
    else
      s.contents.each { |i| i.fitness = evaluate(rng, i) }
    end
  end

  # Should we find the best and worst individuals in each sub-population?

end