projects.head(3) # w_i
resource | |
---|---|
project | |
p0 | 1.1 |
p1 | 1.4 |
p2 | 1.2 |
teams.head(3) # c_j
capacity | |
---|---|
team | |
t0 | 2.4 |
t1 | 1.8 |
t2 | 1.1 |
# gurobipy-pandas creates variables as a series
model = gp.Model()
model.ModelSense = GRB.MAXIMIZE
assignments = project_values.gppd.add_vars(
model, vtype=GRB.BINARY, obj="profit", name="x"
)
assignments.head() # p_ij & x_ij
profit | x | ||
---|---|---|---|
project | team | ||
p0 | t4 | 0.4 | <gurobi.Var x[p0,t4]> |
p1 | t4 | 1.3 | <gurobi.Var x[p1,t4]> |
p2 | t0 | 1.7 | <gurobi.Var x[p2,t0]> |
t1 | 1.7 | <gurobi.Var x[p2,t1]> | |
t2 | 1.7 | <gurobi.Var x[p2,t2]> |
# gurobipy-pandas adds one constraint per grouped constraint
capacity_constraints = gppd.add_constrs(
model,
(projects["resource"] * assignments["x"]).groupby("team").sum(),
GRB.LESS_EQUAL,
teams["capacity"],
name='capacity',
)
capacity_constraints.apply(model.getRow).head()
team t0 1.2 x[p2,t0] + 0.9 x[p4,t0] + 1.3 x[p5,t0] + x... t1 1.2 x[p2,t1] + 0.9 x[p4,t1] + 1.3 x[p5,t1] + x... t2 1.2 x[p2,t2] + 0.9 x[p4,t2] + 1.3 x[p5,t2] + x... t3 1.2 x[p2,t3] + 0.9 x[p4,t3] + 1.3 x[p5,t3] + x... t4 1.1 x[p0,t4] + 1.4 x[p1,t4] + 1.2 x[p2,t4] + 1... Name: capacity, dtype: object
model.optimize()
(
assignments["x"].gppd.X.to_frame()
.query("x >= 0.9").reset_index()
.groupby("team").agg({"project": list})
)
project | |
---|---|
team | |
t0 | [p4, p5] |
t1 | [p2] |
t2 | [p11] |
t3 | [p6, p29] |
t4 | [p14, p15, p26] |