Class: Annealing::Metal

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

Overview

It manages the total energy of a given collection

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(current_state, current_temperature, configuration = nil) ⇒ Metal

Returns a new instance of Metal.



8
9
10
11
12
# File 'lib/annealing/metal.rb', line 8

def initialize(current_state, current_temperature, configuration = nil)
  @configuration = configuration || Annealing.configuration.merge({})
  @state = current_state
  @temperature = current_temperature
end

Instance Attribute Details

#configurationObject (readonly)

Returns the value of attribute configuration.



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

def configuration
  @configuration
end

#stateObject (readonly)

Returns the value of attribute state.



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

def state
  @state
end

#temperatureObject (readonly)

Returns the value of attribute temperature.



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

def temperature
  @temperature
end

Instance Method Details

#cool!(new_temperature) ⇒ Object

This method is not idempotent! It relies on random probability to select the next state



20
21
22
23
24
25
26
27
28
# File 'lib/annealing/metal.rb', line 20

def cool!(new_temperature)
  cooled_metal = cool(new_temperature)
  if prefer?(cooled_metal)
    cooled_metal
  else
    @temperature = new_temperature
    self
  end
end

#energyObject



14
15
16
# File 'lib/annealing/metal.rb', line 14

def energy
  @energy ||= configuration.energy_calculator.call(state)
end