From c7117a5b920a9741eb0b1e0eb96d47ba85efe15e Mon Sep 17 00:00:00 2001 From: Manuel Bustillo Date: Sun, 10 Nov 2024 12:35:41 +0100 Subject: [PATCH] Implement shift perturbation --- app/jobs/table_simulator_job.rb | 1 + app/services/tables/shift.rb | 30 ++++++++++++++++++++++++++++++ spec/services/tables/shift_spec.rb | 6 ++++++ 3 files changed, 37 insertions(+) create mode 100644 app/services/tables/shift.rb create mode 100644 spec/services/tables/shift_spec.rb diff --git a/app/jobs/table_simulator_job.rb b/app/jobs/table_simulator_job.rb index 0b90396..ef91719 100644 --- a/app/jobs/table_simulator_job.rb +++ b/app/jobs/table_simulator_job.rb @@ -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) diff --git a/app/services/tables/shift.rb b/app/services/tables/shift.rb new file mode 100644 index 0000000..8bb962c --- /dev/null +++ b/app/services/tables/shift.rb @@ -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 diff --git a/spec/services/tables/shift_spec.rb b/spec/services/tables/shift_spec.rb new file mode 100644 index 0000000..0526884 --- /dev/null +++ b/spec/services/tables/shift_spec.rb @@ -0,0 +1,6 @@ +require 'rails_helper' + +module Tables + RSpec.describe Shift do + end +end