class Solution

Base class for solutions, extended by each given problem

Attributes

check_strategy[RW]
mutation_strategy[RW]
new_generation_strategy[RW]

Public Class Methods

new(representation = nil) click to toggle source
# File lib/gimuby/genetic/solution/solution.rb, line 8
def initialize(representation = nil)
  @fitness = nil

  @check_strategy ||= nil
  @new_generation_strategy ||= nil
  @mutation_strategy ||= nil

  if representation.nil?
    init_representation
  else
    set_solution_representation(representation)
  end
end

Public Instance Methods

get_fitness() click to toggle source

@return [Float]

# File lib/gimuby/genetic/solution/solution.rb, line 27
def get_fitness
  unless has_fitness?
    @fitness = evaluate
    event_data = {:solution => self}
    get_event_manager.trigger_event(:on_solution_needs_evaluation, event_data)
  end
  @fitness
end
get_solution_representation() click to toggle source
# File lib/gimuby/genetic/solution/solution.rb, line 55
def get_solution_representation
  raise NotImplementedError
end
mutate() click to toggle source
# File lib/gimuby/genetic/solution/solution.rb, line 40
def mutate
  @mutation_strategy.mutate(self)
end
reproduce(sol1, sol2) click to toggle source
# File lib/gimuby/genetic/solution/solution.rb, line 44
def reproduce(sol1, sol2)
  new_solutions_representations = @new_generation_strategy.reproduce(sol1, sol2)
  new_solutions_representations.map do |representation|
    solution = sol1.clone
    solution.set_solution_representation representation
    solution.check
    solution.reset_fitness_state
    solution
  end
end
reset_fitness_state() click to toggle source
# File lib/gimuby/genetic/solution/solution.rb, line 36
def reset_fitness_state
  @fitness = nil
end
set_solution_representation(representation) click to toggle source
# File lib/gimuby/genetic/solution/solution.rb, line 59
def set_solution_representation(representation)
  raise NotImplementedError
end

Protected Instance Methods

check() click to toggle source
# File lib/gimuby/genetic/solution/solution.rb, line 77
def check
  unless @check_strategy.nil?
    checked_representation = @check_strategy.check(get_solution_representation)
    set_solution_representation(checked_representation)
  end
end
evaluate() click to toggle source
# File lib/gimuby/genetic/solution/solution.rb, line 73
def evaluate
  raise NotImplementedError
end
get_event_manager() click to toggle source
# File lib/gimuby/genetic/solution/solution.rb, line 84
def get_event_manager
  $dependencies.event_manager
end
has_fitness?() click to toggle source
# File lib/gimuby/genetic/solution/solution.rb, line 69
def has_fitness?
  not @fitness.nil?
end
init_representation() click to toggle source
# File lib/gimuby/genetic/solution/solution.rb, line 65
def init_representation
  raise NotImplementedError
end