Manuel Bustillo 7f12fcba18
All checks were successful
Run unit tests / unit_tests (pull_request) Successful in 4m35s
Implement a cache at table level to avoid duplicate evaluations
2024-08-01 21:14:28 +02:00

35 lines
860 B
Ruby

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|
table_a.product(table_b).each do |(person_a, person_b)|
original_discomfort_a = table_a.reset
original_discomfort_b = table_b.reset
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
table_a.discomfort = original_discomfort_a
table_b.discomfort = original_discomfort_b
end
end
end
end
end