# second order cone constraint in JuMP 0.19 with gurobi solver

**URL:** https://discourse.julialang.org/t/second-order-cone-constraint-in-jump-0-19-with-gurobi-solver/21813
**Category:** Optimization (Mathematical)
**Tags:** question
**Created:** [March 13, 2019, 8:42am UTC](https://discourse.julialang.org/t/second-order-cone-constraint-in-jump-0-19-with-gurobi-solver/21813 "2019-03-13T08:42:39Z")
**Posts on this page:** 12
**Page:** 1

<div class="post-metadata">

### Author: ![nitu0317](https://avatars.discourse-cdn.com/v4/letter/n/e99b99/32.png) [@nitu0317](https://discourse.julialang.org/u/nitu0317)
#### Post date: [March 13, 2019, 8:42am UTC](https://discourse.julialang.org/t/second-order-cone-constraint-in-jump-0-19-with-gurobi-solver/21813/1 "2019-03-13T08:42:39Z")

</div>

I failed to add SOCP constraint to my model with gurobi solver after updating JuMP to 0.19.

Since `norm` is no longer supported, I tried the example provided in documentation,

```julia
using JuMP,Gurobi,MathOptInterface

env = Gurobi.Env()

model = Model(with_optimizer(Gurobi.Optimizer, env))

@variable(model, x)

@variable(model, t)

#@constraint(model, norm([x-1, x-2]) <= t)

@constraint(model, [t, x-1, x-2] in SecondOrderCone())

print(model)

JuMP.optimize!(model)

```

But the error message says

```julia
ERROR: LoadError: Constraints of type MathOptInterface.VectorAffineFunction{Float64}-in-MathOptIInterface.SecondOrderCone are not supported by the solver and there are no bridges that can refrormulate it into supported constraints.

```

Please help on this.

---

<div class="post-metadata">

### Author: ![blegat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/blegat/32/217090_2.png) [@blegat](https://discourse.julialang.org/u/blegat)
#### Post date: [March 13, 2019, 12:00pm UTC](https://discourse.julialang.org/t/second-order-cone-constraint-in-jump-0-19-with-gurobi-solver/21813/2 "2019-03-13T12:00:59Z")

</div>

For Gurobi you need to enter it as a quadratic constraint. The transformation from SOC to quadratic is currently discussed here:

> <https://github.com/jump-dev/MathOptInterface.jl/pull/478>
>
> Adds a SOCtoQuad bridges which allows to use the
> \`\`\`julia
> @constraint(model, \[…t, x, y\] in SecondOrderCone())
> \`\`\`
> with solvers expecting SOC constraint in quadratic form (e.g. \[LQOI\](https://github.com/JuliaOpt/LinQuadOptInterface.jl) solvers).
> 
> It also add missing methods in MOIU for quadratic functions.
> While doing this, I noticed, that the \`\_pair\` and \`\_canonicalize\` functions of \`src/functions.jl\` were doing the same thing than \`MOIU.termindices\` and \`MOIU.coefficient\`. Hence I moved \`coefficient\` and \`termindices\` (rename \`term\_indices\` following the style guide) to MOI and renamed and documented \`\_pair\` into \`term\_pair\`.
> I also noticed that we have both \`MOI.\_constant\` and \`MOIU.\_constant\` hence I have removed \`MOIU.\_constant\`. We should still replace this by a function with a better name and doc but this will not be addressed in this PR.
> 
> Closes https://github.com/JuliaOpt/MathOptInterfaceBridges.jl/issues/92

We try to make a decision about this during this afternoon collaboration time at JuMP-dev.

---

<div class="post-metadata">

### Author: ![anubhavratha](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/anubhavratha/32/12729_2.png) [@anubhavratha](https://discourse.julialang.org/u/anubhavratha)
#### Post date: [August 30, 2019, 3:26pm UTC](https://discourse.julialang.org/t/second-order-cone-constraint-in-jump-0-19-with-gurobi-solver/21813/3 "2019-08-30T15:26:38Z")

</div>

Hi, new to Julia and JuMP here, but really enjoying it so far.

Unfortunately, I am a bit stuck with this issue and would be happy to know how the dev plan for SOCtoQuadBridge look like at the moment?

As I try to convert an old (but working fine) Python code to Julia, I run into the issue that Gurobi in JuMP cannot automatically reformulate constraints of form `x^2 + y^2 <= z^2, z >= 0` (“Q-matrix” is not PSD but has only 1 negative eigenvalue, so I believe this constraint should be SOC representable). It throws the error that “Q-matrix is not PSD”.The equivalent constraint in the GurobiPy API does result in an “optimal” solution.

A minimal example is posted below. For my larger problem, unfortunately, MOSEK with the `SecondOrderCone()` constraint is not working well as Mosek solver stalls (JuMP status: SLOW\_PROGRESS, Mosek status: Mosek.MSK\_RES\_TRM\_STALL).

It is possible that I am doing something wrong.  
Many thanks in advance for your help.

```julia
using JuMP, Mosek, MosekTools, Gurobi

#Uncomment for Gurobi
m = Model(with_optimizer(Gurobi.Optimizer, Presolve=0, OutputFlag=1, DualReductions=0))

#Uncomment for Mosek
#m = Model(with_optimizer(Mosek.Optimizer,MSK_IPAR_LOG=1,MSK_DPAR_INTPNT_CO_TOL_REL_GAP=1.0e-10))

@variable(m, x)
@variable(m, y)
@variable(m, z)

@constraint(m, z >= 0)
@constraint(m, 1 <= x <= 3)
@constraint(m, 1 <= y <= 3)

#Uncomment for Gurobi
@constraint(m, x*x + y*y <= z*z)

#Uncomment for Mosek
#@constraint(m, [z,x,y] in SecondOrderCone())

@objective(m, Min, x+y)

optimize!(m)
status = termination_status(m)

println(status)
println(JuMP.value(x))
println(JuMP.value(y))
println(JuMP.value(z))

```

---

<div class="post-metadata">

### Author: ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)
#### Post date: [August 30, 2019, 6:27pm UTC](https://discourse.julialang.org/t/second-order-cone-constraint-in-jump-0-19-with-gurobi-solver/21813/4 "2019-08-30T18:27:10Z")

</div>

You should use bounds instead of constraints.

```julia
@variable(m, 1 <= x <= 3)
@variable(m, 1 <= y <= 3)
@variable(m, z >= 0)

```

Gurobi can’t preserve your constraints to prove `z >= 0` .

`SecondOrderCone` support is coming. See [https://github.com/JuliaOpt/Gurobi.jl/pull/231](https://github.com/JuliaOpt/Gurobi.jl/pull/231)

---

<div class="post-metadata">

### Author: ![anubhavratha](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/anubhavratha/32/12729_2.png) [@anubhavratha](https://discourse.julialang.org/u/anubhavratha)
#### Post date: [August 30, 2019, 6:52pm UTC](https://discourse.julialang.org/t/second-order-cone-constraint-in-jump-0-19-with-gurobi-solver/21813/5 "2019-08-30T18:52:48Z")

</div>

Thanks so much, Oscar. It works now for my larger problem too. Looking forward to and following updates on the `SecondOrderCone` support for Gurobi.

---

<div class="post-metadata">

### Author: ![wenqi\_kou](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wenqi_kou/32/9348_2.png) [@wenqi\_kou](https://discourse.julialang.org/u/wenqi_kou)
#### Post date: [September 27, 2019, 5:55am UTC](https://discourse.julialang.org/t/second-order-cone-constraint-in-jump-0-19-with-gurobi-solver/21813/6 "2019-09-27T05:55:59Z")

</div>

Hi Oscar, is `SecondOrderCone` supported by Gurobi now?  
As you said " Gurobi can’t preserve constraints to prove `z >= 0` ", I have a similar issue. But my constraint `z >= 0` is in a loop, so I cannot use bound since I cannot attach same name variable to my model. If so, any better way to solve this problem? Thanks in advance.

---

<div class="post-metadata">

### Author: ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)
#### Post date: [September 27, 2019, 1:21pm UTC](https://discourse.julialang.org/t/second-order-cone-constraint-in-jump-0-19-with-gurobi-solver/21813/7 "2019-09-27T13:21:47Z")

</div>

Yes. It just needs a new tag: [Bump version by odow · Pull Request #259 · jump-dev/Gurobi.jl · GitHub](https://github.com/JuliaOpt/Gurobi.jl/pull/259)

For now, you could try `] add Gurobi#master`.

