Module: EvoSynth::Evolvers::RunnableEvolver

Includes:
Observable
Included in:
Evolver
Defined in:
lib/evosynth/evolvers/runnable_evolver.rb

Overview

This module provides different termination strategies for evolvers as well as the observable property (observers will get notified each generation)

Defined Under Namespace

Classes: Goal

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#generations_computedObject (readonly)

Returns the value of attribute generations_computed.



37
38
39
# File 'lib/evosynth/evolvers/runnable_evolver.rb', line 37

def generations_computed
  @generations_computed
end

Instance Method Details

#run_until(&condition) ⇒ Object

:yields: generations computed, best solution



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/evosynth/evolvers/runnable_evolver.rb', line 39

def run_until(&condition) # :yields: generations computed, best solution
  @generations_computed = 0
  changed
  notify_observers self, @generations_computed

  case condition.arity
    when 0
      loop_condition = condition
    when 1
      loop_condition = lambda { !yield @generations_computed }
    when 2
      loop_condition = lambda { !yield @generations_computed, best_solution }
  else
    raise ArgumentError, "please provide a block with the arity of 0, 1 or 2"
  end

  while loop_condition.call
    next_generation
    @generations_computed += 1
    changed
    notify_observers self, @generations_computed
  end

  return_result
end

#run_until_fitness_reached(fitness) ⇒ Object



69
70
71
72
# File 'lib/evosynth/evolvers/runnable_evolver.rb', line 69

def run_until_fitness_reached(fitness)
  goal = Goal.new(fitness)
  run_until { |gen, best| best >= goal }
end

#run_until_generations_reached(max_generations) ⇒ Object



65
66
67
# File 'lib/evosynth/evolvers/runnable_evolver.rb', line 65

def run_until_generations_reached(max_generations)
  run_until { |gen| gen == max_generations }
end