Class: Algorithm::LocalSearch::SimulatedAnnealing

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

Instance Attribute Summary collapse

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 Attribute Details

#temperatureObject (readonly)

Returns the value of attribute temperature.



5
6
7
# File 'lib/opt_alg_framework/algorithm/local_search/simulated_annealing.rb', line 5

def temperature
  @temperature
end

Instance Method Details

#startObject

Main method.



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

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