# Invalid variable type present in SDP Optimal Power Flow formulation

**URL:** <https://discourse.julialang.org/t/invalid-variable-type-present-in-sdp-optimal-power-flow-formulation/13536>\
**Category:** Optimization (Mathematical)\
**Created:** [August 15, 2018, 11:32pm UTC](https://discourse.julialang.org/t/invalid-variable-type-present-in-sdp-optimal-power-flow-formulation/13536 "2018-08-15T23:32:06Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alvaro\_Jose\_Gonzalez](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alvaro_jose_gonzalez/32/2915_2.png) [@Alvaro\_Jose\_Gonzalez](https://discourse.julialang.org/u/Alvaro_Jose_Gonzalez)\
**Post date:** [August 15, 2018, 11:32pm UTC](https://discourse.julialang.org/t/invalid-variable-type-present-in-sdp-optimal-power-flow-formulation/13536/1 "2018-08-15T23:32:06Z")

</div>

I am trying to recreate the SDP formulation of the OPF given by [Lavaei and Low 2013](http://netlab.caltech.edu/publications/Lavaei-2012-OPF-TPS.pdf).  
After defining the whole model, I receive a warning of an invalid variable. Am I missing any type declaration besides the one for the SDP matrices?  
Any help is highly appreciated.

Here’s the error and code:

```julia
Invalid variable type present

Stacktrace:
 [1] setvartype!(::Mosek.MosekMathProgSolverInterface.MosekMathProgConicModel, ::Array{Symbol,1}) at C:\Users\alvaro.gonzalez\.julia\v0.6\Mosek\src\MosekConicInterface.jl:619
 [2] #build#119(::Bool, ::Bool, ::JuMP.ProblemTraits, ::Function, ::JuMP.Model) at C:\Users\alvaro.gonzalez\.julia\v0.6\JuMP\src\solvers.jl:388
 [3] (::JuMP.#kw##build)(::Array{Any,1}, ::JuMP.#build, ::JuMP.Model) at .\<missing>:0
 [4] #solve#116(::Bool, ::Bool, ::Bool, ::Array{Any,1}, ::Function, ::JuMP.Model) at C:\Users\alvaro.gonzalez\.julia\v0.6\JuMP\src\solvers.jl:168
 [5] solve(::JuMP.Model) at C:\Users\alvaro.gonzalez\.julia\v0.6\JuMP\src\solvers.jl:150
 [6] include_string(::String, ::String) at .\loading.jl:522

```

```julia
`# Optimization model
sdpOPF = Model(solver = MosekSolver())
  #Sets
  Nodes = 1:N

  # Variables
  @variable(sdpOPF, h)
  @variable(sdpOPF, λ[k in Nodes])
  @variable(sdpOPF, λsub[k in Nodes], lowerbound = 0)
  @variable(sdpOPF, λover[k in Nodes], lowerbound = 0)
  @variable(sdpOPF, ζ[k in Nodes])
  @variable(sdpOPF, ζsub[k in Nodes], lowerbound = 0)
  @variable(sdpOPF, ζover[k in Nodes], lowerbound = 0)
  @variable(sdpOPF, μ[k in Nodes])
  @variable(sdpOPF, μsub[k in Nodes], lowerbound = 0)
  @variable(sdpOPF, μover[k in Nodes], lowerbound = 0)

  # Lagrange multiplier for line-flow limits
  Hlm = Dict()
  for l in Nodes
    for m in Nodes
      Hlm[l,m] = @variable(sdpOPF, [1:3,1:3], category = :SDP, basename="H_$l$m")
    end
  end

  # Lagrange multiplier for the cuadratic cost functions
  Rk = Dict()
  for k in Nodes
    Rk[k] = @variable(sdpOPF, [1:2,1:2], category = :SDP, basename="R_$k")
  end

  # Matrxi-valued function A. A is PSD
  @variable(sdpOPF, A[1:2*N, 1:2*N], SDP)

  # CONSTRAINTS
  @constraint(sdpOPF, λk[k in Nodes],
    λ[k] == λover[k] - λsub[k] + c[k,2] + 2*sqrt(c[k,3])*Rk[k][1,2])
  @constraint(sdpOPF, ζk[k in Nodes],
    ζ[k] == ζover[k] - ζsub[k])
  @constraint(sdpOPF, μk[k in Nodes],
    μ[k] == μover[k] - μsub[k])

  # Objective function
  @constraint(sdpOPF, objtest,
    h == sum(λsub[k]*Pmin[k] - λover[k]*Pmax[k] + λ[k]*Pd[k]
    + ζsub[k]*Qmin[k] - ζover[k]*Qmax[k] + ζ[k]*Qd[k]
    + μsub[k]*Vmin[k]^2 - μover[k]*Vmax[k]^2 + Ω[k]*(c[k,1]-Rk[k][1,2]) for k in Nodes)
    -sum(Ωlm[l,m]*((Smax[l,m]^2)*Hlm[l,m][1,1]+Hlm[l,m][2,2]+Hlm[l,m][3,3]) for l in Nodes, m in Nodes)
    )

  # Matrix-valued function A
  @constraint(sdpOPF,
    A .== sum(λ[k]*Yk[k] + ζ[k]*Yk_over[k] + μ[k]*Mk[k] for k in Nodes)
      + sum(2*Hlm[l,m][1,2]*Ylm[l,m] + 2*Hlm[l,m][1,3]*Ylm_over[l,m] for l in Nodes, m in Nodes))

  # H must be Symmetric
  @constraintref H_Symm[Nodes, Nodes, 1:3, 1:3]
    for l in Nodes, m in Nodes, i in 1:3, j in 1:3
      if i!=j
        H_Symm[l,m,i,j] = @constraint(sdpOPF, Hlm[l,m][i,j] == Hlm[l,m][j,i])
      end
    end

  # R must be Symmetric
  @constraintref R_Symm[Nodes, 1:2, 1:2]
    for k in Nodes, i in 1:2, j in 1:2
      if i!=j
        R_Symm[k,i,j] = @constraint(sdpOPF, Rk[k][i,j] == Rk[k][j,i])
      end
    end

  # Rk[1,1] ==1
  @constraintref R_11[Nodes]
    for k in Nodes
      R_11[k] = @constraint(sdpOPF, Rk[k][1,1] == 1)
    end

  @objective(sdpOPF, Max, h)
  solve(sdpOPF)`

```

---

<div class="post-metadata">

**Author:** ![ccoffrin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ccoffrin/32/400_2.png) [@ccoffrin](https://discourse.julialang.org/u/ccoffrin)\
**Post date:** [August 15, 2018, 11:52pm UTC](https://discourse.julialang.org/t/invalid-variable-type-present-in-sdp-optimal-power-flow-formulation/13536/2 "2018-08-15T23:52:23Z")

</div>

[PowerModels.jl](https://github.com/lanl-ansi/PowerModels.jl) includes a refrence implementation of this model. As of v0.8 you can run it like so,

```julia
result = run_opf("matpower_case.m", SDPWRMPowerModel, sdp_solver)

```

With Mosek the implementation should scale up to networks with about 100 buses.

[This file](https://github.com/lanl-ansi/PowerModels.jl/blob/a9032ad52bf4a57b4851e8b0e8aa3b200ef8017c/src/form/wrm.jl) provides an example of how the PSD constraint needs to be setup.

---

<div class="post-metadata">

**Author:** ![miles.lubin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/miles.lubin/32/279_2.png) [@miles.lubin](https://discourse.julialang.org/u/miles.lubin)\
**Post date:** [August 15, 2018, 11:56pm UTC](https://discourse.julialang.org/t/invalid-variable-type-present-in-sdp-optimal-power-flow-formulation/13536/3 "2018-08-15T23:56:50Z")

</div>

`category = :SDP` is not the correct syntax for declaring PSD variables. See [Variables — JuMP -- Julia for Mathematical Optimization 0.18 documentation](http://www.juliaopt.org/JuMP.jl/0.18/refvariable.html#semidefinite-and-symmetric-variables).

---

<div class="post-metadata">

**Author:** ![Alvaro\_Jose\_Gonzalez](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alvaro_jose_gonzalez/32/2915_2.png) [@Alvaro\_Jose\_Gonzalez](https://discourse.julialang.org/u/Alvaro_Jose_Gonzalez)\
**Post date:** [August 16, 2018, 2:21am UTC](https://discourse.julialang.org/t/invalid-variable-type-present-in-sdp-optimal-power-flow-formulation/13536/4 "2018-08-16T02:21:49Z")

</div>

Thanks! That was the issue. I got confused with the example for declaring anonymous variables.
