| Class | Algorithms::SimulatedAnnealing |
| In: |
lib/algorithms/simulated_annealing.rb
|
| Parent: | Object |
| argmin | -> | best |
| argmin | [R] | |
| diff_cost | [R] | |
| generator | [R] | |
| global_threshold | [R] | |
| initial_cost | [R] | |
| initial_temperature | [R] | |
| iteration | [R] | |
| iteration_max | [R] | |
| iteration_modulus | [R] | |
| output | [R] | |
| probability_threshold | [R] | |
| step_modulus | [R] | |
| step_multiplicator | [R] | |
| support | [R] |
Example:
obj = MySimulatedAnnealingObject.new
SimulatedAnnealing.new(
:support => obj,
:global_threshold => 200,
:step_multiplicator => 0.63,
:initial_temperature => 10,
:initial_cost => obj.cost,
:iteration_modulus => 500, # display status once a 500
:step_modulus => 5000 # change the temperature once a 5000
)
# File lib/algorithms/simulated_annealing.rb, line 33 def initialize ( opts ) @support = opts[:support] @global_threshold = opts[:global_threshold] || opts[:global_thresold] # :) backward compatibility @step_multiplicator = opts[:step_multiplicator] @temperature = opts[:initial_temperature] @cur_cost = opts[:initial_cost] @output = opts[:output] || STDOUT @iteration_modulus = opts[:iteration_modulus] || 1 @step_modulus = opts[:step_modulus] || 1 @generator = opts[:generator] @iteration_max = opts[:iteration_max] || (1.0 / 0.0) # Infinity @progression = ' ' @diff_cost = 0 @iteration = 0 @probability_threshold = 0 @argmin = @support.copy_of_the_current_solution @min = @cur_cost end
# File lib/algorithms/simulated_annealing.rb, line 138 def choose_probability ( generator=nil ) 1.0.choose(generator) end
# File lib/algorithms/simulated_annealing.rb, line 111 def disp ( output=@output ) args = [ @up, @same, @down, @reject, @temperature, @proba_mean / @proba_mean_count, @threshold_mean / @threshold_mean_count, @cur_cost ] fmt = '- { "+%-5d|=%-5d|-%-5d|x%-5d", temp: %-4f, ' + 'prob: %-7f, thres: %-7f, cost: %-5s }' output.puts fmt % args reset end
# File lib/algorithms/simulated_annealing.rb, line 132 def progression ( x ) @progression = x disp if (@iteration % @iteration_modulus).zero? end
# File lib/algorithms/simulated_annealing.rb, line 55 def reset @up, @down, @same, @reject, @proba_mean, @proba_mean_count, @threshold_mean, @threshold_mean_count = @@zeros end
# File lib/algorithms/simulated_annealing.rb, line 61 def run reset @output.puts '---' while @cur_cost > @global_threshold and @iteration < @iteration_max # puts "Iteration(#{it}) #{@cur_cost}" @transition = @support.choose_transition(@generator) @diff_cost = @support.transition_cost(@cur_cost, @transition) @iteration += 1 if (@iteration % @step_modulus).zero? @temperature *= @step_multiplicator end if @diff_cost > 0 @probability_threshold = Math.exp(- @diff_cost.to_f / @temperature) @proba = choose_probability(@generator) @proba_mean += @proba @proba_mean_count += 1 @threshold_mean += @probability_threshold @threshold_mean_count += 1 if @proba < @probability_threshold progression '+' @up += 1 else progression ' ' @reject += 1 next end else @probability_threshold = 1 @proba = 1 if @diff_cost.zero? @same += 1 progression '=' else @down += 1 progression '-' end end @support.apply_transition(@transition) @cur_cost += @diff_cost if @cur_cost < @min @min = @cur_cost @argmin = @support.copy_of_the_current_solution end end end