Model optimized to 0 & all constraints removed by presolve
回答済みI am very new to Gurobi/Python and I've run into a problem. I have written the following code, but for some reason the model optimizes to 0, because apparently all constraints are removed by presolve because they are redundant. I can't seem to figure out why my constraints are redundant. Can somebody help me out?
import gurobipy as gp
from gurobipy import GRB
model = gp.Model("ObjectiveFunction")
# Parameters
num_regions = 2
num_age_groups = 2
num_time_periods = 3
num_control_measures = 2
days_per_period = 28
eff = 0.78
death_rate = {
0: 0.005,
1: 0.01
}
infection_rate = {
0: 0.1,
1: 0.05
}
susceptibility_rate = {
0: 0.01,
1: 0.02
}
population = {
0: {0:3, 1:4},
1: {0:5, 1:2}
}
total_population = {
0 : 8,
1 : 6
}
social_contact = {
0 : {0:18, 1:5},
1 : {0:10, 1:2}
}
a = {
1: 2,
2: 6
}
Q = {
0: {1: 2, 2:1},
1: {1: 0, 2:6}
}
y = [[0, 0],
[0, 1],
[0, 1]]
# Variables
V = {}
for r in range(num_regions):
for i in range(num_age_groups):
for t in range(1, num_time_periods):
V[i, r, t] = model.addVar(vtype=GRB.CONTINUOUS, lb=0, name=f"V_{i}_{r}_{t}")
C = {}
for i in range(num_age_groups):
for r in range(num_regions):
for t in range(num_time_periods):
C[i, r, 'u', t] = model.addVar(vtype=GRB.CONTINUOUS, lb=0, name=f"C_{i}_{r}_u_{t}")
C[i, r, 'v1', t] = model.addVar(vtype=GRB.CONTINUOUS, lb=0,name=f"C_{i}_{r}_v1_{t}")
# Objective function
objective = gp.LinExpr()
for r in range(num_regions):
for i in range(num_age_groups):
for t in range(num_time_periods):
objective += (
infection_rate[i] * susceptibility_rate[i] * death_rate[i] / total_population[r] *
gp.quicksum(
social_contact[i][c] * days_per_period * (C[i, r, 'u', t]*y[t][c] + (1 - eff)*C[i, r, 'v1', t]*y[t][c]) for c in range(num_control_measures))
)
model.setObjective(objective, GRB.MINIMIZE)
# Constraint 1 - total vaccines distributed at time t not more than availability at time t
for t in range(1, num_time_periods):
model.addConstr(gp.quicksum(V[i, r, t] for i in range(num_age_groups) for r in range(num_regions)) <= a[t], name=f"c1_t{t}")
# Constraint 2 - total vaccines distributed in region r at time t not more than capable in region r
for r in range(num_regions):
for t in range(1, num_time_periods):
model.addConstr(gp.quicksum(V[i, r, t] for i in range(num_age_groups)) <= Q[r][t], name=f"c2_r{r}_t{t}")
# Constraint 3 - unvaccinated and vaccinated people in region r not more than population in region r at every time t
for r in range(num_regions):
for t in range(num_time_periods):
for i in range(num_age_groups):
model.addConstr(gp.quicksum(C[i, r, 'u', t] + C[i, r, 'v1', t]) <= population[i][r], name=f"c3_r{r}_t{t}_i{i}")
# Constraint 4 - total vaccines distributed in region r not more than population in region r over all t
for r in range(num_regions):
model.addConstr(gp.quicksum(V[i, r, t] for i in range(num_age_groups) for t in range(1, num_time_periods)) <= gp.quicksum(population[i][r] for i in range(num_age_groups)), name=f"c4_r{r}")
# Constraint 5 - cumulative unvaccinated at time t equal to unvaccinated at time t-1 minus the newly vaccinated at time t
for t in range(1, num_time_periods):
for i in range(num_age_groups):
for r in range(num_regions):
model.addConstr(
C[i, r, 'u', t] == C[i, r, 'u', t-1] - V[i, r, t], name=f"c5_r{r}_t{t}_i{i}")
# Constraint 6 - cumulative vaccinated at time t equal to vaccinated at time t-1 plus the newly vaccinated at time t
for t in range(1, num_time_periods):
for i in range(num_age_groups):
for r in range(num_regions):
model.addConstr(
C[i, r, 'v1', t] == C[i, r, 'v1', t-1] + V[i, r, t], name=f"c6_r{r}_t{t}_i{i}")
# Set starting values
for i in range(num_age_groups):
for r in range(num_regions):
C[i, r, 'u', 0].start = population[i][r] # Set the starting value for C_{i}_{r}_u_{t}
C[i, r, 'v1', 0].start = 0
# Usage
model.optimize()
if model.status == GRB.OPTIMAL:
print("Objective value:", model.objVal)
for t in range(1, num_time_periods):
for i in range(num_age_groups):
for r in range(num_regions):
print(f"V_{i}_{r}_{t} = {V[i, r, t].X}")
print(f"C_{i}_{r}_u_{t} = {C[i, r, 'u', t].X}")
print(f"C_{i}_{r}_v1_{t} = {C[i, r, 'v1', t].X}")
-
Executing your code results in
Traceback (most recent call last):
File "test.py", line 103, in <module>
model.addConstr(gp.quicksum(C[i, r, 'u', t] + C[i, r, 'v1', t]) <= population[i][r], name=f"c3_r{r}_t{t}_i{i}")
File "src/gurobipy/gurobi.pxi", line 3706, in gurobipy.quicksum
TypeError: 'gurobipy.LinExpr' object is not iterablewhich points to the constraint
# Constraint 3 - unvaccinated and vaccinated people in region r not more than population in region r at every time t
for r in range(num_regions):
for t in range(num_time_periods):
for i in range(num_age_groups):
model.addConstr(gp.quicksum(C[i, r, 'u', t] + C[i, r, 'v1', t]) <= population[i][r], name=f"c3_r{r}_t{t}_i{i}")There is a \(\texttt{for}\) statement missing inside of the \(\texttt{gp.quicksum}\) call.
0 -
Hi Jaromil,
Thanks for looking at my code! You’re right, I forgot to remove the quicksum. Unfortunately I still run into the same problem after I fix this error. The constraints are not considered, which makes the code return 0 as the optimal value. Do you have any idea how I could fix this? Or some tips as to how I can figure it out?
0 -
Hi Rosa,
I think the best way to find out what is wrong is to write the model to an LP file and inspect it.
# Usage
model.write("myLP.lp")
model.optimize()You can open the \(\texttt{myLP.lp}\) file in any standard text editor and analyze your model. From what I can see is that the current version of your model is a small purely continuous model with a couple of equality constraints which all can be used for substitution. Setting all variables to \(0\) is a feasible point and it's simultaneously the optimal solution because all objective coefficients are non-negative. There is no constraint which would force any of the variable to be \(>0\).
Best regards,
Jaromił0 -
Hi Jaromil,
Thanks for the tip! I figured out what the problem was. The initial value for C_i_r_u_0 was not set correctly, which ended in an optimal value of 0. I now added a constraint to ensure the correct initial value.
Kind regards,
Rosa0
サインインしてコメントを残してください。
コメント
4件のコメント