Compare commits

...

2 Commits

Author SHA1 Message Date
ee1fc6c8fd WIP specs
Some checks failed
Check usage of free licenses / build-static-assets (pull_request) Successful in 45s
Add copyright notice / copyright_notice (pull_request) Failing after 1m13s
Run unit tests / unit_tests (pull_request) Failing after 1m42s
2024-11-10 13:56:54 +01:00
c7117a5b92 Implement shift perturbation 2024-11-10 12:35:45 +01:00
3 changed files with 64 additions and 0 deletions

View File

@ -7,6 +7,7 @@ class TableSimulatorJob < ApplicationJob
engine = VNS::Engine.new
engine.add_perturbation(Tables::Swap)
engine.add_perturbation(Tables::Shift)
initial_solution = Tables::Distribution.new(min_per_table: 8, max_per_table: 10)
initial_solution.random_distribution(Guest.potential.shuffle)

View File

@ -0,0 +1,30 @@
# Copyright (C) 2024 Manuel Bustillo
module Tables
class Shift
private attr_reader :initial_solution
def initialize(initial_solution)
@initial_solution = initial_solution
end
def each
@initial_solution.tables.permutation(2) do |table_a, table_b|
table_a.each do |person|
original_discomfort_a = table_a.reset
original_discomfort_b = table_b.reset
table_a.delete(person)
table_b << person
yield(@initial_solution)
ensure
table_b.delete(person)
table_a << person
table_a.discomfort = original_discomfort_a
table_b.discomfort = original_discomfort_b
end
end
end
end
end

View File

@ -0,0 +1,33 @@
require 'rails_helper'
module Tables
RSpec.describe Shift do
describe '#each' do
let(:shifts) do
acc = []
described_class.new(initial_solution).each do |solution|
acc << solution.tables.map(&:dup)
end
acc
end
context 'when there are two tables with two people each' do
let(:initial_solution) do
Distribution.new(min_per_table: 2, max_per_table: 2).tap do |distribution|
distribution.tables << %i[a b].to_table
distribution.tables << %i[c d].to_table
end
end
it 'yields all possible shifts between the tables' do
expect(shifts).to contain_exactly(
[[:b], %i[c d a]],
[[:a], %i[c d b]],
[%i[b a d], [:c]],
[%i[b a c], [:d]]
)
end
end
end
end
end