Trouble in implementing `if` conditionals in Gurobi Python Objective Function
OngoingI have an objective function that has a if
conditional in it. I am having trouble implementing it in Gurobi Python.
I want to maximize the utility using an objective function like
U =1 erf(z), if z<=0
= erf(z) otherwise
where z is an integer variable
Need urgent help. Any advice would be appreciated
Thanks,
Somreeta

Hi Somreeta,
If \(\mbox{erf}(z)\) is a linear function of \(z\) decision variable, you can define an auxiliary binary variable \(x\) where:
\[\mbox{if}~~ z \leq 0 \rightarrow x = 1\] \[\mbox{if}~~ z \geq 1 \rightarrow x = 0\]
The objective function can then be implemented as \(U = x \big(1\mbox{erf}(z)\big) + (1x) \mbox{erf}(z)\) which is quadratic.
Using contraposition, the conditional statements are equivalent to:
\[\mbox{if}~~ x \neq 1 \rightarrow z \nleq 0 \mbox{, which implies: if} ~~ x = 0 \rightarrow z \geq 1\]
\[\mbox{if}~~ x \neq 0 \rightarrow z \ngeq 1 \mbox{, which implies: if} ~~ x = 1 \rightarrow z \leq 0\]
These constraints can be implemented using the Gurobi's API for indicator constraints.
If \(\mbox{erf}(z)\) is a higher order function of \(z\), you can model it as a piecewiselinear objective. For more details, please check out the documentation on piecewiselinear objectives and the python example piecewise.py.
Best regards,
Maliheh
0 
Thank you for the help.
I have a related question. As addGenConstrPow accepts a constant exponent, it does not accept k, which is an iterator variable.
Is there an alternative way of doing it?
Following is the code snippet,
w=m.addVar(name="w")
myAuxVar= m.addVar(vtype=GRB.CONTINUOUS, name="myAuxVar")
m.addConstr(myAuxVar==Lambda * w)
powconstr1= m.addGenConstrPow(myAuxVar, v, k, "pow2", "FuncPieces=1000")where k is an iterator variable
I receive the following error message Encountered an attribute error
Thanks
Somreeta
0
Please sign in to leave a comment.
Comments
2 comments