> But my constraint `z >= 0` is in a loop, so I cannot use bound since I cannot attach same name variable to my model. If so, any better way to solve this problem?

You want to use the anonymous syntax:

```Julia
z = @variable(model, lower_bound = 0)

```

See: [Variables · JuMP](https://www.juliaopt.org/JuMP.jl/v0.20.0/variables/#Anonymous-JuMP-variables-1)

---

<div class="post-metadata">

### Author: ![wenqi\_kou](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wenqi_kou/32/9348_2.png) [@wenqi\_kou](https://discourse.julialang.org/u/wenqi_kou)
#### Post date: [September 27, 2019, 1:38pm UTC](https://discourse.julialang.org/t/second-order-cone-constraint-in-jump-0-19-with-gurobi-solver/21813/8 "2019-09-27T13:38:12Z")

</div>

I am sorry. I am new here. What do you mean `] add Gurobi#master` here? Can you explain more? Thanks!

---

<div class="post-metadata">

### Author: ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)
#### Post date: [September 27, 2019, 1:52pm UTC](https://discourse.julialang.org/t/second-order-cone-constraint-in-jump-0-19-with-gurobi-solver/21813/9 "2019-09-27T13:52:44Z")

</div>

Open the Julia REPL and type

```plaintext
julia> ] add Gurobi#master

```

This will install the latest version of Gurobi.jl. E.g.,

 ![image](https://global.discourse-cdn.com/julialang/original/3X/2/5/2553893b96977ed80908d24df1704589e896097d.png)

---

<div class="post-metadata">

### Author: ![wenqi\_kou](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wenqi_kou/32/9348_2.png) [@wenqi\_kou](https://discourse.julialang.org/u/wenqi_kou)
#### Post date: [September 27, 2019, 4:53pm UTC](https://discourse.julialang.org/t/second-order-cone-constraint-in-jump-0-19-with-gurobi-solver/21813/10 "2019-09-27T16:53:59Z")

</div>

Thanks but julia return this:

 ![image](https://global.discourse-cdn.com/julialang/original/3X/d/6/d6aabb50dce9f06d2de11bad20829aefbf86da44.png)  
and here is my Pkg.status():  
 ![image](https://global.discourse-cdn.com/julialang/original/3X/c/e/ce7a3f542dad7fe048f4328a84512e2091796132.png)  
What should I do?

---

<div class="post-metadata">

### Author: ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)
#### Post date: [September 27, 2019, 7:14pm UTC](https://discourse.julialang.org/t/second-order-cone-constraint-in-jump-0-19-with-gurobi-solver/21813/11 "2019-09-27T19:14:12Z")

</div>

Try running `] up` first. You need JuMP 0.20

---

<div class="post-metadata">

### Author: ![wenqi\_kou](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wenqi_kou/32/9348_2.png) [@wenqi\_kou](https://discourse.julialang.org/u/wenqi_kou)
#### Post date: [September 27, 2019, 8:25pm UTC](https://discourse.julialang.org/t/second-order-cone-constraint-in-jump-0-19-with-gurobi-solver/21813/12 "2019-09-27T20:25:37Z")

</div>

It works now! Thank you so much, Oscar.
