Adding Quadratic constraint
AnsweredHello,
I am trying to add a Quadratic constraint to represent the ellipsoid in my model using Gurobipy. The constraint is of the type
X.T * inv(covariance) * X
at the moment, I use numpy multiplication to create Qexpr. I was wondering if there is a efficient way to handle this like in MATLAB version?
Thanks
Vishnu
-
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?. -
Have you tried the matrix-friendly Python API? It interacts well with NumPy ndarrays and SciPy sparse matrices. For example, we can add a constraint of the form \( x^\top Q x \leq 1 \) as follows:
x = m.addMVar(3)
Q = np.random.rand(3, 3)
m.addConstr(x @ Q @ x <= 1)Note that we use MVar objects with this syntax, not Var objects. The expression \( \texttt{x @ Q @ x} \) is of type MQuadExpr instead of QuadExpr.
For more examples, see matrix1.py and matrix2.py. Both of these are included in your Gurobi installation.
0
Post is closed for comments.
Comments
2 comments