Class: Annealing::Simulator

Inherits:
Object
  • Object
show all
Defined in:
lib/annealing/simulator.rb

Overview

It runs simulated annealing

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config_hash = {}) ⇒ Simulator

Returns a new instance of Simulator.



8
9
10
# File 'lib/annealing/simulator.rb', line 8

def initialize(config_hash = {})
  @configuration = Annealing.configuration.merge(config_hash)
end

Instance Attribute Details

#configurationObject (readonly)

Returns the value of attribute configuration.



6
7
8
# File 'lib/annealing/simulator.rb', line 6

def configuration
  @configuration
end

Instance Method Details

#run(initial_state, config_hash = {}) ⇒ Object

rubocop:disable Metrics/MethodLength



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/annealing/simulator.rb', line 13

def run(initial_state, config_hash = {})
  with_runtime_config(config_hash) do |runtime_config|
    initial_temperature = runtime_config.temperature
    current = Metal.new(initial_state, initial_temperature, runtime_config)
    best = current
    steps = 0
    until termination_condition_met?(current, runtime_config)
      steps += 1
      current = reduce_temperature(current, steps, runtime_config)
      # If the current state has lower energy than the previous best (lowest energy) state
      # we've seen so far, the current state is the new best state.
      best = current if best.lower_energy?(current)
    end
    final_or_best(current, best, runtime_config)
  end
end