How to write max function in models
AnsweredI have tried Model.addGenConstrMin() and max_ . But couldn't able to run the model.
what i want is to write following line of Python-Cplex into Python-Gurobi
-
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 Ankit,
The max function works a bit different in Gurobi. It is used to define a max constraint
\[ z = \max\{x_1,x_2,1,2\},\]
where \(z,x_1,x_2\) are optimization variables and \(1,2\) constant values. The term \(\texttt{ct[nStage,j]-D[j]}\) describes a linear expression and not an optimization variable. Thus, in order to model the objective function, you have to introduce 2 additional auxiliary variables for each \(j\). Then, your code might look something like
import gurobipy as gp
from gurobipy import GRB
m = gp.Model('test')
# some arbitrarily picked values
job = [1,2,3,4,5]
nStage = 3
ct = m.addVars(4, job, vtype=GRB.CONTINUOUS, name="ct")
D = m.addVars(job, vtype=GRB.CONTINUOUS, name="D")
# define auxiliary variables
aux1 = m.addVars(job, vtype=GRB.CONTINUOUS, name="aux1")
aux2 = m.addVars(job, vtype=GRB.CONTINUOUS, name="aux2")
# add constraints defining ct_j - D_j = aux1_j
m.addConstrs( (ct[nStage,j]-D[j] - aux1[j] == 0 for j in job), name="aux_constr1")
# add constraints defining aux2_j = max(aux1_j, 0)
m.addConstrs( (aux2[j] == gp.max_([aux1[j],0.0]) for j in job), name="aux_constr2")
# set objective to be the sum of all aux2 variables
m.setObjective( gp.quicksum(aux2[j] for j in job))Best regards,
Jaromił2 -
Thanks for the reply. It worked.
I am facing the same problem with one constraint.
mdl.add_constraints(ct[g,j1])>= mdl.max(ct[g-1,j1], ct[g,j]) + PT[g-1][j1-1] for f in factory for j in job for j1 in job if j1!=j for g in stage if g>1 for i in machine[g-1] )where ct is the decision variable. I tried as follows but couldn't able to run itFirst approach:mdl.addConstrs(ct[g,j1] >= gp.max_(ct[g-1,j1],ct[g,j]) + PT[g-1][j1-1] for f in factory for j in job for j1 in job if j1!=j for g in stage if g>1 for i in machine[g-1] )Second approach:mdl.addConstrs((aux3[g,j] == gp.max_(ct[g-1,j1],ct[g,j]) for j in job for j1 in job if j1!=j for g in stage if g>1), name="aux_constr3")mdl.addConstrs(ct[g,j1] >= aux3[g,j] + PT[g-1][j1-1] for f in factory for j in job for j1 in job if j1!=j for g in stage if g>1 for i in machine[g-1] )0 -
Hi Ankit,
The max_() function has to be used in an equality constraint. Thus, your second approach is the way to go. Could you provide the error message and (if possible) a minimal working example? How do you define your variables \(\texttt{ct}\)?
Best regards,
Jaromił0 -
Does Gurobi linearize max_ constraints automatically? could this lead to slower computation?
0 -
Does Gurobi linearize max_ constraints automatically?
Yes.
could this lead to slower computation?
Do you mean in comparison to treating the \(\max\) function as a nondifferentiable function? It is possible that there are cases where treating the \(\max\) function without linearization via additional binary variables and auxiliary constraints might be beneficial. I would say that linearization should work very well in most of cases.
0
Post is closed for comments.
Comments
6 comments