Class: Algorithms::SimulatedAnnealing::Engine

Inherits:
Object
  • Object
show all
Defined in:
lib/algorithms/simulated_annealing.rb

Instance Method Summary collapse

Constructor Details

#initialize(solution) ⇒ Engine

Returns a new instance of Engine.



16
17
18
19
# File 'lib/algorithms/simulated_annealing.rb', line 16

def initialize(solution)
  @initial_solution = solution
  @solutions_history = [@initial_solution]
end

Instance Method Details

#best_solutionObject



32
# File 'lib/algorithms/simulated_annealing.rb', line 32

def best_solution; @solutions_history[-1] end

#run(iterations) ⇒ Object



21
22
23
24
25
26
27
28
29
30
# File 'lib/algorithms/simulated_annealing.rb', line 21

def run(iterations)
  iterations.times do |i|
    temperature = iterations.to_f / i.to_f
    current_solution = @solutions_history[-1]
    next_solution = current_solution.next_solution
    if acceptance_probability(current_solution.score, next_solution.score, temperature) > rand
      @solutions_history << next_solution
    end
  end
end