# COSMO.jl: SecondOrderCone

**URL:** <https://discourse.julialang.org/t/cosmo-jl-secondordercone/82497>\
**Category:** Optimization (Mathematical)\
**Tags:** question\
**Created:** [June 9, 2022, 12:43pm UTC](https://discourse.julialang.org/t/cosmo-jl-secondordercone/82497 "2022-06-09T12:43:51Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![DanDoe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dandoe/32/52717_2.png) [@DanDoe](https://discourse.julialang.org/u/DanDoe)\
**Post date:** [June 9, 2022, 12:43pm UTC](https://discourse.julialang.org/t/cosmo-jl-secondordercone/82497/1 "2022-06-09T12:43:51Z")

</div>

I would like to use the [`COSMO` optimizing package](https://oxfordcontrol.github.io/COSMO.jl/stable/) for a [second order cone program](https://en.wikipedia.org/wiki/Second-order_cone_programming).

I have a constraint of the form \Vert A \boldsymbol x + \boldsymbol b \Vert\_2 \leq 1 which seems to be a perfect fit for the [`COSMO.Constraint`](https://oxfordcontrol.github.io/COSMO.jl/stable/api/#COSMO.Constraint) together with [`SecondOrderCone`](https://oxfordcontrol.github.io/COSMO.jl/stable/api/#COSMO.SecondOrderCone) with t=1. However, there seems to be no way to supply the value of t, but only the dimensionality `dim`of the argument of the norm (in my case A \boldsymbol x + \boldsymbol b).

Unfortunately, there are no examples how to use the `SecondOrderCone` cone **without** using `JuMP`. I would like to avoid `JuMP` since it seems unnecessary overhead for my problem and also complicates (if not completely prevents) the use of higher precision types then `Float64`.

Is someone here who managed to use `SecondOrderCone` without using `JuMP`?  
I also opened an issue on github since in any case, the documentation on this could be improved.

---

<div class="post-metadata">

**Author:** ![blob](https://avatars.discourse-cdn.com/v4/letter/b/ebca7d/32.png) [@blob](https://discourse.julialang.org/u/blob)\
**Post date:** [June 9, 2022, 3:35pm UTC](https://discourse.julialang.org/t/cosmo-jl-secondordercone/82497/2 "2022-06-09T15:35:16Z")

</div>

I played with the problem a bit. Looking at the documentation for using `SecondOrderCone` in [JuMP](https://jump.dev/JuMP.jl/stable/manual/constraints/#Second-order-cone-constraints) and in [MOI](https://docs.juliahub.com/MathOptInterface/tyub8/0.9.20/apireference/#MathOptInterface.SecondOrderCone) it looks like we need to increase the dimensionality of our problem by adding one more variable `t`. Let’s say that we are looking for `x`. Then we pass `[t x] ϵ SecondOrderCone` to a constraint. And since we want `t=1`, we also need to add a constraint for that. After this lengthy introduction, this code works (based on the [QP example from COSMO](https://oxfordcontrol.github.io/COSMO.jl/stable/examples/qp/)):

```julia
using COSMO, SparseArrays, LinearAlgebra, Test

q = [0;1; 1.];
P = sparse([0 0 0; 0 0. 0;0 0 0]);
A = [1. 0 0; 0 0 0;0 0 0];
l = [1.; 0; 0]; # We don't care about lower and upper bound for x, only for t
u = [1.; 0; 0];

constraint1 = COSMO.Constraint(A, zeros(3), COSMO.Box(l,u),1); # Here we say that we only want box constraints for t
constraint2 = COSMO.Constraint([1 0 0;0 1.0 0; 0 0 1.0], [0.0;0;0], COSMO.SecondOrderCone)

constraints = [constraint1; constraint2]

settings = COSMO.Settings(verbose=true);
model = COSMO.Model();
assemble!(model, P, q,constraints, settings = settings);
res = COSMO.optimize!(model);

```

Just to check, the same problem in JuMP:

```julia
using COSMO, SparseArrays, LinearAlgebra, Test, JuMP

q = [1; 1.];
P = sparse([0. 0;0 0]);

m = JuMP.Model(COSMO.Optimizer);

@variable(m,t)
@variable(m,x[1:2])
@objective(m, Min, x'*P*x+q'*x)
@constraint(m, [t; x] in MOI.SecondOrderCone(3))
@constraint(m,t==1.0)

optimize!(m)

```

---

<div class="post-metadata">

**Author:** ![DanDoe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dandoe/32/52717_2.png) [@DanDoe](https://discourse.julialang.org/u/DanDoe)\
**Post date:** [June 10, 2022, 10:05am UTC](https://discourse.julialang.org/t/cosmo-jl-secondordercone/82497/3 "2022-06-10T10:05:12Z")

</div>

Thanks for your effort! Based on my understanding,  
`constraint2 = COSMO.Constraint([1 0 0;0 1.0 0; 0 0 1.0], [0.0;0;0], COSMO.SecondOrderCone) `  
should translate into  
 \Vert t + x\_1 + x\_2 \Vert\_2 \leq ? which is not the same as \Vert x\_1 + x\_2 \Vert\_2 \leq t, right?

---

<div class="post-metadata">

**Author:** ![blob](https://avatars.discourse-cdn.com/v4/letter/b/ebca7d/32.png) [@blob](https://discourse.julialang.org/u/blob)\
**Post date:** [June 10, 2022, 10:31am UTC](https://discourse.julialang.org/t/cosmo-jl-secondordercone/82497/4 "2022-06-10T10:31:22Z")

</div>

Not really. If you look at how [JuMP](https://jump.dev/JuMP.jl/stable/manual/constraints/#Second-order-cone-constraints) does it (and since COSMO has an interface to JuMP, I am assuming they do in the same way), we have:  
 ![image](https://global.discourse-cdn.com/julialang/original/3X/d/4/d4c6542b25ca431ffccebcbea52255bdcf0dc8f4.png)  
which then gets translated into:  
 ![image](https://global.discourse-cdn.com/julialang/original/3X/9/f/9f0c1b2cf1721158e6e4ac43f5aa0fd40d5ef231.png)  
The first element in `constraint2 = COSMO.Constraint([1 0 0;0 1.0 0; 0 0 1.0], [0.0;0;0],` is put on the right-hand side of the inequality, whereas the rest is under the norm.

---

<div class="post-metadata">

**Author:** ![DanDoe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dandoe/32/52717_2.png) [@DanDoe](https://discourse.julialang.org/u/DanDoe)\
**Post date:** [June 19, 2022, 10:15am UTC](https://discourse.julialang.org/t/cosmo-jl-secondordercone/82497/5 "2022-06-19T10:15:03Z")

</div>

[Confirmed by the author of the package](https://github.com/oxfordcontrol/COSMO.jl/issues/157#issuecomment-1159548658).
