Skip to content

Commit

Permalink
fix use of gurobipy.quicksum()
Browse files Browse the repository at this point in the history
gurobi documentation -> "Passing a tupledict to the quicksum function is deprecated as the result is inconsistent with passing a native Python dictionary to quicksum. The sum method of the tupledict should be used instead to compute the sum over the values of the tupledict. "
  • Loading branch information
martinlackner committed Jan 8, 2024
1 parent 7d233c9 commit af89c3b
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 13 deletions.
6 changes: 3 additions & 3 deletions abcvoting/abcrules_gurobi.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ def set_opt_model_func(model, in_committee):
utility[(voter, x)] = model.addVar(vtype=gb.GRB.BINARY, name=f"utility({i,x})")

# constraint: the committee has the required size
model.addConstr(gb.quicksum(in_committee) == committeesize)
model.addConstr(in_committee.sum() == committeesize)

# constraint: utilities are consistent with actual committee
for voter in profile:
Expand Down Expand Up @@ -234,7 +234,7 @@ def set_opt_model_func(model, in_committee):
utility[(voter, x)] = model.addVar(vtype=gb.GRB.BINARY, name=f"utility({i, x})")

# constraint: the committee has the required size
model.addConstr(gb.quicksum(in_committee) == committeesize)
model.addConstr(in_committee.sum() == committeesize)

# constraint: utilities are consistent with actual committee
for voter in profile:
Expand Down Expand Up @@ -663,7 +663,7 @@ def set_opt_model_func(model, in_committee):
voteratmostdistances[(i, dist)] = 0

# constraint: the committee has the required size
model.addConstr(gb.quicksum(in_committee) == committeesize)
model.addConstr(in_committee.sum() == committeesize)

# constraint: distances are consistent with actual committee
for i, voter in enumerate(profile):
Expand Down
16 changes: 8 additions & 8 deletions abcvoting/properties.py
Original file line number Diff line number Diff line change
Expand Up @@ -662,7 +662,7 @@ def _check_pareto_optimality_gurobi(profile, committee):

# constraint: the condition of having strictly more approved candidates in
# dominating committee will be satisfied for at least one voter
model.addConstr(gb.quicksum(condition_strictly_more) >= 1)
model.addConstr(condition_strictly_more.sum() >= 1)

# constraint: all voters should have at least as many preferred candidates
# in the dominating committee as in the query committee.
Expand All @@ -675,7 +675,7 @@ def _check_pareto_optimality_gurobi(profile, committee):
)

# constraint: committee has the right size
model.addConstr(gb.quicksum(in_committee) == len(committee))
model.addConstr(in_committee.sum() == len(committee))

# set the objective function
model.setObjective(
Expand Down Expand Up @@ -816,7 +816,7 @@ def _check_EJR_gurobi(profile, committee, quota):
in_cut = model.addVars(profile.num_cand, vtype=gb.GRB.BINARY, name="in_cut")

# the voters in group should agree on at least ell candidates
model.addConstr(gb.quicksum(in_cut) >= ell)
model.addConstr(in_cut.sum() >= ell)

# candidates in cut should be approved by all voters in group
for vi, voter in enumerate(profile):
Expand Down Expand Up @@ -968,7 +968,7 @@ def _check_PJR_gurobi(profile, committee, quota):
in_cut = model.addVars(profile.num_cand, vtype=gb.GRB.BINARY, name="in_cut")

# the voters in group should agree on at least ell candidates
model.addConstr(gb.quicksum(in_cut) >= ell)
model.addConstr(in_cut.sum() >= ell)

# candidates in cut should be approved by all voters in group
for vi, voter in enumerate(profile):
Expand Down Expand Up @@ -1521,10 +1521,10 @@ def _check_FJR_gurobi(profile, committee, quota):

# coalition large enough to deserve |set_of_candidates| seats
model.addConstr(
gb.quicksum(set_of_candidates) * quota
set_of_candidates.sum() * quota
<= gb.quicksum(voter.weight * set_of_voters[vi] for vi, voter in enumerate(profile))
)
model.addConstr(gb.quicksum(set_of_voters) >= 1)
model.addConstr(set_of_voters.sum() >= 1)
for i, voter in enumerate(profile):
# if i is in set_of_voters, then:
# (a) i has utility at most beta-1 in committee
Expand Down Expand Up @@ -1703,10 +1703,10 @@ def _check_core_gurobi(profile, committee, quota):

# set_of_voters is large enough to afford set_of_candidates
model.addConstr(
gb.quicksum(set_of_candidates) * quota
set_of_candidates.sum() * quota
<= gb.quicksum(voter.weight * set_of_voters[vi] for vi, voter in enumerate(profile))
)
model.addConstr(gb.quicksum(set_of_voters) >= 1)
model.addConstr(set_of_voters.sum() >= 1)

# every voter in set_of_voters prefers set_of_candidates to committee
for i, voter in enumerate(profile):
Expand Down
2 changes: 1 addition & 1 deletion examples/preflib-files/new_example.cat
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# FILE NAME: new_example.cat
# FILE NAME: /home/martin/Documents/code/pycharm-projects/abcvoting/examples/preflib-files/new_example.cat
# TITLE:
# DESCRIPTION:
# DATA TYPE:
Expand Down
2 changes: 1 addition & 1 deletion tests/data/test5.cat
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# FILE NAME: test5.cat
# FILE NAME: /home/martin/Documents/code/pycharm-projects/abcvoting/tests/data/test5.cat
# TITLE:
# DESCRIPTION:
# DATA TYPE:
Expand Down

0 comments on commit af89c3b

Please sign in to comment.