class Annealing::Pool
It manages the total energy of a given collection
Attributes
collection[R]
energy_calculator[R]
Public Class Methods
new(collection, energy_calculator = nil)
click to toggle source
# File lib/annealing/pool.rb, line 8 def initialize(collection, energy_calculator = nil) @collection = collection.dup @energy_calculator = energy_calculator || Annealing.configuration.total_energy_calculator end
Public Instance Methods
better_than?(pool)
click to toggle source
# File lib/annealing/pool.rb, line 17 def better_than?(pool) energy < pool.energy end
energy()
click to toggle source
# File lib/annealing/pool.rb, line 13 def energy @energy ||= energy_calculator.call(collection) end
next()
click to toggle source
# File lib/annealing/pool.rb, line 35 def next Pool.new(swap_collection, energy_calculator) end
solution_at(temperature)
click to toggle source
# File lib/annealing/pool.rb, line 21 def solution_at(temperature) move = self.next energy_delta = energy - move.energy if energy_delta.positive? || (Math::E**(energy_delta / temperature)) > rand move else self end end
to_s()
click to toggle source
# File lib/annealing/pool.rb, line 31 def to_s format('%<energy>.4f:%<value>s', energy: energy, value: collection) end
Private Instance Methods
size()
click to toggle source
# File lib/annealing/pool.rb, line 51 def size collection.size end
swap_collection()
click to toggle source
# File lib/annealing/pool.rb, line 43 def swap_collection swapped = collection.dup idx_a = rand(size) idx_b = rand(size) swapped[idx_b], swapped[idx_a] = swapped[idx_a], swapped[idx_b] swapped end