From f2e91c8d7acb8af31fddad5b990fd9c200692066 Mon Sep 17 00:00:00 2001 From: Manuel Bustillo Date: Sun, 10 Nov 2024 18:55:04 +0100 Subject: [PATCH 1/2] Run perturbations in both orders --- app/services/vns/engine.rb | 11 +++++++++-- spec/services/vns/engine_spec.rb | 13 +++++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) create mode 100644 spec/services/vns/engine_spec.rb diff --git a/app/services/vns/engine.rb b/app/services/vns/engine.rb index 00f2c80..65ceb71 100644 --- a/app/services/vns/engine.rb +++ b/app/services/vns/engine.rb @@ -2,6 +2,13 @@ module VNS class Engine + class << self + def sequence(elements) + elements = elements.to_a + (elements + elements.reverse).chunk(&:itself).map(&:first) + end + end + def target_function(&function) @target_function = function end @@ -23,12 +30,12 @@ module VNS puts "Initial score: #{@best_score.to_f}" - @perturbations.each do |perturbation| + self.class.sequence(@perturbations).each do |perturbation| puts "Running perturbation: #{perturbation.name}" optimize(perturbation.new(@best_solution)) end - @best_solution + @best_solution end private diff --git a/spec/services/vns/engine_spec.rb b/spec/services/vns/engine_spec.rb new file mode 100644 index 0000000..974f342 --- /dev/null +++ b/spec/services/vns/engine_spec.rb @@ -0,0 +1,13 @@ +require 'rails_helper' + +module VNS + RSpec.describe Engine do + describe '.sequence' do + it { expect(described_class.sequence([])).to eq([]) } + it { expect(described_class.sequence([1])).to eq([1]) } + it { expect(described_class.sequence([1, 2])).to eq([1, 2, 1]) } + it { expect(described_class.sequence([1, 2, 3])).to eq([1, 2, 3, 2, 1]) } + it { expect(described_class.sequence([1, 2, 3, 4])).to eq([1, 2, 3, 4, 3, 2, 1]) } + end + end +end -- 2.47.1 From fe91f75ec077b1369c8f67fcf6bf09840612c7ef Mon Sep 17 00:00:00 2001 From: Manuel Bustillo Date: Sun, 10 Nov 2024 17:57:29 +0000 Subject: [PATCH 2/2] Add copyright notice --- spec/services/vns/engine_spec.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/spec/services/vns/engine_spec.rb b/spec/services/vns/engine_spec.rb index 974f342..d4064f6 100644 --- a/spec/services/vns/engine_spec.rb +++ b/spec/services/vns/engine_spec.rb @@ -1,3 +1,5 @@ +# Copyright (C) 2024 Manuel Bustillo + require 'rails_helper' module VNS -- 2.47.1