Example of SOCP constraints in cvxpy

1,527 views
Skip to first unread message

Alex Flint

unread,
Jan 26, 2015, 12:40:16 PM1/26/15
to cv...@googlegroups.com
Can anyone point me to an example of expressing the SOCP constraint

    || Ax + b ||_2 <= c^T x + d

using cvxpy?

Thanks!

Steven Diamond

unread,
Jan 26, 2015, 1:03:29 PM1/26/15
to cv...@googlegroups.com
import numpy as np
import cvxpy as cvx
m = 10
n = 5
A = np.random.randn(m, n)
b = np.random.randn(m, 1)
c = np.random.randn(n, 1)
d = np.random.randn()

x = cvx.Variable(n)
soc_constraint = (cvx.norm(A*x + b, 2) <= c.T*x + d)

yassine.ab...@gmail.com

unread,
Feb 3, 2015, 5:03:42 AM2/3/15
to cv...@googlegroups.com
Hi everyone !

I get that it is possible to model an SOC constraint just like you explained above, but it seems to me that, internally, using the norm atom consists in creating a formal SOC constraint (cvxpy.constraints.second_order.SOC object) and then adding the minimization of the right hand side to the objective, thereby creating additional variable and constraints. Is there not a workaround to use directly the SOC object ? When I try it in a naive way (i.e just calling the constructor of the SOC class with argument t and x_elems), I get an error "'SOC' object has no attribute 'is_dcp'". 

Thanks in advance !

Steven Diamond

unread,
Feb 3, 2015, 12:53:29 PM2/3/15
to cv...@googlegroups.com
That's a good question. Right now it's not possible to have a pure SOC constraint, i.e. directly use the SOC object. 

The reason is that cvxpy doesn't support constraints other than <= and ==. This will change in the next major update, but for the moment I don't think it matters. Extra variables aren't a big deal because the problem data matrix in the final cone program is extremely sparse.

Ilya Grigoriev

unread,
Oct 11, 2018, 10:34:44 PM10/11/18
to cvxpy
Is this still the recommended way to create SOC constraints? There is now a SOC constraint class, but the documentation for it is a little confusing. (What does it mean that "each row of X is a cone"?) It also says somewhere that end users are never supposed to use it.

The application I have in mind is stochastic linear programming.

If this is the recommended way, it would be nice to add it to the tutorial. (I was very much surprised it is OK to use norm inside a constraint. I tried x * x for a scalar, and it did not work, though I now realize I should have tried x * x.T). I could send a pull request once I get something working, but it would be quite amateurish.

Thank you very much!
             Ilya.

Ilya Grigoriev

unread,
Oct 12, 2018, 12:02:25 AM10/12/18
to cvxpy
Things made a lot more sense after I read the DCP section more carefully. It explains why x*x does not work, and also that the constraints CVXPY accepts are a lot more general than I thought they were.

        Ilya.

Steven Diamond

unread,
Oct 13, 2018, 10:55:38 AM10/13/18
to cvxpy
You can use the SOC constraint object if you like. The documentation is explaining how the axis argument works. If you write SOC(x,t) where x is a vector and t is a scalar, this won't be relevant.
Reply all
Reply to author
Forward
0 new messages