Class: MutationSearch

Inherits:
Optimizer show all
Defined in:
lib/mutation_search.rb

Overview

This class is an adapter for generation based optimiztion algorithms. The user has to specify 3 functions to implement different algorithms:

  1. The termination criterion, this function recives the number of steps and

the results array. The results array is an array of arrays which contains the results of all past steps. The format is like this:

[
  # Generation 1
  [ 
    # Generation 1 Candidate 1
    [
      # Generation 1 Candidate 1 Genotype
      flagstate_candidate1,
      # Generation 1 Candidate 1 Result
      job_result1
    ],
    # Generation 1 Candidate 2
    ...
  ],
  # Generation 2
  [
    ...
  ]
  ...
]

The termination criterion decides if the algorithm shoud stop by returning true.

  1. The init function and the mutate function. Both recive the same arguments:

the current genotypes (the init gets only an empty array), the results as described above (init gets an empty array again), a random generator, the mutation state. The mutation state is an object that gets passed to the init and mutate function and can be used to store state that is used across generations.

The return values of the init and mutate function are the genotypes of the next generation and the modified mutation_state (this may be a new object, if necessary).

Refer to the code of the OptimizerFactory module for examples.

Constant Summary

Constants included from LoggedClass

LoggedClass::LoggedClasses

Instance Attribute Summary

Attributes inherited from Optimizer

#results

Instance Method Summary collapse

Methods inherited from Optimizer

#run

Methods included from LoggedClass

#debug, #error, #fatal, generate_outputter_config, included, #info, #warn

Constructor Details

#initialize(name, seed, evaluator, init, mutate, termination_criterion, load_state_filename = nil) ⇒ MutationSearch

Returns a new generation based Optimizer.

Parameters:

  • name (String)

    Name of the Algorithm.

  • seed (Fixnum)

    The seed for the random generator.

  • evaluator (Evaluator)

    The Evaluator to use.

  • init (Proc)

    The init function.

  • mutate (Proc)

    The mutate function.

  • termination_criterion (Proc)

    The termination criterion.

  • flag_set (FlagSet)

    The flag set to use.

  • load_state_filename (String) (defaults to: nil)

    The name of a file to load the state from.


52
53
54
55
56
57
# File 'lib/mutation_search.rb', line 52

def initialize(name, seed, evaluator, init, mutate,
               termination_criterion, load_state_filename = nil)
  super name, seed, evaluator, load_state_filename
  @init, @mutate, @termination_criterion = init, mutate, termination_criterion
  @genotypes = []
end

Instance Method Details

#load_state(filename) ⇒ Object


91
92
93
94
95
96
# File 'lib/mutation_search.rb', line 91

def load_state(filename)
  File.open(filename, "r") do |file|
    _, @steps, @results, @genotypes,
      @mutation_state, @random = Marshal.restore file.read
  end
end

#save_state(filename) ⇒ Object


83
84
85
86
87
88
# File 'lib/mutation_search.rb', line 83

def save_state(filename)
  File.open(filename, "w") do |file|
    file.write (Marshal.dump [:mutation_search, @steps, @results,
                              @genotypes, @mutation_state, @random])
  end
end

#stepObject


60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/mutation_search.rb', line 60

def step()
  # At first initialize the flags
  if @steps.zero? then
    @genotypes, @mutation_state = @init[@genotypes, @results,
                                        @random, @mutation_state]
  end

  # evaluate genotypes
  @results << (@evaluator[@genotypes].zip (@genotypes.map do |genotype|
    genotype.dup
  end))

  # increment step-counter
  @steps += 1
  @evaluator.step

  raise OptimizerCompleteSignal if @termination_criterion[@steps, @results]

  @genotypes, @mutation_state = @mutate[@genotypes, @results,
                                        @random, @mutation_state]
end