diff --git a/Gemfile b/Gemfile index 478d4be..45a5913 100644 --- a/Gemfile +++ b/Gemfile @@ -67,5 +67,4 @@ end gem "money" gem 'acts-as-taggable-on' -gem "vns", path: "../vns" gem "rubytree" diff --git a/Gemfile.lock b/Gemfile.lock index a571f8a..5df9abb 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -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 diff --git a/app/services/vns/engine.rb b/app/services/vns/engine.rb new file mode 100644 index 0000000..5c260aa --- /dev/null +++ b/app/services/vns/engine.rb @@ -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 diff --git a/config/initializers/inflections.rb b/config/initializers/inflections.rb index 3860f65..157a851 100644 --- a/config/initializers/inflections.rb +++ b/config/initializers/inflections.rb @@ -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