# JuMP semidefinite programming - formulating via constraints versus variables

**URL:** <https://discourse.julialang.org/t/jump-semidefinite-programming-formulating-via-constraints-versus-variables/90832>\
**Category:** Optimization (Mathematical)\
**Tags:** jump\
**Created:** [November 25, 2022, 10:56pm UTC](https://discourse.julialang.org/t/jump-semidefinite-programming-formulating-via-constraints-versus-variables/90832 "2022-11-25T22:56:02Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![greatpet](https://avatars.discourse-cdn.com/v4/letter/g/e495f1/32.png) [@greatpet](https://discourse.julialang.org/u/greatpet)\
**Post date:** [November 25, 2022, 10:56pm UTC](https://discourse.julialang.org/t/jump-semidefinite-programming-formulating-via-constraints-versus-variables/90832/1 "2022-11-25T22:56:02Z")

</div>

I have the following two model definitions in JuMP. They are formally equivalent. Will one of them be more efficient than the other?

Version 1:

```julia
a = [1.0 0.0; 0.0 1.0]
b = [1.0 0.0; 0.0 -1.0]
@variable(model, x)
@constraint(model, a + x * b >= 0, PSDCone())

```

Version 2:

```julia
a = [1.0 0.0; 0.0 1.0]
b = [1.0 0.0; 0.0 -1.0]
@variable(model, x)
@variable(model, X[1:2, 1:2], PSD)
for i in 1:2, j in 1:2
    @constraint(model, a[i,j] + x * b[i,j] == X[i,j])
end

```

After defining the model in either manner, I will run

```julia
@objective(model, Max, x)
optimize!(model)

```

The performance of the code `optimize!(model)` is what I’m concerned about.

P.S. Of course the above problem is trivial. In practice I’ll deal with matrices of size N much bigger than 2, but still just one variable x or at most a few variables.

---

<div class="post-metadata">

**Author:** ![zdenek\_hurak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zdenek_hurak/32/53118_2.png) [@zdenek\_hurak](https://discourse.julialang.org/u/zdenek_hurak)\
**Post date:** [November 25, 2022, 11:08pm UTC](https://discourse.julialang.org/t/jump-semidefinite-programming-formulating-via-constraints-versus-variables/90832/2 "2022-11-25T23:08:39Z")

</div>

In the documentation for [Semidefinite constraints](https://jump.dev/JuMP.jl/stable/manual/constraints/#Semidefinite-constraints) you can read

> Where possible, prefer constructing a matrix of [Semidefinite variables](https://jump.dev/JuMP.jl/stable/manual/variables/#Semidefinite-variables) using the [`@variable`](https://jump.dev/JuMP.jl/stable/reference/variables/#JuMP.@variable) macro, rather than adding a constraint like `@constraint(model, X >= 0, PSDCone())` . In some solvers, adding the constraint via [`@constraint`](https://jump.dev/JuMP.jl/stable/reference/constraints/#JuMP.@constraint) is less efficient, and can result in additional intermediate variables and constraints being added to the model.

---

<div class="post-metadata">

**Author:** ![greatpet](https://avatars.discourse-cdn.com/v4/letter/g/e495f1/32.png) [@greatpet](https://discourse.julialang.org/u/greatpet)\
**Post date:** [November 25, 2022, 11:20pm UTC](https://discourse.julialang.org/t/jump-semidefinite-programming-formulating-via-constraints-versus-variables/90832/3 "2022-11-25T23:20:59Z")

</div>

But in my problem, declaring a semidefinite matrix variable (version 2) means that I then have to add N^2 constraints for the value of each matrix component. Could this become a source of inefficiency?

---

<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:** [November 26, 2022, 4:56am UTC](https://discourse.julialang.org/t/jump-semidefinite-programming-formulating-via-constraints-versus-variables/90832/4 "2022-11-26T04:56:52Z")

</div>

> Will one of them be more efficient than the other?

It likely depends on the solver. But since version 1 is the easiest to read, you should probably just stick with that unless it is too slow.

In more detail, `a + x * b >= 0, PSDCone()` is a `VectorAffineFunction-in-PositiveSemidefiniteConeSquare`. If solvers support that it will be efficient, otherwise we’d rewrite to version 2 anyway.

The documentation is really about the difference between `@constraint(model, X >= 0, PSDCone())` and `@variable(model, X[1:n, 1:n], PSD)`, i.e., a variable instead of an affine function.

---

<div class="post-metadata">

**Author:** ![greatpet](https://avatars.discourse-cdn.com/v4/letter/g/e495f1/32.png) [@greatpet](https://discourse.julialang.org/u/greatpet)\
**Post date:** [November 26, 2022, 11:45pm UTC](https://discourse.julialang.org/t/jump-semidefinite-programming-formulating-via-constraints-versus-variables/90832/5 "2022-11-26T23:45:00Z")

</div>

For large matrix sizes, I get much better accuracy if I use [Dualization.jl](https://github.com/jump-dev/Dualization.jl) (at least with the Mosek solver), probably due to how my problem is formulated, following [suggestions in another thread](https://discourse.julialang.org/t/solving-sdp-problem-is-100x-slower-than-matlab-cvx/79704/5).
