# Convex.jl for SDP

**URL:** <https://discourse.julialang.org/t/convex-jl-for-sdp/111474>\
**Category:** Optimization (Mathematical)\
**Tags:** convex, sdp\
**Created:** [March 11, 2024, 5:17pm UTC](https://discourse.julialang.org/t/convex-jl-for-sdp/111474 "2024-03-11T17:17:00Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![iHany](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ihany/32/18151_2.png) [@iHany](https://discourse.julialang.org/u/iHany)\
**Post date:** [March 11, 2024, 5:17pm UTC](https://discourse.julialang.org/t/convex-jl-for-sdp/111474/1 "2024-03-11T17:17:01Z")

</div>

I am trying to follow up [1, Example 1]. This involves semidefinite programming described in Corollary 1:

 ![image](https://global.discourse-cdn.com/julialang/original/3X/d/1/d1478a033a764e5c5ab489bf196d6d3841a1d98d.png)

The following is what I’ve tried, and basically I think I need some specialized syntax for SDP.

# Trial

## Code

```julia
using Convex
using LinearAlgebra
using SCS

"""
[1, Example 1]
# Refs
[1] Khlebnikov, Mikhail V.
"Quadratic stabilization of bilinear control systems."
Automation and Remote Control 77 (2016): 980-991.
"""
function main()
    A = [0 1;
         1 1]
    b = [0 1]'
    D = [1 1;
        -1 1]
    n = 2
    ϵ = 0.1 # TODO: variable?
    μ = 0.0 # TODO: see remark of Corollary 1
    P = Semidefinite(n)
    y = Variable(n)
    tmp = [(A*P + P*A' + b*y' + y*b' + ϵ*D*P*D') y;
           y' -ϵ*I]
    prob = minimize(
                    -eigmin(P),
                    isposdef(-tmp),
                   )
    solve!(prob, SCS.Optimizer; silent_solver=true)
    k = inv(evaluate(P)) * y
    println(evaluate(P))
    println(evaluate(k))
end

```

## Error message

```julia
julia> main()
ERROR: MethodError: no method matching isless(::Matrix{Any}, ::Int64)

Closest candidates are:
  isless(::AbstractFloat, ::Real)
   @ Base operators.jl:179
  isless(::ForwardDiff.Dual{Tx}, ::Integer) where Tx
   @ ForwardDiff ~/.julia/packages/ForwardDiff/PcZ48/src/dual.jl:144
  isless(::ForwardDiff.Dual{Tx}, ::Real) where Tx
   @ ForwardDiff ~/.julia/packages/ForwardDiff/PcZ48/src/dual.jl:144
  ...

Stacktrace:
 [1] <(x::Matrix{Any}, y::Int64)
   @ Base ./operators.jl:343
 [2] <=(x::Matrix{Any}, y::Int64)
   @ Base ./operators.jl:392
 [3] main()
   @ Main ~/.julia/dev/DDControl/bl_ctrl_quad_stab.jl:29
 [4] top-level scope
   @ REPL[2]:1
 [5] top-level scope
   @ ~/.julia/packages/Infiltrator/LtFao/src/Infiltrator.jl:726

```

I followed [this example provided by Convex.jl](https://jump.dev/Convex.jl/stable/examples/general_examples/basic_usage/#SDP-cone-and-Eigenvalues) but somehow it fails.  
Can anyone help me?

# Refs

[1] [Khlebnikov, Mikhail V. “Quadratic stabilization of bilinear control systems.” _Automation and Remote Control_ 77 (2016): 980-991.](https://link.springer.com/article/10.1134/S0005117916060047)

---

<div class="post-metadata">

**Author:** ![iHany](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ihany/32/18151_2.png) [@iHany](https://discourse.julialang.org/u/iHany)\
**Post date:** [March 11, 2024, 8:57pm UTC](https://discourse.julialang.org/t/convex-jl-for-sdp/111474/2 "2024-03-11T20:57:53Z")

</div>

To include SDP constraint, I needed to the following change, which is basically:

1. constructing matrix via `vcat` and `hcat`
2. appropriate clarification of matrix dimension (not `\epsilon * I` but `epsilon` for this case)

```julia
    tmp = vcat(hcat((A*P + P*A' + b*y' + y*b' + ϵ*D*P*D' + μ*P), y), hcat(y', -ϵ))
    prob = minimize(
                    -eigmin(P),
                    isposdef(-tmp),
                   )

```

Note: this “seems” to be solved but the values are different from the reported ones in the reference.

---

<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:** [March 12, 2024, 4:01am UTC](https://discourse.julialang.org/t/convex-jl-for-sdp/111474/3 "2024-03-12T04:01:56Z")

</div>

It should work if you do:

```Julia
tmp = [
    (A*P + P*A' + b*y' + y*b' + ϵ*D*P*D') y;
    y' -ϵ*Matrix(I(1))
]

```

Convex doesn’t support `UniformScaling` from LinearAlgebra.
