Class: SA::Context
- Inherits:
-
Object
- Object
- SA::Context
- Defined in:
- lib/sa/context.rb
Instance Attribute Summary collapse
-
#best_energy ⇒ Object
Returns the value of attribute best_energy.
-
#best_state ⇒ Object
Returns the value of attribute best_state.
Instance Method Summary collapse
-
#annealing(unit, temp, stop_temp, cool_proc) ⇒ Object
options: temp: Int or Float cool: Proc, return new Temp stop_temp: Int or Float, stop annealing when temp < stop_temp.
- #transfer(energy, state) ⇒ Object
Instance Attribute Details
#best_energy ⇒ Object
Returns the value of attribute best_energy.
3 4 5 |
# File 'lib/sa/context.rb', line 3 def best_energy @best_energy end |
#best_state ⇒ Object
Returns the value of attribute best_state.
3 4 5 |
# File 'lib/sa/context.rb', line 3 def best_state @best_state end |
Instance Method Details
#annealing(unit, temp, stop_temp, cool_proc) ⇒ Object
options:
temp: Int or Float
cool: Proc, return new Temp
stop_temp: Int or Float, stop annealing when temp < stop_temp
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/sa/context.rb', line 9 def annealing(unit, temp, stop_temp, cool_proc) @best_state = unit.state @best_energy = unit.energy @current_energy = unit.energy @current_temp = temp.to_f while @current_temp > stop_temp do unit.sa_iteration(self, @current_temp) @current_temp = cool_proc.call(@current_temp).to_f end unit.state = @best_state if @best_energy < unit.energy unit end |
#transfer(energy, state) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/sa/context.rb', line 25 def transfer(energy, state) if energy <= @current_energy @current_energy = energy if energy < best_energy @best_energy = energy @best_state = state.dup end true else p = Math::E ** -((energy - @current_energy) / @current_temp) v = Kernel.rand if v < p @current_energy = energy true else false end end end |