Skip to content

Commit 312f856

Browse files
author
Hamish Downer
committed
Move the final few report messages into the translations list
1 parent 50af1f1 commit 312f856

File tree

5 files changed

+44
-21
lines changed

5 files changed

+44
-21
lines changed

src/sortition_algorithms/committee_generation/common.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -437,11 +437,7 @@ def _find_committees_for_uncovered_agents(
437437
for covered_agent_id in new_committee:
438438
covered_agents.add(covered_agent_id)
439439
else:
440-
# TODO: i18n - Add to report_messages.py as "agent_not_in_feasible_committee"
441-
# with agent_id parameter
442-
report.add_line_and_log(
443-
f"Agent {agent_id} not contained in any feasible committee.", log_level=logging.INFO
444-
)
440+
report.add_message_and_log("agent_not_in_feasible_committee", log_level=logging.INFO, agent_id=agent_id)
445441

446442
return new_committees, covered_agents, report
447443

src/sortition_algorithms/committee_generation/leximin.py

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,22 @@ def _dual_leximin_stage(
7474
return model, agent_vars, cap_var
7575

7676

77+
def _add_report_update(
78+
report: RunReport, dual_obj: float, value: float, upper: float, committees: set[frozenset[str]]
79+
) -> None:
80+
at_most = f"{dual_obj - upper + value:.2%}"
81+
dual_obj_str = f"{dual_obj:.2%}"
82+
gap_str = f"{value - upper:.2%}"
83+
report.add_message_and_log(
84+
"leximin_is_at_most",
85+
log_level=logging.DEBUG,
86+
at_most=at_most,
87+
dual_obj_str=dual_obj_str,
88+
num_committees=len(committees),
89+
gap_str=gap_str,
90+
)
91+
92+
7793
def _run_leximin_column_generation_loop(
7894
new_committee_model: mip.model.Model,
7995
agent_vars: dict[str, mip.entities.Var],
@@ -147,13 +163,7 @@ def _run_leximin_column_generation_loop(
147163
upper = dual_cap_var.x # ŷ
148164
dual_obj = dual_model.objVal # ŷ - Σ_{i in fixed_probabilities} fixed_probabilities[i] * yᵢ
149165

150-
# TODO: i18n - Complex message with percentage formatting
151-
# Consider adding to report_messages.py with appropriate parameters
152-
report.add_line_and_log(
153-
f"Maximin is at most {dual_obj - upper + value:.2%}, can do {dual_obj:.2%} with "
154-
f"{len(committees)} committees. Gap {value - upper:.2%}.",
155-
log_level=logging.DEBUG,
156-
)
166+
_add_report_update(report, dual_obj, value, upper, committees)
157167
if value <= upper + EPS:
158168
# Within numeric tolerance, the panels in `committees` are enough to constrain the dual, i.e., they are
159169
# enough to support an optimal primal solution.

src/sortition_algorithms/committee_generation/maximin.py

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,22 @@ def _run_maximin_heuristic_for_additional_committees(
167167
return counter
168168

169169

170+
def _add_report_update(report: RunReport, value: float, upper: float, committees: set[frozenset[str]]) -> None:
171+
"""Complex formatting, so pull out of the main flow."""
172+
at_most = f"{value:.2%}"
173+
upper_str = f"{upper:.2%}"
174+
num_committees = len(committees)
175+
gap_str = f"{value - upper:.2%}{'≤' if value - upper <= EPS else '>'}{EPS:%}"
176+
report.add_message_and_log(
177+
"maximin_is_at_most",
178+
log_level=logging.DEBUG,
179+
at_most=at_most,
180+
upper_str=upper_str,
181+
num_committees=num_committees,
182+
gap_str=gap_str,
183+
)
184+
185+
170186
def _run_maximin_optimization_loop(
171187
new_committee_model: mip.model.Model,
172188
agent_vars: dict[str, mip.entities.Var],
@@ -208,13 +224,7 @@ def _run_maximin_optimization_loop(
208224
new_set = ilp_results_to_committee(agent_vars)
209225
value = sum(entitlement_weights[agent_id] for agent_id in new_set)
210226

211-
# TODO: i18n - Complex message with percentage formatting and mathematical symbols
212-
# Consider adding to report_messages.py with appropriate parameters
213-
report.add_line_and_log(
214-
f"Maximin is at most {value:.2%}, can do {upper:.2%} with {len(committees)} "
215-
f"committees. Gap {value - upper:.2%}{'≤' if value - upper <= EPS else '>'}{EPS:%}.",
216-
log_level=logging.DEBUG,
217-
)
227+
_add_report_update(report, value, upper, committees)
218228
if value <= upper + EPS:
219229
# No feasible committee B violates Σ_{i ∈ B} y_{e(i)} ≤ z (at least up to EPS, to prevent rounding errors)
220230
# Thus, we have enough committees

src/sortition_algorithms/committee_generation/nash.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,7 @@ def _solve_nash_welfare_optimization(
115115
nash_welfare = problem.solve(solver=cp.ECOS, warm_start=True)
116116

117117
scaled_welfare = nash_welfare - len(entitlements) * log(number_people_wanted / len(entitlements))
118-
# TODO: i18n - Add to report_messages.py as "scaled_nash_welfare" with welfare parameter
119-
report.add_line_and_log(f"Scaled Nash welfare is now: {scaled_welfare}.", log_level=logging.INFO)
118+
report.add_message_and_log("scaled_nash_welfare", log_level=logging.INFO, scaled_welfare=scaled_welfare)
120119

121120
assert lambdas.value.shape == (len(committees),)
122121
entitled_utilities = matrix.dot(lambdas.value)

src/sortition_algorithms/report_messages.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,14 @@ def N_(message: str) -> str:
6767
"@paulgoelz.de with the following information: algorithm=%(algorithm)s, "
6868
"num_panels=%(num_panels)s, num_agents=%(num_agents)s, min_probs=%(min_probs)s."
6969
),
70+
"agent_not_in_feasible_committee": N_("Agent %(agent_id)s not contained in any feasible committee."),
71+
"scaled_nash_welfare": N_("Scaled Nash welfare is now: %(scaled_welfare)s."),
72+
"maximin_is_at_most": N_(
73+
"Maximin is at most %(at_most)s, can do %(upper_str)s with %(num_committees)s committees. Gap %(gap_str)s."
74+
),
75+
"leximin_is_at_most": N_(
76+
"Leximin is at most %(at_most)s, can do %(dual_obj_str)s with %(num_committees)s committees. Gap %(gap_str)s.",
77+
),
7078
# ========================================================================
7179
# Selection process messages
7280
# ========================================================================

0 commit comments

Comments
 (0)