Maximum of two values
AnsweredHi.
In my model coded in Python, after getting the optimal values of some variables, I built a mathematical expression to get a number, i.e.
for i in I:
y[i] = max(0, y[i-1]+ a*z[i-1])
Here z[i-1] and and y[i-1] are variables' optimal values obtained by solving an optimization model. I'm trying to make y[i] equal to zero if the second expression within the paranthesis takes a negative value.
However, I am getting a "NotImplementedError" error code due to the "max" operation.
Could you kindly help me to solve this problem? Thank you in advance.
Sakir.
s
-
Official comment
This post is more than two 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 Sakir,
first of all, the max_() method in Guroby Python has a slightly different syntax - take a look at the documentation.
On top of this, the max_() method takes variables as arguments and not linear expressions. You might want to try something along the lines of:
aux = model.addVars(I, vtype="C", name="aux")
for i in I
model.addLConstr(aux[i] == y[i-1] + a*z[i-1])
model.addConstr(y[i] == max_(aux[i], constant=0))Hope this helps.
Best regards
Jonasz0 -
Also, the article How do I model logical and general expressions? may be of use when using max in a constraint in gurobipy.
0 -
Hi,
Thank you so much for your quick response. The equation I shared is not a constraint. After solving the constrained optimization model, I'd like to make a straightforward mathematical operation. Actually, when I tried the following, the problem is solved.
y[i] = max(0, y[i-1].X+ a*z[i-1].X)
0
Post is closed for comments.
Comments
4 comments