Run perturbations in both orders
This commit is contained in:
parent
b084328d79
commit
f2e91c8d7a
@ -2,6 +2,13 @@
|
|||||||
|
|
||||||
module VNS
|
module VNS
|
||||||
class Engine
|
class Engine
|
||||||
|
class << self
|
||||||
|
def sequence(elements)
|
||||||
|
elements = elements.to_a
|
||||||
|
(elements + elements.reverse).chunk(&:itself).map(&:first)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def target_function(&function)
|
def target_function(&function)
|
||||||
@target_function = function
|
@target_function = function
|
||||||
end
|
end
|
||||||
@ -23,7 +30,7 @@ module VNS
|
|||||||
|
|
||||||
puts "Initial score: #{@best_score.to_f}"
|
puts "Initial score: #{@best_score.to_f}"
|
||||||
|
|
||||||
@perturbations.each do |perturbation|
|
self.class.sequence(@perturbations).each do |perturbation|
|
||||||
puts "Running perturbation: #{perturbation.name}"
|
puts "Running perturbation: #{perturbation.name}"
|
||||||
optimize(perturbation.new(@best_solution))
|
optimize(perturbation.new(@best_solution))
|
||||||
end
|
end
|
||||||
|
13
spec/services/vns/engine_spec.rb
Normal file
13
spec/services/vns/engine_spec.rb
Normal 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
|
Loading…
x
Reference in New Issue
Block a user