Error while setting up MILP constraints
AnsweredHello all,
I have difficulties to implement constraints for my MILP. The aim is to build a MILP, which schedules jobs on parallel machines m, while having a recipe r. I have the following model notation.
The model formulation is as following:
I defined the data and their ranges and added the decision variables.
X = mo.addVars(numberofJobs, numberofMachines, numberofPositions, vtype =GRB.BINARY, name="X")
Y = mo.addVars(numberofMachines, numberofPositions, numberofRecipes, vtype =GRB.BINARY, name="Y")
Z = mo.addVars(numberofMachines, numberofPositions, numberofRecipes, numberofRecipes, vtype =GRB.BINARY, name="Z")
U = mo.addVars(numberofJobs, vtype =GRB.BINARY, name="U")
S = mo.addVars(numberofMachines, numberofPositions, vtype =GRB.CONTINUOUS, name="S")
C = mo.addVars(numberofMachines, numberofPositions, vtype =GRB.CONTINUOUS, name="C")
The constraint 2.2 shows the error: IndexError: list index out of range.
for m in machines:
for p in positions:
mo.addConstr(S[m,p]+ gp.quicksum([x[j][r][m]*X[j,m,p] for j in jobs]) == C[m,p], "NB1_" + str(m)+str(p))
for m in machines:
for p in positions:
for r in recipes:
for re in recipes:
mo.addConstr(Z[m,p,r,re]>=Y[m,p-1,r]+Y[m,p,re]-1, "NB3_"+str(m)+str(p)+str(r)+str(r))
-
Official comment
This post is more than three years old. Some information may not be up to date. For current information, please check the Gurobi Documentation or Knowledge Base. If you need more help, please create a new post in the community forum, or try Gurobot, our chatbot interface offering instant, expert-level support. -
Hi Moritz,
It is not possible to reproduce your issue without a minimal reproducible example. You should try to isolate the Index and Key errors.
The IndexError means that an array access to at least one of \(\texttt{S[m,p],x[j][r][m],X[j,m,p],C[m,p]}\) was out of the respective array bounds.
The KeyError means that you tried to access an array at position \(\texttt{0,-1,0}\). Probably all your arrays start at \(\texttt{(0,0,0)}\).
Best regards,
Jaromił0 -
Hi Jaromil,
thanks for your fast response! I could fix the IndexError: and the KeyError: with your help and advises.
There is one more question I have regarding my model. I want to get the maximum value of a decision variable in my objective and in a set of constraints as shown below. I want to get the maximum value of C[m,p].
I tried the following two methods.
(1) with gurobipy.max_() lookingmo.setObjective(gp.quicksum(H * U[j] for j in jobs) + gp.max_(C[m,p] for m in machines for p in positions) , GRB.MINIMIZE)Failure code: GurobiError: Unsupported type (<class 'gurobipy.GenExprMax'>) for LinExpr addition argument
and with max()
mo.setObjective(gp.quicksum(H * U[j] for j in jobs) + max(C[m,p] for m in machines for p in positions) , GRB.MINIMIZE)
Failure Code: TypeError: '>' not supported between instances of 'Var' and 'Var'
(2) for the constraint

the code looks like:for m in machines:
mo.addConstr(gp.max_(C[m,p] for p in positions)>=C[m,p], "NB10_"+str(m))Failure code: GurobiError: General expressions can only be equal to a single var
For me the gp.max_() function makes the most sense. Maybe you can help me with this issue as well.
Best regards,
Moritz0 -
Hi Moritz,
When working with general constraints such as the max function, you have to introduce an auxiliary optimization variable to hold the value of the function. This is the \(\texttt{resvar}\) argument in the addGenConstrMax method. To make your code work, you have to model
\[\begin{align*}
z_m &= \max \{C_{m,p}| p \in P\} \quad \forall m \in M\\
w &= \max \{C_{m,p}| p \in P, m \in M\}
\end{align*}\]A possible Python code might be
z = mo.addVars(machines, lb=-GRB.INFINITY)
for m in machines:
mo.addConstr(z[m] == gp.max_(C[m,p] for p in positions))
mo.addConstr(z[m]>=C[m,p], "NB10_"+str(m))
w = mo.addVar(lb=-GRB.INFINITY)
mo.addConstr(w == gp.max_(C[m,p] for p in positions for m in machines))
# use w in your objectiveBest regards,
Jaromił1 -
Hi Jaromił,
huge thanks for your help. My MILP-model is now working and there are no error messages popping up anymore.
Best regards,
Moritz0
Post is closed for comments.
Comments
5 comments