How do I add a quadratic constraint using the scip python interface?
In one of the examples, I see something like model.addCons(x*x+y*y<=6
)
However, since I have a lot of variables(x1..xn and my constraint is of the form x'Qx<=0.2
, where x is n*1 and Q is n*n), this method is rather impossible. How can I put the quadratic constraint in a python dictionary of coeffs as I do the linear constraints? (coeffs={x**2:3.0,y**2:1.0,z**2:5.0}
for example if I want 3x^2+y^2+5z^2<=10
)
Asked
Active
Viewed 116 times
0

cprakashagr
- 751
- 11
- 28

snyder
- 1
-
How does `{x:3, y:1, z:5}` relate to 10? – Tadhg McDonald-Jensen Mar 15 '16 at 06:22
-
the 10 is not important just putting here as an example, 10 will be put into the constraint later by setting lhs. – snyder Mar 15 '16 at 06:25
-
but how is that calculated from 3,1, and 5? 3+1+5=9 so I'm trying to figure out where the 10 came from. – Tadhg McDonald-Jensen Mar 15 '16 at 06:26
-
the 3 and 1 and 5 are the coefficients of the variables x^2, y^2, z^2 – snyder Mar 15 '16 at 06:28
-
oh sorry I thought you wanted to generate the restriction based on the dict. – Tadhg McDonald-Jensen Mar 15 '16 at 06:32
1 Answers
0
This is currently not supported. You need to loop through your quadratic constraints and add them one after the other using the expression method.

mattmilten
- 6,242
- 3
- 35
- 65
-
Thank you for your answer. But since I have a large number of quadratic terms in my constraint (x'Qx, where Q is of the scale 5000*5000). Can I efficiently loop through this large number of terms? – snyder Mar 15 '16 at 10:25
-
I don't see why you couldn't. This largely depends on how your coefficients are stored. When creating the quadratic constraint, though, I highly recommend using the quicksum() method instead of explicitly stating the linear or quadratic expression. – mattmilten Mar 15 '16 at 10:38
-
How can I use the quicksum() method, is there any document about it,thanks – snyder Mar 15 '16 at 15:25
-
It's similar to Gurobi's implementation: https://www.gurobi.com/documentation/6.5/refman/py_quicksum.html – mattmilten Mar 15 '16 at 17:11
-
Example: ```model.addCons(quicksum(c[i,j]*x[i,j] for i in I for j in J) <= 10)``` – mattmilten Mar 15 '16 at 17:12
-