Adding a new function to the PowerModelsAnnex

Hi all,

I have written a function, i.e, sdp_opf, to solve the OPF problem via SDP relaxation which exports the build_sdp_opf. However, when I want to run my code via build_sdp_opf(case,model) I get the following error.

build_sdp_opf not defined

I have already written similar functions to and add them to the PowerModelsAnnex in Julia 0.6 to solve the OPF problem and I didn’t get any error. However, recently I upgraded my Julia to 1.3 and I got the above error. I will appreciate it if you let me know about possible actions that I should take to run my code in Julia 1.3. Thanks in advance!

This seems like a repeat of a question in,

I think the core point here is that a lot changed about how package development works between Julia v0.6 and Julia v1.0. You might start by reviewing those changes. For example,


It’s fairly similar to how it use to work, just make sure that you are working out of the “dev” directory and that your active environment is setup to use that version of the code.

1 Like
Hi Dr. Coffrin,

Thanks for your response, it was useful and I really appreciate it. I fixed that error by activating the environment. As you might know I am writing the SDP relaxation for the OPF problem in rectangular coordinate in PowerModelsAnnex.
The core difference between my formulation and yours in PowerMpdels is about how the “W” matrix is defined. In my implementation I have defined a 2n*2n positive semidefinite matrix named “W” and then I extract “w”,”wr”, “wi” out of the “W” matrix to defined all other constraints based on these variables.
I have two questions. First, with the following implementation I get an error (i.e. MethodError: no method matching _get_name(::QuoteNode)) for line “@variable(model,:w)[i] = W[w_idx,w_idx]+W[w_idx+n_bus,w_idx+n_bus]”.

My second question is, even after commenting out that line I get the following error, UndefVarError: nw not defined. I don’t have any “nw” in my code except in the second line. I will appreciate it if you guide me to solve these error. Thanks in advance!

    PowerModels.standardize_cost_terms!(data, order=2)
    ref = PowerModels.build_ref(data)[:nw][0]
    bus_ids = keys(ref[:bus])
    n_bus = length(bus_ids)
    w_index = 1:length(bus_ids)
    lookup_w_index = Dict((bi,i) for (i,bi) in enumerate(bus_ids))

    WR_start_1 = zeros(length(bus_ids), length(bus_ids)) + I
    WR_start_2 = zeros(length(bus_ids), length(bus_ids))
    WR_start_3 = zeros(length(bus_ids), length(bus_ids))
    WR_start_4 = zeros(length(bus_ids), length(bus_ids))

    W_start=[WR_start_1  WR_start_2;WR_start_3  WR_start_4]
    @variable(model,
    W[i=1:2*length(bus_ids), j=1:2*length(bus_ids)], PSD, base_name="$(nw)_W", start=W_start[i,j]
    )
    @variable(model)[:w] = Dict{Int,Any}()
    for (i, bus) in ref(pm, :bus)
        w_idx = lookup_w_index[i]
        @variable(model,:w)[i] = W[w_idx,w_idx]+W[w_idx+n_bus,w_idx+n_bus]
   end

I think the issue is with this @variable(model)[:w], I think the code should look something more like this,

    PowerModels.standardize_cost_terms!(data, order=2)
    ref = PowerModels.build_ref(data)[:nw][0]
    bus_ids = keys(ref[:bus])
    n_bus = length(bus_ids)
    w_index = 1:length(bus_ids)
    lookup_w_index = Dict((bi,i) for (i,bi) in enumerate(bus_ids))

    WR_start_1 = zeros(length(bus_ids), length(bus_ids)) + I
    WR_start_2 = zeros(length(bus_ids), length(bus_ids))
    WR_start_3 = zeros(length(bus_ids), length(bus_ids))
    WR_start_4 = zeros(length(bus_ids), length(bus_ids))

    W_start=[WR_start_1  WR_start_2;WR_start_3  WR_start_4]
    @variable(model,
    W[i=1:2*length(bus_ids), j=1:2*length(bus_ids)], PSD, base_name="$(nw)_W", start=W_start[i,j]
    )
    w_lookup = Dict{Int,Any}()
    for (i, bus) in ref(pm, :bus)
        w_idx = lookup_w_index[i]
        w_lookup[i] = W[w_idx,w_idx]+W[w_idx+n_bus,w_idx+n_bus]
   end

Thanks for your invaluable response! It fixed the error. Can you please give me some insights on how to solve the issue in the second part of my question. I still get the following error, UndefVarError: nw not defined.
I don’t have any “nw” in my code except in the second line. Thanks in advance!

Most likely this line should be changed to something like base_name="W"