General expression : min_ or max_
AnsweredHi,
I wanted to use min_ expression in the rhs of constraint (please see the below expression, schedule[i] is decision variable).
model.addConstr(quicksum(100*schedule[i] for i in index) <= 100 - min_(100, quicksum(100 for i in index if i.id==0)))
However I had TypeError which is unsupported operand type for -: 'int and 'GenExpr'.
Thus, I changed the above expression as below.
model.addConstr(quicksum(-100 - 100*schedule[i] for i in index) >= min_(100, quicksum(100 for i in index if i.id==0)))
Now I have GurobiError that General expressions can only be used for equality constraint.
Would you please help me how I can solve this issue?
Thanks,
-
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?. -
The min_() helper function can only be used to set one variable equal to a group of variables and/or constants. This would be used if, e.g., you want to add a constraint like \( x = \min\{y, z, 2\} \), where \(x\), \(y\), and \(z\) are variables.
In your case, the elements of \( \texttt{index} \) are not variables. Thus, the minimum of \( \texttt{100} \) and \( \texttt{sum(100 for i in index if i.id == 0)} \) is a constant value that doesn't depend on any variables. This means you can use Python's built-in \( \texttt{min()} \) function:
model.addConstr(quicksum(100*schedule[i] for i in index) <= 100 - min(100, sum(100 for i in index if i.id == 0)))
Note that I used Python's built-in \( \texttt{sum()} \) function instead of Gurobi's quicksum() function. The latter returns a LinExpr object, which won't work well with \( \texttt{min()} \).
0 -
Thank you Eli for your help!
0
Post is closed for comments.
Comments
3 comments