Python API: Using single component of a MVar object in a non-matrix constraint
AnsweredHi
I only recently started working with the Python API (Gurobi 9.1), which support matrix friendly modelling. Particularly, I have a problem that is naturally expressed in terms in matrix/vector form. say that I have a Mvar variable in the form:
\[\mathbf{x}=[x_1,x_2,...,x_N]^T\]
I would like to add a piecewise linear constraint in my problem in the form:
\[y=f(x_i)\]
where \(x_i\) is a single element of vector \(\mathbf{x}\). It seems that using x[i] as the xvar attribute of function GRBModel.addGenConstrPWL() returns an error, so I was wondering whether there is any way to work around this in the Python API that allow to treat elements of a Mvar as standard scalar variables. If not, can I check whether the Matlab API would instead support this kind of statements?
Thanks advance,
Marco
-
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 Marco,
You could try to convert the \(\texttt{MVar}\) object to a nested list of Var objects via
mvars = m.addMVar(10, name="myMVars")
mvars_as_vars = mvars.tolist()
m.addGenConstrPWL(mvars_as_vars[0], mvars_as_vars[1], [0,1,2], [1.5,0,3], "myPWLConstr")You will have to use the latest version of Gurobi to make it work as described above.
Best regards,
Jaromił0 -
Great, this works! Thanks a lot for your help.
Best
Marco
0
Post is closed for comments.
Comments
3 comments