# JuMP to Convex

**URL:** <https://discourse.julialang.org/t/jump-to-convex/49957>\
**Category:** Optimization (Mathematical)\
**Created:** [November 10, 2020, 11:41pm UTC](https://discourse.julialang.org/t/jump-to-convex/49957 "2020-11-10T23:41:31Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![faisal\_moh](https://avatars.discourse-cdn.com/v4/letter/f/c67d28/32.png) [@faisal\_moh](https://discourse.julialang.org/u/faisal_moh)\
**Post date:** [November 10, 2020, 11:41pm UTC](https://discourse.julialang.org/t/jump-to-convex/49957/1 "2020-11-10T23:41:31Z")

</div>

Hi,  
I’m new to Julia. Working on converting an optimization model from JuMP to Convex.

m = Model(Ipopt.Optimizer)

variable(m, x1 \>= 0)  
variable(m, x2 \>= 0)

NLobjective(m, Max, 126x1 - 9x2^2 + 182x2 - 13x2^2)

constraint(m, x1 \<= 4)  
constraint(m, 2x2 \<= 12)  
constraint(m, 3x1 + 2x2 \<= 25)

Any guidance is appreciated.

---

<div class="post-metadata">

**Author:** ![ericphanson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ericphanson/32/215186_2.png) [@ericphanson](https://discourse.julialang.org/u/ericphanson)\
**Post date:** [November 10, 2020, 11:57pm UTC](https://discourse.julialang.org/t/jump-to-convex/49957/2 "2020-11-10T23:57:52Z")

</div>

Are you talking about formulating the problem with Convex.jl? Convex has a pretty different syntax; check out e.g. [this tutorial](https://jump.dev/Convex.jl/stable/quick_tutorial/) to see.

Convex also works somewhat differently than JuMP, since it is designed around conic problems; see for example [this section](https://jump.dev/Convex.jl/stable/#Extended-formulations-and-the-DCP-ruleset-1) of the docs on extended formulations. That means nonlinear solvers like Ipopt can’t be used with Convex.jl.

However, your problem does seem to have a conic formulation you can use with Convex:

```julia
julia> using Convex, COSMO

julia> x1 = Variable()
Variable
size: (1, 1)
sign: real
vexity: affine
id: 251…394

julia> x2 = Variable()
Variable
size: (1, 1)
sign: real
vexity: affine
id: 745…182

julia> objective = 126 * x1 - 9 * square(x2) + 182 * x2 - 13 * square(x2)
+ (concave; real)
├─ * (affine; real)
│ ├─ 126
│ └─ real variable (id: 251…394)
├─ - (concave; negative)
│ └─ * (convex; positive)
│ ├─ 9
│ └─ qol_elem (convex; positive)
│ ├─ …
│ └─ …
├─ * (affine; real)
│ ├─ 182
│ └─ real variable (id: 745…182)
└─ - (concave; negative)
   └─ * (convex; positive)
      ├─ 13
      └─ qol_elem (convex; positive)
         ├─ …
         └─ …

julia> constrs = [x1 <= 4, 2*x2 <= 12, 3*x1 + 2*x2 <= 25]
3-element Array{Convex.LtConstraint,1}:
 <= constraint (affine)
├─ real variable (id: 251…394)
└─ 4
 <= constraint (affine)
├─ * (affine; real)
│ ├─ 2
│ └─ real variable (id: 745…182)
└─ 12
 <= constraint (affine)
├─ + (affine; real)
│ ├─ * (affine; real)
│ │ ├─ 3
│ │ └─ real variable (id: 251…394)
│ └─ * (affine; real)
│ ├─ 2
│ └─ real variable (id: 745…182)
└─ 25

julia> problem = maximize(objective, constrs)
maximize
└─ + (concave; real)
   ├─ * (affine; real)
   │ ├─ 126
   │ └─ real variable (id: 251…394)
   ├─ - (concave; negative)
   │ └─ * (convex; positive)
   │ ├─ …
   │ └─ …
   ├─ * (affine; real)
   │ ├─ 182
   │ └─ real variable (id: 745…182)
   └─ - (concave; negative)
      └─ * (convex; positive)
         ├─ …
         └─ …
subject to
├─ <= constraint (affine)
│ ├─ real variable (id: 251…394)
│ └─ 4
├─ <= constraint (affine)
│ ├─ * (affine; real)
│ │ ├─ 2
│ │ └─ real variable (id: 745…182)
│ └─ 12
└─ <= constraint (affine)
   ├─ + (affine; real)
   │ ├─ * (affine; real)
   │ │ ├─ …
   │ │ └─ …
   │ └─ * (affine; real)
   │ ├─ …
   │ └─ …
   └─ 25

status: `solve!` not called yet

julia> solve!(problem, COSMO.Optimizer)
------------------------------------------------------------------
          COSMO v0.7.7 - A Quadratic Objective Conic Solver
                         Michael Garstka
                University of Oxford, 2017 - 2020
------------------------------------------------------------------

Problem: x ∈ R^{5},
          constraints: A ∈ R^{12x5} (15 nnz),
          matrix size to factor: 17x17,
          Floating-point precision: Float64
Sets: Nonnegatives of dim: 5
          SecondOrderCone of dim: 3
          SecondOrderCone of dim: 3
          ZeroSet of dim: 1
Settings: ϵ_abs = 1.0e-04, ϵ_rel = 1.0e-04,
          ϵ_prim_inf = 1.0e-06, ϵ_dual_inf = 1.0e-04,
          ρ = 0.1, σ = 1e-06, α = 1.6,
          max_iter = 2500,
          scaling iter = 10 (on),
          check termination every 40 iter,
          check infeasibility every 40 iter,
          KKT system solver: QDLDL
Setup Time: 0.06ms

Iter: Objective: Primal Res: Dual Res: Rho:
1 -4.8505e+03 5.3450e+01 5.4218e+01 1.0000e-01
40 -1.0916e+03 5.9188e-01 2.7359e-01 1.0000e-01
80 -8.2161e+02 1.9067e-01 1.3309e+00 1.0000e-01
120 -9.1560e+02 1.0889e-01 8.2135e-01 1.0000e-01
160 -8.6509e+02 5.7417e-02 4.1368e-01 1.0000e-01
200 -8.8906e+02 2.9174e-02 2.2977e-01 1.0000e-01
240 -8.7667e+02 1.7140e-02 1.2106e-01 1.0000e-01
280 -8.8263e+02 8.6168e-03 6.4531e-02 1.0000e-01
320 -8.7954e+02 5.3640e-03 3.4586e-02 1.0000e-01
360 -8.8100e+02 2.7692e-03 1.8063e-02 1.0000e-01
400 -8.8022e+02 1.7995e-03 9.7904e-03 1.0000e-01
440 -8.8057e+02 9.6593e-04 5.0276e-03 1.0000e-01
480 -8.8038e+02 6.4959e-04 2.7635e-03 1.0000e-01
520 -8.8046e+02 3.6258e-04 1.3895e-03 1.0000e-01
560 -8.8041e+02 2.5010e-04 7.8054e-04 1.0000e-01
600 -8.8042e+02 1.4421e-04 3.8051e-04 1.0000e-01
640 -8.8041e+02 1.0115e-04 2.2133e-04 1.0000e-01
680 -8.8041e+02 5.9727e-05 1.0284e-04 1.0000e-01

------------------------------------------------------------------
>>> Results
Status: Solved
Iterations: 680
Optimal objective: -880.4
Runtime: 0.011s (10.85ms)

```

Then you can find the optimal value of the problem and the optimal values of the variables by

```julia
julia> problem.optval
880.4139895737471

julia> evaluate(x1)
4.000000045551431

julia> evaluate(x2)
4.1364021235771595

```

Here, I’ve chosen the optimizer [COSMO.jl](https://github.com/oxfordcontrol/COSMO.jl) which supports the second-order cone (SOCP) formulation used in this problem, but you could also try any of the other solvers listed [here](https://jump.dev/JuMP.jl/stable/installation/#Getting-Solvers-1) which support SOCP constraints.

Edit(@odow): removed some spurious lines from the REPL output.
