Class: Algorithm::LocalSearch::SimulatedAnnealing

Inherits:
Object
  • Object
show all
Includes:
AlgorithmInterface, Math
Defined in:
lib/opt_alg_framework/algorithm/local_search/simulated_annealing.rb

Instance Method Summary collapse

Constructor Details

#initialize(params) ⇒ SimulatedAnnealing

Initialize specifying the cooling rate percentage (@cooling_rate), number of iterations in each temperature (@max_iterations) and passing an instantiated problem and tweak operator class.



9
10
11
12
13
14
15
# File 'lib/opt_alg_framework/algorithm/local_search/simulated_annealing.rb', line 9

def initialize(params)
  @cooling_rate = params[:cooling_rate]
  @problem = params[:problem]
  @tweak_operator = params[:tweak_operator]
  @max_iterations = params[:max_iterations]
  @temperature = initial_temperature
end

Instance Method Details

#startObject

solution_b = best solution solution_n = neighbor solution solution_c = current solution



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/opt_alg_framework/algorithm/local_search/simulated_annealing.rb', line 20

def start
  solution_b = encapsulate_solution(@problem.default_solution.shuffle)
  solution_c = solution_b.dup
  temperature = @temperature
  while temperature > 1 do
    iteration = 1
    while iteration < @max_iterations do
      solution_n = encapsulate_solution(@tweak_operator.tweak(solution_c[:solution]))
      solution_c = solution_n.dup if accept?(solution_c[:fitness], solution_n[:fitness], temperature)
      solution_b = solution_c.dup if solution_c[:fitness] < solution_b[:fitness]
      iteration += 1
    end
    temperature *= 1 - @cooling_rate
  end
  solution_b
end