Class: Lahcuby::Solver
- Inherits:
-
Object
- Object
- Lahcuby::Solver
- Defined in:
- lib/lahcuby/solver.rb
Instance Method Summary collapse
-
#initialize(initial_solution, maximum_number_of_iterations, memory_size) ⇒ Solver
constructor
A new instance of Solver.
- #solve ⇒ Object
Constructor Details
#initialize(initial_solution, maximum_number_of_iterations, memory_size) ⇒ Solver
Returns a new instance of Solver.
4 5 6 7 8 |
# File 'lib/lahcuby/solver.rb', line 4 def initialize(initial_solution, maximum_number_of_iterations, memory_size) @current_solution = initial_solution @maximum_number_of_iterations = maximum_number_of_iterations @memory_size = memory_size end |
Instance Method Details
#solve ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/lahcuby/solver.rb', line 10 def solve() costs_memory = [] @memory_size.times do |_| costs_memory.push(@current_solution.get_cost) end best_solution = @current_solution @maximum_number_of_iterations.times do |iteration| memory_index = iteration % @memory_size new_solution = @current_solution.get_variation beats_current_solution = new_solution.get_cost < @current_solution.get_cost memory_cost = costs_memory[memory_index] beats_memory_cost = new_solution.get_cost < memory_cost if beats_current_solution || beats_memory_cost @current_solution = new_solution end if beats_memory_cost costs_memory[memory_index] = new_solution.get_cost end if @current_solution.get_cost < best_solution.get_cost best_solution = @current_solution end end best_solution end |