Skip to content

Commit 0029e88

Browse files
committed
add multiplicity to project loss calculation
1 parent f08e6e0 commit 0029e88

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

pabutools/analysis/projectloss.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,12 +80,13 @@ def calculate_project_loss(
8080
List of :py:class:`~pabutools.analysis.projectloss.ProjectLoss` objects.
8181
8282
"""
83-
if not hasattr(allocation_details, "iterations") or not hasattr(
84-
allocation_details, "initial_budget_per_voter"
83+
if not all(
84+
hasattr(allocation_details, attr)
85+
for attr in ["iterations", "initial_budget_per_voter", "voter_multiplicity"]
8586
):
8687
raise ValueError(
8788
"Provided budget allocation details do not support calculating project loss. The allocation_details "
88-
"should have an 'iterations' and an 'initial_budget_per_voter' attributes."
89+
"should have an 'iterations', 'initial_budget_per_voter' and 'voter_multiplicity' attributes."
8990
)
9091
if len(allocation_details.iterations) == 0:
9192
if verbose:
@@ -94,6 +95,7 @@ def calculate_project_loss(
9495

9596
project_losses = []
9697
voter_count = len(allocation_details.iterations[0].voters_budget)
98+
voter_multiplicity = allocation_details.voter_multiplicity
9799
voter_spendings: dict[int, list[Tuple[Project, Numeric]]] = {}
98100
for idx in range(voter_count):
99101
voter_spendings[idx] = []
@@ -108,15 +110,21 @@ def calculate_project_loss(
108110
f"Considering: {iteration.project.name}, status: {iteration.was_picked}"
109111
)
110112
budget_lost = {}
111-
for spending in [voter_spendings[i] for i in iteration.supporter_indices]:
113+
for idx in iteration.supporter_indices:
114+
spending = voter_spendings[idx]
112115
for project, spent in spending:
113116
if project not in budget_lost.keys():
114117
budget_lost[project] = 0
115-
budget_lost[project] = budget_lost[project] + spent
118+
budget_lost[project] = (
119+
budget_lost[project] + spent * voter_multiplicity[idx]
120+
)
116121
project_losses.append(
117122
ProjectLoss(
118123
iteration.project,
119-
sum(current_voters_budget[i] for i in iteration.supporter_indices),
124+
sum(
125+
current_voters_budget[i] * voter_multiplicity[i]
126+
for i in iteration.supporter_indices
127+
),
120128
budget_lost,
121129
)
122130
)

0 commit comments

Comments
 (0)