Logarithm with parameterized variables on Python
AnsweredHello guys, I am trying to program a constraint of y=ln(x) but x,y have parameters. Could you help me with this code by an example? Do I need Python 10 for this? I am on 9.5 currently.
Say I want to write:
model.addGenConstrLog(x[a,b],y[c,d] for a in A for b in B for c in C for d in D), what is the correct code for this? how could I put parameterized variables in the logarithmic function?
Kind regards
Iason
-
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. -
Given Var objects \(\texttt{x}\) and \(\texttt{y}\), you can add the constraint \( y = \textrm{ln}(x) \) using Model.addGenConstrLog() as follows:
x = model.addVar(name="x")
y = model.addVar(name="y")
model.addGenConstrLog(x, y)To add multiple general constraints, you can iterate over your indices and add the constraints one by one. Here is another example that models the \(n\) constraints \( y_i = \textrm{ln}(x_i) \) for \(i = 0, \ldots, n-1\):
n = 5
x = model.addVars(n, name="x")
y = model.addVars(n, name="y")
for i in range(n):
model.addGenConstrLog(x[i], y[i])The two arguments to Model.addGenConstrLog() should both be Var objects. What exactly are you trying to model, and how do you define \(\texttt{x}\) and \(\texttt{y}\)?
0 -
No I know how to define x, and y. They are multi-parameter variables and from what you say I have to put the for outside of the logarithm function. That was something I was not aware of. You have answered my question Eli.
Kind regards
Iason0
Post is closed for comments.
Comments
3 comments