Initial version of VNS algorithm #8
@ -14,6 +14,7 @@ module Tables
|
|||||||
def cohesion_penalty
|
def cohesion_penalty
|
||||||
table.map { |guest| guest.affinity_group_list.first }.combination(2).sum do |a, b|
|
table.map { |guest| guest.affinity_group_list.first }.combination(2).sum do |a, b|
|
||||||
distance = AffinityGroupsHierarchy.instance.distance(a, b)
|
distance = AffinityGroupsHierarchy.instance.distance(a, b)
|
||||||
|
|
||||||
next 1 if distance.nil?
|
next 1 if distance.nil?
|
||||||
next 0 if distance.zero?
|
next 0 if distance.zero?
|
||||||
|
|
||||||
|
@ -4,44 +4,67 @@ module Tables
|
|||||||
let(:calculator) { described_class.new(table) }
|
let(:calculator) { described_class.new(table) }
|
||||||
|
|
||||||
describe '#cohesion_penalty' do
|
describe '#cohesion_penalty' do
|
||||||
|
before do
|
||||||
|
# Overridden in each test except trivial cases
|
||||||
|
allow(AffinityGroupsHierarchy.instance).to receive(:distance).and_call_original
|
||||||
|
|
||||||
|
%w[family friends work school].each do |group|
|
||||||
|
allow(AffinityGroupsHierarchy.instance).to receive(:distance).with(group, group).and_return(0)
|
||||||
|
end
|
||||||
|
|
||||||
|
allow(AffinityGroupsHierarchy.instance).to receive(:distance).with('family', 'friends').and_return(nil)
|
||||||
|
allow(AffinityGroupsHierarchy.instance).to receive(:distance).with('friends', 'work').and_return(1)
|
||||||
|
allow(AffinityGroupsHierarchy.instance).to receive(:distance).with('family', 'work').and_return(2)
|
||||||
|
allow(AffinityGroupsHierarchy.instance).to receive(:distance).with('family', 'school').and_return(3)
|
||||||
|
allow(AffinityGroupsHierarchy.instance).to receive(:distance).with('friends', 'school').and_return(4)
|
||||||
|
allow(AffinityGroupsHierarchy.instance).to receive(:distance).with('work', 'school').and_return(5)
|
||||||
|
end
|
||||||
context 'when the table contains just two guests' do
|
context 'when the table contains just two guests' do
|
||||||
let(:table) do
|
|
||||||
[
|
|
||||||
create(:guest, affinity_group_list: ['family']),
|
|
||||||
create(:guest, affinity_group_list: ['friends'])
|
|
||||||
]
|
|
||||||
end
|
|
||||||
|
|
||||||
before do
|
|
||||||
allow(AffinityGroupsHierarchy.instance).to receive(:distance).and_return(distance)
|
|
||||||
end
|
|
||||||
|
|
||||||
context 'when they belong to the same group' do
|
context 'when they belong to the same group' do
|
||||||
let(:distance) { 0 }
|
let(:table) { create_list(:guest, 2, affinity_group_list: ['family']) }
|
||||||
|
|
||||||
it { expect(calculator.send(:cohesion_penalty)).to eq(0) }
|
it { expect(calculator.send(:cohesion_penalty)).to eq(0) }
|
||||||
end
|
end
|
||||||
|
|
||||||
context 'when they belong to completely unrelated groups' do
|
context 'when they belong to completely unrelated groups' do
|
||||||
let(:distance) { nil }
|
let(:table) do
|
||||||
|
[
|
||||||
|
create(:guest, affinity_group_list: ['family']),
|
||||||
|
create(:guest, affinity_group_list: ['friends'])
|
||||||
|
]
|
||||||
|
end
|
||||||
it { expect(calculator.send(:cohesion_penalty)).to eq(1) }
|
it { expect(calculator.send(:cohesion_penalty)).to eq(1) }
|
||||||
end
|
end
|
||||||
|
|
||||||
context 'when they belong to groups at a distance of 1' do
|
context 'when they belong to groups at a distance of 1' do
|
||||||
let(:distance) { 1 }
|
let(:table) do
|
||||||
|
[
|
||||||
|
create(:guest, affinity_group_list: ['friends']),
|
||||||
|
create(:guest, affinity_group_list: ['work'])
|
||||||
|
]
|
||||||
|
end
|
||||||
|
|
||||||
it { expect(calculator.send(:cohesion_penalty)).to eq(0.5) }
|
it { expect(calculator.send(:cohesion_penalty)).to eq(0.5) }
|
||||||
end
|
end
|
||||||
|
|
||||||
context 'when they belong to groups at a distance of 2' do
|
context 'when they belong to groups at a distance of 2' do
|
||||||
let(:distance) { 2 }
|
let(:table) do
|
||||||
|
[
|
||||||
|
create(:guest, affinity_group_list: ['family']),
|
||||||
|
create(:guest, affinity_group_list: ['work'])
|
||||||
|
]
|
||||||
|
end
|
||||||
|
|
||||||
it { expect(calculator.send(:cohesion_penalty)).to eq(Rational(2, 3)) }
|
it { expect(calculator.send(:cohesion_penalty)).to eq(Rational(2, 3)) }
|
||||||
end
|
end
|
||||||
|
|
||||||
context 'when they belong to groups at a distance of 3' do
|
context 'when they belong to groups at a distance of 3' do
|
||||||
let(:distance) { 3 }
|
let(:table) do
|
||||||
|
[
|
||||||
|
create(:guest, affinity_group_list: ['family']),
|
||||||
|
create(:guest, affinity_group_list: ['school'])
|
||||||
|
]
|
||||||
|
end
|
||||||
|
|
||||||
it { expect(calculator.send(:cohesion_penalty)).to eq(Rational(3, 4)) }
|
it { expect(calculator.send(:cohesion_penalty)).to eq(Rational(3, 4)) }
|
||||||
end
|
end
|
||||||
@ -56,18 +79,12 @@ module Tables
|
|||||||
]
|
]
|
||||||
end
|
end
|
||||||
|
|
||||||
before do
|
|
||||||
allow(AffinityGroupsHierarchy.instance).to receive(:distance).with('family', 'friends').and_return(nil)
|
|
||||||
allow(AffinityGroupsHierarchy.instance).to receive(:distance).with('friends', 'work').and_return(1)
|
|
||||||
allow(AffinityGroupsHierarchy.instance).to receive(:distance).with('family', 'work').and_return(2)
|
|
||||||
end
|
|
||||||
|
|
||||||
it 'returns the sum of the penalties for each pair of guests' do
|
it 'returns the sum of the penalties for each pair of guests' do
|
||||||
expect(calculator.send(:cohesion_penalty)).to eq(1 + Rational(1, 2) + Rational(2, 3))
|
expect(calculator.send(:cohesion_penalty)).to eq(1 + Rational(1, 2) + Rational(2, 3))
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
context 'when the table contains four guests' do
|
context 'when the table contains four guests of different groups' do
|
||||||
let(:table) do
|
let(:table) do
|
||||||
[
|
[
|
||||||
create(:guest, affinity_group_list: ['family']),
|
create(:guest, affinity_group_list: ['family']),
|
||||||
@ -77,20 +94,26 @@ module Tables
|
|||||||
]
|
]
|
||||||
end
|
end
|
||||||
|
|
||||||
before do
|
|
||||||
allow(AffinityGroupsHierarchy.instance).to receive(:distance).with('family', 'friends').and_return(nil)
|
|
||||||
allow(AffinityGroupsHierarchy.instance).to receive(:distance).with('friends', 'work').and_return(1)
|
|
||||||
allow(AffinityGroupsHierarchy.instance).to receive(:distance).with('family', 'work').and_return(2)
|
|
||||||
allow(AffinityGroupsHierarchy.instance).to receive(:distance).with('family', 'school').and_return(3)
|
|
||||||
allow(AffinityGroupsHierarchy.instance).to receive(:distance).with('friends', 'school').and_return(4)
|
|
||||||
allow(AffinityGroupsHierarchy.instance).to receive(:distance).with('work', 'school').and_return(5)
|
|
||||||
end
|
|
||||||
|
|
||||||
it 'returns the sum of the penalties for each pair of guests' do
|
it 'returns the sum of the penalties for each pair of guests' do
|
||||||
expect(calculator.send(:cohesion_penalty))
|
expect(calculator.send(:cohesion_penalty))
|
||||||
.to eq(1 + Rational(1, 2) + Rational(2, 3) + Rational(3, 4) + Rational(4, 5) + Rational(5, 6))
|
.to eq(1 + Rational(1, 2) + Rational(2, 3) + Rational(3, 4) + Rational(4, 5) + Rational(5, 6))
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
context 'when the table contains four guests of two split groups' do
|
||||||
|
let(:table) do
|
||||||
|
[
|
||||||
|
create(:guest, affinity_group_list: ['family']),
|
||||||
|
create(:guest, affinity_group_list: ['family']),
|
||||||
|
create(:guest, affinity_group_list: ['friends']),
|
||||||
|
create(:guest, affinity_group_list: ['friends'])
|
||||||
|
]
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'returns the sum of the penalties for each pair of guests' do
|
||||||
|
expect(calculator.send(:cohesion_penalty)).to eq(4)
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user