# Formulate my own objective function using PowerModels.jl variables

**URL:** <https://discourse.julialang.org/t/formulate-my-own-objective-function-using-powermodels-jl-variables/25938>\
**Category:** Specific Domains\
**Tags:** question\
**Created:** [July 2, 2019, 6:03pm UTC](https://discourse.julialang.org/t/formulate-my-own-objective-function-using-powermodels-jl-variables/25938 "2019-07-02T18:03:02Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![faoh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/faoh/32/9483_2.png) [@faoh](https://discourse.julialang.org/u/faoh)\
**Post date:** [July 2, 2019, 6:03pm UTC](https://discourse.julialang.org/t/formulate-my-own-objective-function-using-powermodels-jl-variables/25938/1 "2019-07-02T18:03:02Z")

</div>

Hello,

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!

---

<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:** [July 10, 2019, 8:29pm UTC](https://discourse.julialang.org/t/formulate-my-own-objective-function-using-powermodels-jl-variables/25938/2 "2019-07-10T20:29:15Z")

</div>

Hi Faoh,

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

[https://lanl-ansi.github.io/PowerModels.jl/stable/quickguide/#Building-PowerModels-from-Network-Data-Dictionaries-1](https://lanl-ansi.github.io/PowerModels.jl/stable/quickguide/#Building-PowerModels-from-Network-Data-Dictionaries-1)

```julia
using JuMP
using PowerModels

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

```

---

<div class="post-metadata">

**Author:** ![faoh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/faoh/32/9483_2.png) [@faoh](https://discourse.julialang.org/u/faoh)\
**Post date:** [July 17, 2019, 2:42am UTC](https://discourse.julialang.org/t/formulate-my-own-objective-function-using-powermodels-jl-variables/25938/3 "2019-07-17T02:42:13Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![faoh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/faoh/32/9483_2.png) [@faoh](https://discourse.julialang.org/u/faoh)\
**Post date:** [July 17, 2019, 3:32pm UTC](https://discourse.julialang.org/t/formulate-my-own-objective-function-using-powermodels-jl-variables/25938/4 "2019-07-17T15:32:49Z")

</div>

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

---

<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:** [July 17, 2019, 4:15pm UTC](https://discourse.julialang.org/t/formulate-my-own-objective-function-using-powermodels-jl-variables/25938/5 "2019-07-17T16:15:56Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![faoh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/faoh/32/9483_2.png) [@faoh](https://discourse.julialang.org/u/faoh)\
**Post date:** [July 17, 2019, 4:38pm UTC](https://discourse.julialang.org/t/formulate-my-own-objective-function-using-powermodels-jl-variables/25938/6 "2019-07-17T16:38:33Z")

</div>

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…)  
end

“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!

---

<div class="post-metadata">

**Author:** ![faoh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/faoh/32/9483_2.png) [@faoh](https://discourse.julialang.org/u/faoh)\
**Post date:** [July 17, 2019, 8:11pm UTC](https://discourse.julialang.org/t/formulate-my-own-objective-function-using-powermodels-jl-variables/25938/7 "2019-07-17T20:11:45Z")

</div>

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)  
end.  
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(  
sum(  
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))  
)  
end  
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!

---

<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:** [July 17, 2019, 11:07pm UTC](https://discourse.julialang.org/t/formulate-my-own-objective-function-using-powermodels-jl-variables/25938/8 "2019-07-17T23:07:06Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![faoh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/faoh/32/9483_2.png) [@faoh](https://discourse.julialang.org/u/faoh)\
**Post date:** [July 17, 2019, 11:19pm UTC](https://discourse.julialang.org/t/formulate-my-own-objective-function-using-powermodels-jl-variables/25938/9 "2019-07-17T23:19:03Z")

</div>

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

Thank you!

---

<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:** [July 17, 2019, 11:26pm UTC](https://discourse.julialang.org/t/formulate-my-own-objective-function-using-powermodels-jl-variables/25938/10 "2019-07-17T23:26:39Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![faoh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/faoh/32/9483_2.png) [@faoh](https://discourse.julialang.org/u/faoh)\
**Post date:** [July 23, 2019, 4:23pm UTC](https://discourse.julialang.org/t/formulate-my-own-objective-function-using-powermodels-jl-variables/25938/11 "2019-07-23T16:23:47Z")

</div>

Hi Coffrin,

I notice that in the constraints, there is a function to get the power flow variables:  
function constraint\_thermal\_limit\_from(pm::GenericPowerModel, n::Int, c::Int, f\_idx, rate\_a)  
p\_fr = var(pm, n, c, :p, f\_idx)  
end  
I know the f\_idx = (i, f\_bus, t\_bus), but what is n and c? From the definition, n is the network id and c is the conductor id. When I assign the corresponding number, it shows key not found. Do I need to specify something else for n, and c.  
I want to get the flow variables individually, like this:  
p1=var(pm, nw, cnd,:p,(1,1,5)) #p[(1,1,5)]  
p2=var(pm, nw, cnd,:p,(2,2,3)) #p[(2,2,3)]  
p3=var(pm, nw, cnd,:p,(3,3,4)) #p[(3,3,4)]  
p4=var(pm, nw, cnd,:p,(4,4,5)) #p[(4,4,5)]

pne1=var(pm, nw, cnd,:p\_ne,(1,1,2)) #p\_ne[(1,1,2)]  
pne2=var(pm, nw, cnd,:p\_ne,(2,1,4)) #p\_ne[(2,1,4)]  
pne3=var(pm, nw, cnd,:p\_ne,(3,2,4)) #p\_ne[(3,2,4)]

and put them into my nonlinear objective equation, which is very messy.

Thanks
