How do I switch the max function from CPLEX OPL to Gurobi?
AnsweredHi, i got this constraint implemented on CPLEX OPL and I want to switch it to Gurobi but I don´t know how to do especifically this part "max(z in 1 ..i)(x[z][j][k]+y[z][j][k])".
forall (i in N)
forall (j in M)
forall (k in 1..i)
sum(z in 1 ..i)(p[z]*(x[z][j][k]+y[z][j][k])) + (t[k]*max(z in 1 ..i)(x[z][j][k]+y[z][j][k]))<= d[i];
0
-
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,
You have to introduce an auxiliary variable for the max function and an auxiliary variable for each addition because Gurobi's max function only works with single variables. Then you can use the addGenConstrMax function. An example would be
aux = model.addVar(lb=-GRB.INFINITY, ub=GRB.INFINITY, name="auxiliary_variable")
varList = []
for i in I:
for j in J:
for k in K:
# introduce auxiliary variable for each sum x_zjk + y_zjk
# and add it the list
v = model.addVar(lb=-GRB.INFINITY, ub=GRB.INFINITY)
model.addConstr(v - x[z][j][k]+y[z][j][k] == 0)
varList.append(v)
# introduce the constraint aux = max(varList)
model.addGenConstrMax(aux, varList, name="maxconstr")You can then use the \(\texttt{aux}\) variable whenever you need to use the \(\max \{x_{zjk}+y_{zjk} | z \in Z, j \in J, k \in K\}\) term.
Best regards,
Jaromił0
Post is closed for comments.
Comments
2 comments