2024-10-27 21:42:45 +00:00
|
|
|
# Copyright (C) 2024 Manuel Bustillo
|
|
|
|
|
2024-12-28 18:37:47 +01:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2024-08-01 18:27:41 +00:00
|
|
|
module Tables
|
|
|
|
class Swap
|
|
|
|
private attr_reader :initial_solution
|
|
|
|
def initialize(initial_solution)
|
|
|
|
@initial_solution = initial_solution
|
|
|
|
end
|
|
|
|
|
|
|
|
def each
|
|
|
|
@initial_solution.tables.combination(2) do |table_a, table_b|
|
2024-11-10 17:30:01 +01:00
|
|
|
table_a.to_a.product(table_b.to_a).each do |(person_a, person_b)|
|
2024-08-01 21:14:28 +02:00
|
|
|
original_discomfort_a = table_a.reset
|
|
|
|
original_discomfort_b = table_b.reset
|
|
|
|
|
2024-08-01 18:27:41 +00:00
|
|
|
table_a.delete(person_a)
|
|
|
|
table_b.delete(person_b)
|
|
|
|
|
|
|
|
table_a << person_b
|
|
|
|
table_b << person_a
|
|
|
|
|
|
|
|
yield(@initial_solution)
|
|
|
|
ensure
|
|
|
|
table_a.delete(person_b)
|
|
|
|
table_b.delete(person_a)
|
|
|
|
|
|
|
|
table_a << person_a
|
|
|
|
table_b << person_b
|
2024-08-01 21:14:28 +02:00
|
|
|
|
|
|
|
table_a.discomfort = original_discomfort_a
|
|
|
|
table_b.discomfort = original_discomfort_b
|
2024-08-01 18:27:41 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|