Run perturbation on top of the best solution so far
This commit is contained in:
parent
b1df5d2f53
commit
4befb8505b
@ -34,30 +34,49 @@ module VNS
|
||||
|
||||
@perturbations ||= Set.new
|
||||
|
||||
@best_solution = @initial_solution
|
||||
@best_score = @target_function.call(@best_solution)
|
||||
@current_solution = @initial_solution
|
||||
@best_score = @target_function.call(@current_solution)
|
||||
|
||||
run_all_optimizations
|
||||
|
||||
best_solution = @current_solution
|
||||
|
||||
50.times do
|
||||
@current_solution = Tables::WheelSwap.new(best_solution).call
|
||||
@best_score = @target_function.call(@current_solution)
|
||||
Rails.logger.debug { "After perturbation: #{@best_score}" }
|
||||
|
||||
run_all_optimizations
|
||||
|
||||
if best_solution.discomfort > @current_solution.discomfort
|
||||
best_solution = @current_solution
|
||||
Rails.logger.debug { "Found better solution after perturbation optimization: #{@current_solution.discomfort}" }
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
best_solution
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def run_all_optimizations
|
||||
self.class.sequence(@optimizations).each do |optimization|
|
||||
optimize(optimization)
|
||||
Rails.logger.debug { "Finished optimization phase: #{optimization}" }
|
||||
end
|
||||
|
||||
Rails.logger.debug { "Finished all optimization phases" }
|
||||
|
||||
@best_solution
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def optimize(optimization_klass)
|
||||
loop do
|
||||
optimized = false
|
||||
|
||||
optimization_klass.new(@best_solution).each do |alternative_solution|
|
||||
optimization_klass.new(@current_solution).each do |alternative_solution|
|
||||
score = @target_function.call(alternative_solution)
|
||||
next if score >= @best_score
|
||||
|
||||
@best_solution = alternative_solution.deep_dup
|
||||
@current_solution = alternative_solution.deep_dup
|
||||
@best_score = score
|
||||
optimized = true
|
||||
Rails.logger.debug { "[#{optimization_klass}] Found better solution with score: #{score}" }
|
||||
|
Loading…
x
Reference in New Issue
Block a user