Move VNS implementation to the project
All checks were successful
Run unit tests / unit_tests (pull_request) Successful in 3m55s
All checks were successful
Run unit tests / unit_tests (pull_request) Successful in 3m55s
This commit is contained in:
parent
3971fab196
commit
95237165e1
1
Gemfile
1
Gemfile
@ -67,5 +67,4 @@ end
|
|||||||
gem "money"
|
gem "money"
|
||||||
gem 'acts-as-taggable-on'
|
gem 'acts-as-taggable-on'
|
||||||
|
|
||||||
gem "vns", path: "../vns"
|
|
||||||
gem "rubytree"
|
gem "rubytree"
|
||||||
|
@ -1,9 +1,3 @@
|
|||||||
PATH
|
|
||||||
remote: ../vns
|
|
||||||
specs:
|
|
||||||
vns (0.4.0.pre.rc.1)
|
|
||||||
activesupport
|
|
||||||
|
|
||||||
GEM
|
GEM
|
||||||
remote: https://rubygems.org/
|
remote: https://rubygems.org/
|
||||||
specs:
|
specs:
|
||||||
@ -106,8 +100,6 @@ GEM
|
|||||||
factory_bot (~> 6.4)
|
factory_bot (~> 6.4)
|
||||||
railties (>= 5.0.0)
|
railties (>= 5.0.0)
|
||||||
faker (3.1.1)
|
faker (3.1.1)
|
||||||
faker (3.4.2)
|
|
||||||
i18n (>= 1.8.11, < 2)
|
|
||||||
globalid (1.2.1)
|
globalid (1.2.1)
|
||||||
activesupport (>= 6.1)
|
activesupport (>= 6.1)
|
||||||
i18n (1.14.5)
|
i18n (1.14.5)
|
||||||
@ -293,7 +285,6 @@ DEPENDENCIES
|
|||||||
stimulus-rails
|
stimulus-rails
|
||||||
turbo-rails
|
turbo-rails
|
||||||
tzinfo-data
|
tzinfo-data
|
||||||
vns!
|
|
||||||
web-console
|
web-console
|
||||||
|
|
||||||
RUBY VERSION
|
RUBY VERSION
|
||||||
|
48
app/services/vns/engine.rb
Normal file
48
app/services/vns/engine.rb
Normal 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
|
@ -11,6 +11,6 @@
|
|||||||
# end
|
# end
|
||||||
|
|
||||||
# These inflection rules are supported but not enabled by default:
|
# These inflection rules are supported but not enabled by default:
|
||||||
# ActiveSupport::Inflector.inflections(:en) do |inflect|
|
ActiveSupport::Inflector.inflections(:en) do |inflect|
|
||||||
# inflect.acronym "RESTful"
|
inflect.acronym 'VNS'
|
||||||
# end
|
end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user