Compare commits

..

7 Commits

Author SHA1 Message Date
c8b88ab3b1 Merge pull request 'Introduce an invitations penalty for solutions that split guests in the same invitation' (#302) from invitations-penalty into main
All checks were successful
Run unit tests / rubocop (push) Has been skipped
Run unit tests / check-licenses (push) Has been skipped
Run unit tests / copyright_notice (push) Has been skipped
Run unit tests / unit_tests (push) Successful in 58s
Run unit tests / build-static-assets (push) Successful in 8m33s
Reviewed-on: #302
2025-07-22 13:49:28 +00:00
036cc57aa2 Introduce an invitations penalty for solutions that split guests in the same invitation
All checks were successful
Run unit tests / check-licenses (pull_request) Successful in 1m8s
Run unit tests / rubocop (pull_request) Successful in 1m38s
Run unit tests / copyright_notice (pull_request) Successful in 2m23s
Run unit tests / unit_tests (pull_request) Successful in 4m39s
Run unit tests / build-static-assets (pull_request) Successful in 9m51s
2025-07-22 15:33:13 +02:00
4dfd428ce4 Merge pull request 'Remove deprecation warning by renaming swagger_endpoint -> openapi_endpoint' (#301) from deprecation-warning-swagger into main
All checks were successful
Run unit tests / rubocop (push) Has been skipped
Run unit tests / check-licenses (push) Has been skipped
Run unit tests / copyright_notice (push) Has been skipped
Run unit tests / unit_tests (push) Successful in 1m32s
Run unit tests / build-static-assets (push) Successful in 9m36s
Reviewed-on: #301
2025-07-22 08:09:50 +00:00
51922b4f15 Merge pull request 'Introduce specs for the method Tables::DiscomfortCalculator#cohesion_penalty' (#300) from specs-cohesion_penalty into main
Some checks failed
Run unit tests / rubocop (push) Has been skipped
Run unit tests / check-licenses (push) Has been skipped
Run unit tests / copyright_notice (push) Has been skipped
Run unit tests / unit_tests (push) Successful in 1m43s
Run unit tests / build-static-assets (push) Has been cancelled
Reviewed-on: #300
2025-07-22 07:53:06 +00:00
1e3a49adb8 Remove deprecation warning by renaming swagger_endpoint -> openapi_endpoint
All checks were successful
Run unit tests / rubocop (pull_request) Successful in 48s
Run unit tests / copyright_notice (pull_request) Successful in 1m25s
Run unit tests / check-licenses (pull_request) Successful in 1m30s
Run unit tests / unit_tests (pull_request) Successful in 3m34s
Run unit tests / build-static-assets (pull_request) Successful in 16m19s
2025-07-22 09:49:33 +02:00
7596032284 Introduce specs for the method Tables::DiscomfortCalculator#cohesion_penalty
All checks were successful
Run unit tests / copyright_notice (pull_request) Successful in 50s
Run unit tests / rubocop (pull_request) Successful in 1m24s
Run unit tests / unit_tests (pull_request) Successful in 1m57s
Run unit tests / check-licenses (pull_request) Successful in 49s
Run unit tests / build-static-assets (pull_request) Successful in 10m35s
2025-07-22 09:40:05 +02:00
5dce77c29d Merge pull request 'Define a benchmark to measure the VNS performance and prevent redundant hierarchy calculations' (#298) from benchmark into main
All checks were successful
Run unit tests / rubocop (push) Has been skipped
Run unit tests / copyright_notice (push) Has been skipped
Run unit tests / check-licenses (push) Has been skipped
Run unit tests / unit_tests (push) Successful in 54s
Run unit tests / build-static-assets (push) Successful in 9m27s
Reviewed-on: #298
2025-07-18 14:26:10 +00:00
4 changed files with 117 additions and 5 deletions

View File

@ -16,6 +16,7 @@ class AffinityGroupsHierarchy < Array
end end
discomforts discomforts
invitation_counts
freeze freeze
end end
@ -54,8 +55,16 @@ class AffinityGroupsHierarchy < Array
Rational(dist, dist + 1) Rational(dist, dist + 1)
end end
def guest_count(invitation_id)
@invitation_counts[invitation_id] || 0
end
private private
def invitation_counts
@invitation_counts = Guest.where.not(invitation_id: nil).group(:invitation_id).count
end
def discomforts def discomforts
@discomforts ||= GroupAffinity.pluck(:group_a_id, :group_b_id, @discomforts ||= GroupAffinity.pluck(:group_a_id, :group_b_id,
:discomfort).each_with_object({}) do |(id_a, id_b, discomfort), acc| :discomfort).each_with_object({}) do |(id_a, id_b, discomfort), acc|

View File

@ -15,7 +15,7 @@ module Tables
end end
def breakdown def breakdown
@breakdown ||= { table_size_penalty:, cohesion_penalty: } @breakdown ||= { table_size_penalty:, cohesion_penalty:, invitations_penalty: }
end end
private private
@ -39,6 +39,12 @@ module Tables
10 * (cohesion_discomfort * 1.0 / table.size) 10 * (cohesion_discomfort * 1.0 / table.size)
end end
def invitations_penalty
2 * table.map(&:invitation_id)
.tally
.sum { |invitation_id, guests_in_table| hierarchy.guest_count(invitation_id) - guests_in_table }
end
# #
# Calculates the discomfort of the table based on the cohesion of the guests. The total discomfort # Calculates the discomfort of the table based on the cohesion of the guests. The total discomfort
# is calculated as the sum of the discomfort of each pair of guests. The discomfort of a pair of # is calculated as the sum of the discomfort of each pair of guests. The discomfort of a pair of

View File

@ -10,7 +10,7 @@ Rswag::Ui.configure do |c|
# (under openapi_root) as JSON or YAML endpoints, then the list below should # (under openapi_root) as JSON or YAML endpoints, then the list below should
# correspond to the relative paths for those endpoints. # correspond to the relative paths for those endpoints.
c.swagger_endpoint '/api/api-docs/v1/swagger.yaml', 'API V1 Docs' c.openapi_endpoint '/api/api-docs/v1/swagger.yaml', 'API V1 Docs'
# Add Basic Auth in case your API is private # Add Basic Auth in case your API is private
# c.basic_auth_enabled = true # c.basic_auth_enabled = true

View File

@ -14,14 +14,14 @@ module Tables
describe '#calculate' do describe '#calculate' do
before do before do
allow(calculator).to receive_messages(table_size_penalty: 2, cohesion_discomfort: 3) allow(calculator).to receive_messages(table_size_penalty: 2, cohesion_penalty: 5, invitations_penalty: 4)
end end
let(:table) { Table.new(create_list(:guest, 6)) } let(:table) { Table.new(create_list(:guest, 6)) }
it 'returns the sum of the table size penalty and the average cohesion penalty', :aggregate_failures do it 'returns the sum of the table size penalty and the average cohesion penalty', :aggregate_failures do
expect(calculator.calculate).to eq(7) expect(calculator.calculate).to eq(11)
expect(calculator.breakdown).to eq(table_size_penalty: 2, cohesion_penalty: 5) expect(calculator.breakdown).to eq(table_size_penalty: 2, cohesion_penalty: 5, invitations_penalty: 4)
end end
end end
@ -73,5 +73,102 @@ module Tables
it { expect(calculator.send(:table_size_penalty)).to eq(10) } it { expect(calculator.send(:table_size_penalty)).to eq(10) }
end end
end end
describe '#cohesion_penalty' do
let(:calculator) { described_class.new(table:, hierarchy:) }
let(:table) { Table.new(guests) }
context 'when all guests belong to the same group' do
let(:guests) { create_list(:guest, 6, group: family) }
let(:hierarchy) { instance_double(AffinityGroupsHierarchy, discomfort: 0) }
it 'returns 0 as cohesion penalty' do
expect(calculator.send(:cohesion_penalty)).to eq(0)
end
end
context 'when guests belong to two different groups with discomfort 1' do
let(:guests) do
create_list(:guest, 3, group: family) +
create_list(:guest, 3, group: friends)
end
let(:hierarchy) do
instance_double(AffinityGroupsHierarchy, discomfort: 1)
end
it 'calculates the correct cohesion penalty' do
# 3 from family, 3 from friends: 3*3*1 = 9 discomfort
expect(calculator.send(:cohesion_penalty)).to eq(10 * (9.0 / 6))
end
end
context 'when guests belong to three groups with different discomforts' do
let(:guests) do
create_list(:guest, 2, group: family) +
create_list(:guest, 2, group: friends) +
create_list(:guest, 2, group: work)
end
let(:hierarchy) do
instance_double(AffinityGroupsHierarchy)
end
before do
allow(hierarchy).to receive(:discomfort).with(family.id, friends.id).and_return(0.5)
allow(hierarchy).to receive(:discomfort).with(family.id, work.id).and_return(1)
allow(hierarchy).to receive(:discomfort).with(friends.id, work.id).and_return(0.2)
allow(hierarchy).to receive(:discomfort).with(friends.id, family.id).and_return(0.5)
allow(hierarchy).to receive(:discomfort).with(work.id, family.id).and_return(1)
allow(hierarchy).to receive(:discomfort).with(work.id, friends.id).and_return(0.2)
end
it 'calculates the correct cohesion penalty' do
# pairs: (family, friends): 2*2*0.5 = 2
# (family, work): 2*2*1 = 4
# (friends, work): 2*2*0.2 = 0.8
# total discomfort = 2 + 4 + 0.8 = 6.8
expect(calculator.send(:cohesion_penalty)).to eq(10 * (6.8 / 6))
end
end
end
describe '#invitations_penalty' do
let(:invitation_a) { create(:invitation) }
let(:invitation_b) { create(:invitation) }
let(:invitation_c) { create(:invitation) }
let(:table) do
create_list(:guest, 2, invitation: invitation_a) +
create_list(:guest, 3, invitation: invitation_b) +
create_list(:guest, 4, invitation: invitation_c)
end
context 'when the table contains all members of an invitation' do
it 'returns 0 as penalty' do
expect(calculator.send(:invitations_penalty)).to eq(0)
end
end
context 'when there is an additional guest of one of the invitations that is not included' do
before do
create(:guest, invitation: invitation_a)
end
it 'returns the penalty for the missing guest' do
expect(calculator.send(:invitations_penalty)).to eq(2)
end
end
context 'when there are multiple guests missing from different invitations' do
before do
create(:guest, invitation: invitation_b)
create(:guest, invitation: invitation_c)
end
it 'returns 2x # of guests left out as the total penalty for all missing guests' do
expect(calculator.send(:invitations_penalty)).to eq(4)
end
end
end
end end
end end