Skip to content
This repository has been archived by the owner on Apr 21, 2022. It is now read-only.

Trouble writing a .cbf with constraints for a mixed-integer PSD program #86

Closed
bcdandurand opened this issue Oct 10, 2019 · 7 comments
Closed

Comments

@bcdandurand
Copy link

bcdandurand commented Oct 10, 2019

I am using Julia 1.2 and the most recent MOI. I have the following reasonably minimal example

using MathOptFormat,SCS,JuMP


(nbuses, nlines, ngens) = (9, 9, 3)
(N, L, G) = (1:9, 1:9, 1:3)
(fromLines, toLines, fromBus, toBus) = (Array{Int64,1}[[1], [], [4], [2], [3], [5], [6], [7, 8], [9]], Array{Int64,1}[[], [7], [], [1, 9], [2], [3, 4], [5], [6], [8]], [1, 4, 5, 3, 6, 7, 8, 8, 9], [4, 5, 6, 6, 7, 8, 2, 9, 4])
(BusGeners, Y) = (Array{Int64,1}[[1], [2], [3], [], [], [], [], [], []], Dict{Any,Any}("ffR" => [0.0, 1.9421912487147266, 1.2820091384241148, 0.0, 1.155087480890097, 1.6171224732461358, 0.0, 1.1876043792911484, 1.36518771331058],"ffI" => [-17.36111111111111, -10.43168205186793, -5.409244962361526, -17.064846416382252, -9.679770426363174, -13.623478596908443, -16.0, -5.822134533308591, -11.516095563139931],"ttR" => [0.0, 1.9421912487147266, 1.2820091384241148, 0.0, 1.155087480890097, 1.6171224732461358, 0.0, 1.1876043792911484, 1.36518771331058],"ttI" => [-17.36111111111111, -10.43168205186793, -5.409244962361526, -17.064846416382252, -9.679770426363174, -13.623478596908443, -16.0, -5.822134533308591, -11.516095563139931],"ftR" => [-0.0, -1.9421912487147266, -1.2820091384241148, -0.0, -1.155087480890097, -1.6171224732461358, -0.0, -1.1876043792911484, -1.36518771331058],"tfI" => [17.36111111111111, 10.510682051867931, 5.588244962361526, 17.064846416382252, 9.784270426363173, 13.697978596908444, 16.0, 5.975134533308591, 11.60409556313993],"shR" => [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],"shI" => [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],"ftI" => [17.36111111111111, 10.510682051867931, 5.588244962361526, 17.064846416382252, 9.784270426363173, 13.697978596908444, 16.0, 5.975134533308591, 11.60409556313993],"tfR" => [0.0, -1.9421912487147266, -1.2820091384241148, 0.0, -1.155087480890097, -1.6171224732461358, 0.0, -1.1876043792911484, -1.36518771331058]))
(Pmin, Pmax, Qmin, Qmax) = ([0.1, 0.1, 0.1], [2.5, 3.0, 2.7], [-3.0, -3.0, -3.0], [3.0, 3.0, 3.0])
(Wmin, Wmax) = ([0.81, 0.81, 0.81, 0.81, 0.81, 0.81, 0.81, 0.81, 0.81], [1.21, 1.21, 1.21, 1.21, 1.21, 1.21, 1.21, 1.21, 1.21])
(PD, QD) = ([0.0, 0.0, 0.0, 0.0, 0.9, 0.0, 1.0, 0.0, 1.25], [0.0, 0.0, 0.0, 0.0, 0.3, 0.0, 0.35, 0.0, 0.5])

    mMP = Model(with_optimizer(SCS.Optimizer,verbose=1,max_iters=10000))
    @variable(mMP, -1 <= α[i=N] <= 1)
    @variable(mMP, -1 <= β[i=N] <= 1)
    @variable(mMP, γp[i=N] >= 0)
    @variable(mMP, γm[i=N] >= 0)
    @constraint(mMP, [i=N], γp[i]+γm[i] <= 1)
    @variable(mMP, ζpUB[g=G] >= 0)
    @variable(mMP, ζpLB[g=G] >= 0)
    @variable(mMP, ζqUB[g=G] >= 0)
    @variable(mMP, ζqLB[g=G] >= 0)
    @variable(mMP, x[l=L], Bin)
    @constraint(mMP, sum(x[l] for l in L) <= 3)

    @variable(mMP, λF[l=L]); @variable(mMP, λT[l=L]); @variable(mMP, μF[l=L]); @variable(mMP, μT[l=L])

  for i in N
   for g in BusGeners[i]
    @constraint(mMP,
    -α[i] + ζpUB[g] - ζpLB[g] == 0 )
    @constraint(mMP,
    -β[i] + ζqUB[g] - ζqLB[g] == 0 )
   end
  end

    @constraint(mMP, AMcf1[l in L], α[fromBus[l]] - x[l] <= λF[l]); @constraint(mMP, AMcf2[l in L], α[fromBus[l]] + x[l] >= λF[l])
    @constraint(mMP, AMcf3[l in L], -(1 - x[l]) <= λF[l]); @constraint(mMP, AMcf4[l in L], (1 - x[l]) >= λF[l])
    @constraint(mMP, AMct1[l in L], α[toBus[l]] - x[l] <= λT[l]); @constraint(mMP, AMct2[l in L], α[toBus[l]] + x[l] >= λT[l])
    @constraint(mMP, AMct3[l in L], -(1 - x[l]) <= λT[l]); @constraint(mMP, AMct4[l in L], (1 - x[l]) >= λT[l])

    @constraint(mMP, BMcf1[l in L], β[fromBus[l]] - x[l] <= μF[l]); @constraint(mMP, BMcf2[l in L], β[fromBus[l]] + x[l] >= μF[l])
    @constraint(mMP, BMcf3[l in L], -(1 - x[l]) <= μF[l]); @constraint(mMP, BMcf4[l in L], (1 - x[l]) >= μF[l])
    @constraint(mMP, BMct1[l in L], β[toBus[l]] - x[l] <= μT[l]); @constraint(mMP, BMct2[l in L], β[toBus[l]] + x[l] >= μT[l])
    @constraint(mMP, BMct3[l in L], -(1 - x[l]) <= μT[l]); @constraint(mMP, BMct4[l in L], (1 - x[l]) >= μT[l])

   @expression(mMP, C[i=1:(2*nbuses),j=i:(2*nbuses)], 0)
  for i in N
    C[i,i] += γp[i] - γm[i] + α[i]*Y["shR"][i] - β[i]*Y["shI"][i]
    C[nbuses+i,nbuses+i] += γp[i] - γm[i] + α[i]*Y["shR"][i] - β[i]*Y["shI"][i]
    for l in fromLines[i]
      C[i,i] += λF[l]*Y["ffR"][l] - μF[l]*Y["ffI"][l]
      C[nbuses+i,nbuses+i] += λF[l]*Y["ffR"][l] - μF[l]*Y["ffI"][l]
    end
    for l in toLines[i]
      C[i,i] += λT[l]*Y["ttR"][l] - μT[l]*Y["ttI"][l]
      C[nbuses+i,nbuses+i] += λT[l]*Y["ttR"][l] - μT[l]*Y["ttI"][l]
    end
  end
  for l in L
      from=fromBus[l]; to=toBus[l]
      C[from,nbuses+to] -= 0.5*(  λF[l]*Y["ftI"][l] + μF[l]*Y["ftR"][l] - λT[l]*Y["tfI"][l] - μT[l]*Y["tfR"][l]  )
      C[to,nbuses+from] += 0.5*(  λF[l]*Y["ftI"][l] + μF[l]*Y["ftR"][l] - λT[l]*Y["tfI"][l] - μT[l]*Y["tfR"][l]  )
      if from < to
        C[from,to] += 0.5*(λF[l]*Y["ftR"][l]  - μF[l]*Y["ftI"][l] + λT[l]*Y["tfR"][l] - μT[l]*Y["tfI"][l])
        C[nbuses+from,nbuses+to] += 0.5*(λF[l]*Y["ftR"][l]  - μF[l]*Y["ftI"][l] + λT[l]*Y["tfR"][l] - μT[l]*Y["tfI"][l])
      else
        C[to,from] += 0.5*(λF[l]*Y["ftR"][l]  - μF[l]*Y["ftI"][l] + λT[l]*Y["tfR"][l] - μT[l]*Y["tfI"][l])
        C[nbuses+to,nbuses+from] += 0.5*(λF[l]*Y["ftR"][l]  - μF[l]*Y["ftI"][l] + λT[l]*Y["tfR"][l] - μT[l]*Y["tfI"][l])
      end
  end

  @variable(mMP, H[i=1:(2*nbuses),j=1:(2*nbuses)], PSD)
  @constraint(mMP, SetH[i=1:(2*nbuses),j=i:(2*nbuses)], C[i,j] - H[i,j] == 0 )
  @objective(mMP, Max, sum(ζpLB[g]*Pmin[g] - ζpUB[g]*Pmax[g] + ζqLB[g]*Qmin[g] - ζqUB[g]*Qmax[g]  for g in G)
    + sum( γm[i]*Wmin[i]-γp[i]*Wmax[i] + α[i]*PD[i] + β[i]*QD[i] for i in N))

  fn_base=string("CBF/","case",9,"-",3,".cbf")
  mathoptformat_model = MathOptFormat.CBF.Model()
  MOI.copy_to(mathoptformat_model, backend(mMP))
  MOI.write_to_file(mathoptformat_model, fn_base)

