Avoid stack too deep erros due to excessive recursion #178
@ -29,7 +29,7 @@ module VNS
|
|||||||
@best_score = @target_function.call(@best_solution)
|
@best_score = @target_function.call(@best_solution)
|
||||||
|
|
||||||
self.class.sequence(@perturbations).each do |perturbation|
|
self.class.sequence(@perturbations).each do |perturbation|
|
||||||
optimize(perturbation.new(@best_solution))
|
optimize(perturbation)
|
||||||
end
|
end
|
||||||
|
|
||||||
@best_solution
|
@best_solution
|
||||||
@ -37,15 +37,22 @@ module VNS
|
|||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def optimize(perturbation)
|
def optimize(perturbation_klass)
|
||||||
perturbation.each do |alternative_solution|
|
loop do
|
||||||
score = @target_function.call(alternative_solution)
|
optimized = false
|
||||||
next if score >= @best_score
|
|
||||||
|
|
||||||
@best_solution = alternative_solution.deep_dup
|
perturbation_klass.new(@best_solution).each do |alternative_solution|
|
||||||
@best_score = score
|
score = @target_function.call(alternative_solution)
|
||||||
|
next if score >= @best_score
|
||||||
|
|
||||||
return optimize(perturbation.class.new(@best_solution))
|
@best_solution = alternative_solution.deep_dup
|
||||||
|
@best_score = score
|
||||||
|
optimized = true
|
||||||
|
|
||||||
|
break
|
||||||
|
end
|
||||||
|
|
||||||
|
return unless optimized
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user