How to write a maximize constraint using gurobipy
AnsweredHi,
I'm using gurobi on python and I'd like to solve a route problem but there are some constraints I don't know how to write as in the picture:

I have tried several ways,
1st try: model.addConstr(a[j,k] == max_((b[i,k] + T_ij[i,j] - ( 1-x[i,j,k] )*M), 0) )
2nd try: to use the indicator constraint separately like below
model.addConstr((c == 0) >> (b[i,k] + T_ij[i,j] - ( 1-x[i,j,k] )*M <= -eps) )
model.addConstr((c == 1) >> (b[i,k] + T_ij[i,j] - ( 1-x[i,j,k] )*M >= 0))
3rd try: model.addGenConstrMax( a[j,k], [0, b[i,k] + T_ij[i,j] - ( 1-x[i,j,k] )*M] ) to run the code but none of them worked.
- note that M represents BigM and eps I set for is 1e-6
- a[] and b[] are continuous variables, x[] is a binary variable, and T_ij[] is a parameter
-
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 why not try our AI Gurobot?. -
Hi Ying,
Your first try was almost successful. Please note that the max_() only accepts single Variables and constants as arguments but the term \(\texttt{(b[i,k] + T_ij[i,j] - ( 1-x[i,j,k] )*M)}\) is a LinExpr. Thus, in order to make your first approach work, you can add auxiliary variables for each of the terms as
N = [1,2,3]
V = [4,5,6]
a = m.addVars(N,V,name="a")
b = m.addVars(N,V,name="b")
T_ij = m.addVars(N,N,name="T_")
x = m.addVars(N,N,V,name="x")
aux = m.addVars(N,N,V,name="aux")
M = 10000
for i in N:
for j in N:
for k in V:
m.addConstr(aux[i,j,k] == (b[i,k] + T_ij[i,j] - ( 1-x[i,j,k] )*M))
m.addConstr(a[j,k] == max_( aux[i,j,k], 0) )The same applies to your third approach because the max_() is just a shortcut for the addGenConstrMax() function.
Your 2nd approach should work as it is, but I would recommend using one of the \(\texttt{max}\) functions.
Best regards,
Jaromił0 -
Hi Jaromił,
I see! That's why I couldn't manage it correctly.
Thank you so much for your solution and help!!
Best regards,
Ying
0
Post is closed for comments.
Comments
3 comments