The only way this code writes the cbf file without an error is if I code out all of the constraints. With the error, the message is something like

ERROR: LoadError: MathOptInterface.UnsupportedConstraint{MathOptInterface.ScalarAffineFunction{Float64},MathOptInterface.EqualTo{Float64}}: `MathOptInterface.ScalarAffineFunction{Float64}`-in-`MathOptInterface.EqualTo{Float64}` constraint is not supported by the model.
Stacktrace: ....etc.

What am I missing? Thank you.

@blegat
Copy link
Contributor

blegat commented Oct 10, 2019

Try replacing

MOI.copy_to(mathoptformat_model, backend(mMP))

by

MOI.copy_to(MOI.Bridges.full_bridge_optimizer(mathoptformat_model), backend(mMP))

@bcdandurand
Copy link
Author

Trying that, I get the following error:

ERROR: LoadError: MethodError: no method matching full_bridge_optimizer(::MathOptFormat.CBF.InnerModel{Float64})
Closest candidates are:
full_bridge_optimizer(::MathOptInterface.ModelLike, !Matched::Type) at /homes/bcdandurand/.julia/packages/MathOptInterface/iPJzh/src/Bridges/Bridges.jl:28
Stacktrace:
[1] top-level scope at /nfs2/bcdandurand/OPF-VID/src/mwe.jl:96
[2] include at ./boot.jl:328 [inlined]
[3] include_relative(::Module, ::String) at ./loading.jl:1094
[4] include(::Module, ::String) at ./Base.jl:31
[5] exec_options(::Base.JLOptions) at ./client.jl:295
[6] _start() at ./client.jl:464
in expression starting at /nfs2/bcdandurand/OPF-VID/src/mwe.jl:96

@bcdandurand
Copy link
Author

I tried using Float64 as the 2nd argument to full_bridge_optimizer and it seemed to work, I got a cbf. Is Float64 the correct 2nd argument?

@odow
Copy link
Owner

odow commented Oct 10, 2019

Yes, Float64 is the correct argument. The docs should be improved, but the longer-term fix is to integrate MathOptFormat into JuMP and handle this behind-the-scenes.

@bcdandurand
Copy link
Author

Thank you for your help.

@odow
Copy link
Owner

odow commented Oct 10, 2019

I'm going to keep this open until the docs are improved or this is integrated into JuMP.

@odow odow reopened this Oct 10, 2019
@odow
Copy link
Owner

odow commented Dec 9, 2019

Closed by jump-dev/JuMP.jl#2114

@odow odow closed this as completed Dec 9, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants