Skip to content

Commit

Permalink
add new multiplicity tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Kubvv committed Feb 28, 2024
1 parent 0029e88 commit 401c3c5
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 17 deletions.
28 changes: 14 additions & 14 deletions tests/test_analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ def test_profile_properties(self):
assert median_total_score(instance, card_multi_profile) == 3

def test_project_loss(self):
projects = [Project(chr(ord("a") + idx), 2) for idx in range(6)]
projects = [Project(chr(ord("a") + idx), 4) for idx in range(6)]
supporters = [[0, 1, 2, 4], [2, 3, 4], [0, 2], [0, 1], [4], [5]]
was_picked = [True, True, False, False, False, False]
voters_budget = [
Expand All @@ -280,28 +280,28 @@ def test_project_loss(self):
)
for idx in range(len(projects))
]
allocation_details = MESAllocationDetails(initial_budget_per_voter)
allocation_details = MESAllocationDetails(
initial_budget_per_voter,
[2 for _ in range(len(voters_budget[0]))],
)
allocation_details.iterations = iterations

project_losses = calculate_project_loss(allocation_details)
expected_budgets = [
frac(4, 1),
frac(2, 1),
frac(1, 2),
frac(1, 1),
frac(0, 1),
frac(1, 1),
]
expected_budgets = [8, 4, 1, 2, 0, 2]
expected_losses = [
{},
{projects[0]: frac(1, 1)},
{projects[0]: frac(1, 1), projects[1]: frac(1, 2)},
{projects[0]: frac(1, 1)},
{projects[0]: frac(1, 2), projects[1]: frac(1, 2)},
{projects[0]: 2},
{projects[0]: 2, projects[1]: 1},
{projects[0]: 2},
{projects[0]: 1, projects[1]: 1},
{},
]

for idx, project_loss in enumerate(project_losses):
assert project_loss.name == projects[idx].name
assert project_loss.supporters_budget == expected_budgets[idx]
assert project_loss.budget_lost == expected_losses[idx]

# No iterations
project_losses = calculate_project_loss(MESAllocationDetails(1, [1]))
assert project_losses == []
26 changes: 23 additions & 3 deletions tests/test_rule.py
Original file line number Diff line number Diff line change
Expand Up @@ -725,6 +725,16 @@ def mes_phragmen(instance, profile, resoluteness=True):
[frac(1, 2), frac(1, 2), frac(1, 2), frac(1, 2), frac(1, 2)],
[frac(1, 2), frac(1, 2), frac(1, 2), frac(1, 2), frac(1, 2)],
),
(
[5, 1, 2, 1, 2],
[0, 1, 2],
[0, 1, 2],
[1, 3],
[frac(1, 2), frac(1, 4), frac(1, 4), frac(1, 4), frac(1, 4)],
[frac(1, 2), frac(1, 4), frac(1, 4), frac(1, 4), frac(1, 4)],
True,
[2, 1, 2, 1, 2, 2],
),
]
)
def test_mes_analytics(
Expand All @@ -735,6 +745,8 @@ def test_mes_analytics(
picked_projects_idxs,
expected_third_voter_budget,
expected_fourth_voter_budget,
multiprofile=False,
expected_multiplicity=[1 for _ in range(10)],
):
projects = [Project(chr(ord("a") + idx), costs[idx]) for idx in range(0, 5)]
instance = Instance(projects, budget_limit=5)
Expand All @@ -752,21 +764,29 @@ def test_mes_analytics(
ApprovalBallot({projects[4]}),
]
)
if multiprofile:
profile = profile.as_multiprofile()
result = method_of_equal_shares(instance, profile, Cost_Sat, analytics=True)

assert sorted(list(result), key=lambda proj: proj.name) == [
projects[idx] for idx in picked_projects_idxs
]
assert result.details.initial_budget_per_voter == frac(1, 2)
assert result.details.voter_multiplicity == expected_multiplicity

check_voters = [2, 2, 5] if multiprofile else [3, 4, 8]
for idx, anl in enumerate(
sorted(result.details.iterations, key=lambda iter: iter.project.name)
):
assert anl.project.name == projects[idx].name
assert anl.was_picked == (idx in picked_projects_idxs)
assert anl.voters_budget[3] == expected_third_voter_budget[idx]
assert anl.voters_budget[4] == expected_fourth_voter_budget[idx]
assert anl.voters_budget[8] == frac(1, 2)
assert (
anl.voters_budget[check_voters[0]] == expected_third_voter_budget[idx]
)
assert (
anl.voters_budget[check_voters[1]] == expected_fourth_voter_budget[idx]
)
assert anl.voters_budget[check_voters[2]] == frac(1, 2)

def test_mes_analytics_irresolute(self):
projects = [Project(chr(ord("a") + idx), 3) for idx in range(0, 3)]
Expand Down

0 comments on commit 401c3c5

Please sign in to comment.