Class: Wallace::Termination

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

Overview

This class is used to store and check against the termination criteria of the evolutionary process.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Termination

Constructs a new termination criteria.

Parameters:

  • opts, a hash of keyword options for this method. -> generations, limit on the total number of generations to perform. -> stagnation, limit on the successive number of generations without change to the best solution. -> fitness, target fitness value. -> error, the maximum allowed error in the target fitness value.



18
19
20
21
22
23
# File 'lib/core/termination.rb', line 18

def initialize(opts = {})
  @generation_limit = opts[:generations] || 200
  @stagnation_limit = opts[:stagnation]
  @fitness_limit = opts[:fitness]
  @error = opts[:error] || 0
end

Instance Attribute Details

#errorObject (readonly)

Returns the value of attribute error.



5
6
7
# File 'lib/core/termination.rb', line 5

def error
  @error
end

#fitness_limitObject (readonly)

Returns the value of attribute fitness_limit.



5
6
7
# File 'lib/core/termination.rb', line 5

def fitness_limit
  @fitness_limit
end

#generation_limitObject (readonly)

Returns the value of attribute generation_limit.



5
6
7
# File 'lib/core/termination.rb', line 5

def generation_limit
  @generation_limit
end

#stagnation_limitObject (readonly)

Returns the value of attribute stagnation_limit.



5
6
7
# File 'lib/core/termination.rb', line 5

def stagnation_limit
  @stagnation_limit
end

Instance Method Details

#finished?(state) ⇒ Boolean

Determines whether the evolutionary process has reached its termination criteria or not.

Parameters:

  • state, the current state of the evolution.

Returns true if it has finished and false if otherwise.

Returns:

  • (Boolean)


32
33
34
35
36
37
# File 'lib/core/termination.rb', line 32

def finished?(state)
  return true if not @generation_limit.nil? and state.generations == @generation_limit
  return true if not @fitness_limit.nil? and state.best.fitness.value <= @fitness_limit + @error
  return true if not @stagnation_limit.nil? and state.stagnation == @stagnation_limit
  return false
end