Class: Solution

Inherits:
Object
  • Object
show all
Defined in:
lib/gimuby/genetic/solution/solution.rb

Overview

Base class for solutions, extended by each given problem

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(representation = nil) ⇒ Solution

Returns a new instance of Solution.



8
9
10
11
12
13
14
15
16
17
18
19
20
# 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

Instance Attribute Details

#check_strategyObject

Returns the value of attribute check_strategy.



22
23
24
# File 'lib/gimuby/genetic/solution/solution.rb', line 22

def check_strategy
  @check_strategy
end

#mutation_strategyObject

Returns the value of attribute mutation_strategy.



24
25
26
# File 'lib/gimuby/genetic/solution/solution.rb', line 24

def mutation_strategy
  @mutation_strategy
end

#new_generation_strategyObject

Returns the value of attribute new_generation_strategy.



23
24
25
# File 'lib/gimuby/genetic/solution/solution.rb', line 23

def new_generation_strategy
  @new_generation_strategy
end

Instance Method Details

#get_fitnessFloat

Returns:

  • (Float)


27
28
29
30
31
32
33
34
# 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_representationObject

Raises:

  • (NotImplementedError)


55
56
57
# File 'lib/gimuby/genetic/solution/solution.rb', line 55

def get_solution_representation
  raise NotImplementedError
end

#mutateObject



40
41
42
# File 'lib/gimuby/genetic/solution/solution.rb', line 40

def mutate
  @mutation_strategy.mutate(self)
end

#reproduce(sol1, sol2) ⇒ Object



44
45
46
47
48
49
50
51
52
53
# 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_stateObject



36
37
38
# File 'lib/gimuby/genetic/solution/solution.rb', line 36

def reset_fitness_state
  @fitness = nil
end

#set_solution_representation(representation) ⇒ Object

Raises:

  • (NotImplementedError)


59
60
61
# File 'lib/gimuby/genetic/solution/solution.rb', line 59

def set_solution_representation(representation)
  raise NotImplementedError
end