Move VNS implementation to the project
All checks were successful
Run unit tests / unit_tests (pull_request) Successful in 3m55s

This commit is contained in:
Manuel Bustillo 2024-08-01 20:22:51 +02:00
parent 3971fab196
commit 95237165e1
4 changed files with 51 additions and 13 deletions

View File

@ -67,5 +67,4 @@ end
gem "money"
gem 'acts-as-taggable-on'
gem "vns", path: "../vns"
gem "rubytree"

View File

@ -1,9 +1,3 @@
PATH
remote: ../vns
specs:
vns (0.4.0.pre.rc.1)
activesupport
GEM
remote: https://rubygems.org/
specs:
@ -106,8 +100,6 @@ GEM
factory_bot (~> 6.4)
railties (>= 5.0.0)
faker (3.1.1)
faker (3.4.2)
i18n (>= 1.8.11, < 2)
globalid (1.2.1)
activesupport (>= 6.1)
i18n (1.14.5)
@ -293,7 +285,6 @@ DEPENDENCIES
stimulus-rails
turbo-rails
tzinfo-data
vns!
web-console
RUBY VERSION

View File

@ -0,0 +1,48 @@
module VNS
class Engine
def target_function(&function)
@target_function = function
end
def add_perturbation(klass)
@perturbations ||= Set.new
@perturbations << klass
end
attr_writer :initial_solution
def run
raise 'No target function defined' unless @target_function
raise 'No perturbations defined' unless @perturbations
raise 'No initial solution defined' unless @initial_solution
@best_solution = @initial_solution
@best_score = @target_function.call(@best_solution)
puts "Initial score: #{@best_score.to_f}"
@perturbations.each do |perturbation|
puts "Running perturbation: #{perturbation.name}"
optimize(perturbation.new(@best_solution))
end
@best_solution
end
private
def optimize(perturbation)
perturbation.each do |alternative_solution|
score = @target_function.call(alternative_solution)
next if score >= @best_score
@best_solution = alternative_solution.deep_dup
@best_score = score
puts "New lowest score: #{@best_score.to_f}"
return optimize(perturbation.class.new(@best_solution))
end
end
end
end

View File

@ -11,6 +11,6 @@
# end
# These inflection rules are supported but not enabled by default:
# ActiveSupport::Inflector.inflections(:en) do |inflect|
# inflect.acronym "RESTful"
# end
ActiveSupport::Inflector.inflections(:en) do |inflect|
inflect.acronym 'VNS'
end