Run perturbations in both orders #114

Merged
bustikiller merged 2 commits from vns-combinations into main 2024-11-10 18:01:16 +00:00
2 changed files with 22 additions and 2 deletions
Showing only changes of commit f2e91c8d7a - Show all commits

View File

@ -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

View File

@ -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