Formulate my own objective function using PowerModels.jl variables


I want to use PowerModels.jl flow variables to formulate a different objective function.
I try to see the format of variable_branch_flow and call it with PowerModels.variable_branch_flow(pm). However, when I try to see the output, it always says nothing. Is there anyway I can print out the variable_branch_flow before solving the optimization problem? Or I have to use the code in variable_branch_flow to check the format?

Thank you!

Hi Faoh,

I think the simplest solution is to pass in a JuMP model that you can view. Here are some examples,

using JuMP
using PowerModels

m = Model()
result = run_ac_opf("case.m", with_optimizer(Ipopt.Optimizer), jump_model=m)

Hi Coffrin,

I am wondering can I get load data from the build model?
pm = build_model("~/PowerModels.jl-master/test/data/matpower/case5_tnep.m", DCPPowerModel, PowerModels.post_tnep, ref_extensions=[ref_add_on_off_va_bounds!,ref_add_ne_branch!])
Is there any dict that I can find load information from the pm?

Thank you!

Hi Coffrin,

I have another question when I try to access the branch flow.
I use the a part of code from ‘variable_active_branch_flow’ to get the branch flow information.

p = var(pm, nw, cnd)[:p] = JuMP.@variable(pm.model,
[(l,i,j) in ref(pm, nw, :arcs)], base_name="(nw)_(cnd)_p",
start = comp_start_value(ref(pm, nw, :branch, l), “p_start”, cnd)
When I printout the p, it shows
1-dimensional DenseAxisArray{VariableRef,1,…} with index sets:
Dimension 1, Tuple{Int64,Int64,Int64}[(4, 4, 5), (2, 2, 3), (3, 3, 4), (1, 1, 5), (4, 5, 4), (2, 3, 2), (3, 4, 3), (1, 5, 1)]
And data, a 8-element Array{VariableRef,1}:
0_1_p[(4, 4, 5)]
0_1_p[(2, 2, 3)]
0_1_p[(3, 3, 4)]
0_1_p[(1, 1, 5)]
0_1_p[(4, 5, 4)]
0_1_p[(2, 3, 2)]
0_1_p[(3, 4, 3)]
0_1_p[(1, 5, 1)].
When I access, say p[(4, 4, 5)], it only shows the 0_1_p[(4, 4, 5)] not the flow value of branch (4,4,5).
Can I access the branch flow value during the optimization?

Thank you

Hi Faoh,

For your first question, have you tried data = PowerModels.parse_file("~/PowerModels.jl-master/test/data/matpower/case5_tnep.m")?

For your second question, to see the value of a variable you need something like JuMP.value(p[(4, 4, 5)]), see the JuMP docs for more detials.

I hope that helps!

Thanks for your response!
Actually, for the first question, the parse_file code works in the outside of the optimization problem.
I try something like this to parse the network data within the optimization, but it throws me some error.

function run_tnep(file, model_constructor, optimizer; kwargs…)
data = PowerModels.parse_file(file)

return run_model(file, model_constructor, optimizer, post_tnep; ref_extensions=[ref_add_on_off_va_bounds!,ref_add_ne_branch!], solution_builder = solution_tnep!, kwargs…)

“the general form of the tnep optimization model”
function post_tnep(pm::GenericPowerModel)
data = PowerModels.parse_file(file)

I think the file is the file path to “~/PowerModels.jl-master/test/data/matpower/case5_tnep.m”.

I also try to add file in function like this post_tnep(file, pm::GenericPowerModel). It doesn’t work either.

What else I can try to parse the network data during the optimization process?

Thank you!

Hi Coffrin,

In your original constraint and objective code, does your code get the variable’s value?
Like this one
function constraint_thermal_limit_to(pm::GenericPowerModel, n::Int, c::Int, t_idx, rate_a)
p_to = var(pm, n, c, :p, t_idx)
q_to = var(pm, n, c, :q, t_idx)
JuMP.@constraint(pm.model, p_to^2 + q_to^2 <= rate_a^2)
Since you are comparing the flow with the limitation, what value are p_to and q_to? I think it should be the implicit variable (flow) during the optimization and you didn’t add other function to get the value. Correct me if I misunderstand.

As the objective,
“Cost of building branches”
function objective_tnep_cost(pm::GenericPowerModel)
return JuMP.@objective(pm.model, Min,
sum( branch[“construction_cost”]*var(pm, n, c, :branch_ne, i) for (i,branch) in nw_ref[:ne_branch] )
for c in conductor_ids(pm, n))
for (n, nw_ref) in nws(pm))
I think it also considers the variable in [:branch_ne].
Is there any other function/rules in powermodels that can allow me to check the value of implicit variables?

Thank you!

These variables are often called “decision variables” in mathematical programming. They do not have a state until after an optimization solver is executed on the algorithm. After the solver is executed the JuMP.value is used to retrieve the variables’ value.

I see. In that case, can I create another decision variable that is consisted of your current variables?

Thank you!

Yes. JuMP lets you add new variables. Have a look at PowerModelsAnnex for some examples.