General constraint for difference between two log functions
AnsweredHi,
I have seen this post:
But I'm still not sure how to proceed. My problem is shown below. I'm not sure how to go about modeling eq. 39. Here, Z and \si are decision variables, the rest are parameters.
The linked post suggests adding a general constraint of the form
# Add piecewise-linear constraint
m.addGenConstrPWL(x, y, xs, ys, "pwl")
if I didn't have the difference in eq. 39, then it would've been straightforward. Perhaps it's obvious, but can you guide me in modeling this constraint?

-
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 Peyman,
In your case, you don't have to use the addGenConstrPWL function as you don't want to model a custom nonlinear function. For the \(\log\) function, Gurobi already has the addGenConstrLog function which you can use. You can then just use the addGenConstrLog twice to model the two \(\log\) terms in Eq. 39.
Best regards,
Jaromił0 -
Hi Jaromił,
Thank you for your help. If I may, can I ask for a short snippet of how that would work? I've spent a good chunk of time on it and am still struggling with it.
Let's say Z is an M by N variable:
z = m.addVars(M, N , name = "weights", vtype=GRB.CONTINUOUS)
As I understand it, I need to use addGenConstrLog to add a new variable y = log(z).
y = m.addVars(M, N , name = "y", vtype=GRB.CONTINUOUS)
gc1 = [m.addGenConstrLog(z[m,n], y[m,n]) for m in M for n in N]where I guess gc1 is the error code. now for the modeling the difference in logs, it should be something along the lines of
for m in M:
for n in N:
z[m,n] - z[M[0],n] = B \\some numberIs this approach correct?
Best,
Peyman0 -
Hi Peyman,
The approach is correct. Please note that
gc1 = [m.addGenConstrLog(z[m,n], y[m,n]) for m in M for n in N]
may provide an Attribute Error since your model object is called \(\texttt{m}\) and you use \(\texttt{m}\) to iterate over \(\texttt{M}\). Additionally the final constraints should read
for i in M:
for n in N:
m.addConstr(y[i,n] - y[M[0],n] == B, name="logConstr")because \(\texttt{y}\) are the actual values of the \(\log(z)\) function, see the documentation of addGenConstrLog.
Best regards,
Jaromił1 -
Thank you very much Jaromił!
Best,
Peyman0
Post is closed for comments.
Comments
5 comments