# Getting variable values in PowerModels.jl

**URL:** <https://discourse.julialang.org/t/getting-variable-values-in-powermodels-jl/15192>\
**Category:** Optimization (Mathematical)\
**Created:** [September 19, 2018, 6:43pm UTC](https://discourse.julialang.org/t/getting-variable-values-in-powermodels-jl/15192 "2018-09-19T18:43:37Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rasoul](https://avatars.discourse-cdn.com/v4/letter/r/bb73d2/32.png) [@Rasoul](https://discourse.julialang.org/u/Rasoul)\
**Post date:** [September 19, 2018, 6:43pm UTC](https://discourse.julialang.org/t/getting-variable-values-in-powermodels-jl/15192/1 "2018-09-19T18:43:37Z")

</div>

Does anyone know how I can get the value of the lifted variables (e.g., \_td) in PowerModels.jl? Thanks in advance!

---

<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:** [September 19, 2018, 7:05pm UTC](https://discourse.julialang.org/t/getting-variable-values-in-powermodels-jl/15192/2 "2018-09-19T19:05:55Z")

</div>

This function may provide a helpful example, [PowerModels.jl/obbt.jl at master · lanl-ansi/PowerModels.jl · GitHub](https://github.com/lanl-ansi/PowerModels.jl/blob/master/src/util/obbt.jl)

---

<div class="post-metadata">

**Author:** ![Rasoul](https://avatars.discourse-cdn.com/v4/letter/r/bb73d2/32.png) [@Rasoul](https://discourse.julialang.org/u/Rasoul)\
**Post date:** [September 20, 2018, 2:28pm UTC](https://discourse.julialang.org/t/getting-variable-values-in-powermodels-jl/15192/3 "2018-09-20T14:28:15Z")

</div>

Thanks for the response! In obbt.jl code a variable optimizes first and its optimal value obtains by “getobjectivevalue()” command. I need to minimize generation cost and obtain all lifted variables (e.g., \_d, w\_fr, etc). Can you please guide me how I can obtain those variables? Is the any specific command that can do this?Thanks in advance!

---

<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:** [September 20, 2018, 2:56pm UTC](https://discourse.julialang.org/t/getting-variable-values-in-powermodels-jl/15192/4 "2018-09-20T14:56:36Z")

</div>

As of PowerModels v0.8, you can do something like this,

```julia
using JuMP; using PowerModels; using Ipopt
pm = build_generic_model("case3.m", QCWRPowerModel, PowerModels.post_opf)
solve_generic_model(pm, IpoptSolver())
getvalue(var(pm, :td))

```

---

<div class="post-metadata">

**Author:** ![Rasoul](https://avatars.discourse-cdn.com/v4/letter/r/bb73d2/32.png) [@Rasoul](https://discourse.julialang.org/u/Rasoul)\
**Post date:** [September 20, 2018, 3:46pm UTC](https://discourse.julialang.org/t/getting-variable-values-in-powermodels-jl/15192/5 "2018-09-20T15:46:36Z")

</div>

Thanks for your response. I could obtain every single variable’s vale in the model. However, I need their values to four digits after decimal points. I used the “floor” command but I got an error.

```julia
floor(getvalue(var(pm, :td)))

```

ERROR: MethodError: no method matching floor(::JuMP.JuMPArray{Float64,1,Tuple{Base.KeyIterator{Dict{Tuple{Int64,Int64},Dict{String,Any}}}}})

I will appreciate it if you guide me to solve the issue!

---

<div class="post-metadata">

**Author:** ![Rasoul](https://avatars.discourse-cdn.com/v4/letter/r/bb73d2/32.png) [@Rasoul](https://discourse.julialang.org/u/Rasoul)\
**Post date:** [September 20, 2018, 4:32pm UTC](https://discourse.julialang.org/t/getting-variable-values-in-powermodels-jl/15192/6 "2018-09-20T16:32:21Z")

</div>

Values of all angle differences are zero for all branches and values of voltage magnitude are 1 for all buses in “nesta\_case5\_pjm”. I think these values have been rounded before storing in the dictionary. Actual value of these variables (i.e., these values in the optimal solution) are different than those obtained by “getvalue” command. Please let me know if I have done something wrong. I implemented the “getvalue” command like the command in your previous post but I don’t know why the results are not compatible with the optimal solution. Thanks in advance!

---

<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:** [September 20, 2018, 6:02pm UTC](https://discourse.julialang.org/t/getting-variable-values-in-powermodels-jl/15192/7 "2018-09-20T18:02:49Z")

</div>

If I run,

```julia
using JuMP; using PowerModels; using Ipopt
pm = build_generic_model("nesta_case5_pjm.m", QCWRPowerModel, PowerModels.post_opf)
solve_generic_model(pm, IpoptSolver())

getvalue(var(pm, :td))
getvalue(var(pm, :vm))

```

I have an output like,

```julia
julia> getvalue(var(pm, :td))
0_1_td: 1 dimensions:
[(1, 2)] = 0.0737577421787345
[(2, 3)] = 0.0009511415894786759
[(1, 4)] = 0.05449794187473173
[(1, 5)] = -0.013965702498034971
[(3, 4)] = -0.02021094189348145
[(4, 5)] = -0.06846364437276671

julia> getvalue(var(pm, :vm))
0_1_vm: 1 dimensions:
[4] = 1.0948294398423672
[2] = 1.0857389679556273
[3] = 1.0942198705117394
[5] = 1.1
[1] = 1.0999999924705084

```

For the floor thing you could try something like,

```julia
for bp in ids(pm, :buspairs)
    println(floor(getvalue(var(pm, :td)[bp])))
end

```

---

<div class="post-metadata">

**Author:** ![Rasoul](https://avatars.discourse-cdn.com/v4/letter/r/bb73d2/32.png) [@Rasoul](https://discourse.julialang.org/u/Rasoul)\
**Post date:** [September 21, 2018, 12:16am UTC](https://discourse.julialang.org/t/getting-variable-values-in-powermodels-jl/15192/8 "2018-09-21T00:16:35Z")

</div>

Thank you so much for the response! It was useful for me.  
I got the same results as you. Voltage angle difference values are appeared by their corresponding (f\_bus, t\_bus) tuples. Is there any way to put voltage angle difference values and their corresponding tuples (i.e., f\_bus, t\_bus) together in a matrix and sort rows of the matrix based on voltage angle difference values. I need that to determine branches with large angle differences. I will appreciate your response!

---

<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:** [September 21, 2018, 3:02am UTC](https://discourse.julialang.org/t/getting-variable-values-in-powermodels-jl/15192/9 "2018-09-21T03:02:23Z")

</div>

This is probably how I would compute that (tested in Julia v0.6),

```julia
case = PowerModels.parse_file("nesta_case5_pjm.m")
result = run_opf(case, ACPPowerModel, IpoptSolver())
PowerModels.update_data(case, result["solution"])

bus_va = Dict( parse(Int,i) => bus["va"] for (i,bus) in case["bus"])
branch_vad = Dict(i => bus_va[branch["f_bus"]] - bus_va[branch["t_bus"]] for (i,branch) in case["branch"])

branch_order = sort(collect(branch_vad), by=x -> -abs(x[2]))

```

The advantage of not digging into the model variables is that it can work with a wider variety of power formulations.

---

<div class="post-metadata">

**Author:** ![Rasoul](https://avatars.discourse-cdn.com/v4/letter/r/bb73d2/32.png) [@Rasoul](https://discourse.julialang.org/u/Rasoul)\
**Post date:** [September 21, 2018, 6:34pm UTC](https://discourse.julialang.org/t/getting-variable-values-in-powermodels-jl/15192/10 "2018-09-21T18:34:16Z")

</div>

Thanks for the response! Just one more question, what is the best way to convert bus\_va or bra nch\_order elements to Float or Int. I used the following command to convert the keys in bus\_va into Float but I couldn’t convert values in bus\_va into Float. Can you please let me know how I can do that? Note that I have to use “imap” command because I need orders to be preserved. Thanks in advance!

```julia
collect(imap(first,bus_va))

```

---

<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:** [September 21, 2018, 8:58pm UTC](https://discourse.julialang.org/t/getting-variable-values-in-powermodels-jl/15192/11 "2018-09-21T20:58:32Z")

</div>

Does this resolve your issue?

```julia
bus_va = Dict( parse(Int,i) => bus["va"] for (i,bus) in case["bus"])
branch_vad = Dict( parse(Int,i) => bus_va[branch["f_bus"]] - bus_va[branch["t_bus"]] for (i,branch) in case["branch"])

branch_order = sort(collect(branch_vad), by=x -> -abs(x[2]))

```

---

<div class="post-metadata">

**Author:** ![Rasoul](https://avatars.discourse-cdn.com/v4/letter/r/bb73d2/32.png) [@Rasoul](https://discourse.julialang.org/u/Rasoul)\
**Post date:** [September 21, 2018, 10:09pm UTC](https://discourse.julialang.org/t/getting-variable-values-in-powermodels-jl/15192/12 "2018-09-21T22:09:40Z")

</div>

No, it did resolve the issue to some extent. However, output for these command are still dictionary which I cannot turn them into matrices easily. For instance following is “branch\_order”

6=\>-0.0626634  
1=\>0.0617572  
2=\>0.0489352  
3=\>-0.0137282  
5=\>-0.00976905  
4=\>-0.00305291

But I need that to be in matrix form

[ 6 , -0.0626634  
1 , 0.0617572  
2 , 0.0489352  
3 ,-0.0137282  
5 ,-0.00976905  
4 ,-0.00305291]

I will appreciate it if you guide me to turn them to matrices. Thanks in advance!

---

<div class="post-metadata">

**Author:** ![Rasoul](https://avatars.discourse-cdn.com/v4/letter/r/bb73d2/32.png) [@Rasoul](https://discourse.julialang.org/u/Rasoul)\
**Post date:** [September 21, 2018, 11:16pm UTC](https://discourse.julialang.org/t/getting-variable-values-in-powermodels-jl/15192/13 "2018-09-21T23:16:17Z")

</div>

I used following commands to change the dictionary first into a tuple and then change the tuple into array.

```julia
a = [(k,v) for (k,v) in branch_order]
t = [y[i] for y in a, i in 1:2]

```

It works for me but I surfed the net and people were pessimistic about performance of these commands. Do you think these commands make the code sluggish? Or do have a better suggestion? Thanks in advance!

---

<div class="post-metadata">

**Author:** ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)\
**Post date:** [September 22, 2018, 12:41am UTC](https://discourse.julialang.org/t/getting-variable-values-in-powermodels-jl/15192/14 "2018-09-22T00:41:02Z")

</div>

You can just go

```julia
[key_value[i] for key_value in branch_order, i in 1:2]

```

---

<div class="post-metadata">

**Author:** ![Rasoul](https://avatars.discourse-cdn.com/v4/letter/r/bb73d2/32.png) [@Rasoul](https://discourse.julialang.org/u/Rasoul)\
**Post date:** [September 22, 2018, 1:21am UTC](https://discourse.julialang.org/t/getting-variable-values-in-powermodels-jl/15192/15 "2018-09-22T01:21:09Z")

</div>

Thanks for the response!  
Following is the results that I have obtained for a variable.

[(1, 2)] = 0.0737577421787345  
[(2, 3)] = 0.0009511415894786759  
[(1, 4)] = 0.05449794187473173  
[(1, 5)] = -0.013965702498034971  
[(3, 4)] = -0.02021094189348145  
[(4, 5)] = -0.06846364437276671

Can you please guide me how I can extract just the values? Thanks in advance!

0.0737577421787345  
0.0009511415894786759  
0.05449794187473173  
-0.013965702498034971  
-0.02021094189348145  
-0.06846364437276671

---

<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:** [September 22, 2018, 6:38pm UTC](https://discourse.julialang.org/t/getting-variable-values-in-powermodels-jl/15192/16 "2018-09-22T18:38:34Z")

</div>

> [@Rasoul](#):
>
> It works for me but I surfed the net and people were pessimistic about performance of these commands.

I would not worry too much about performance until you run into runtime issues. Most power network datasets are relatively small.

---

<div class="post-metadata">

**Author:** ![Rasoul](https://avatars.discourse-cdn.com/v4/letter/r/bb73d2/32.png) [@Rasoul](https://discourse.julialang.org/u/Rasoul)\
**Post date:** [September 23, 2018, 9:27pm UTC](https://discourse.julialang.org/t/getting-variable-values-in-powermodels-jl/15192/17 "2018-09-23T21:27:16Z")

</div>

Thanks for the response! When I executed “getvalue(var(pm, :td))” command I get the following results.  
[(1, 2)] = 0.0726892490132161  
[(2, 3)] = 0.0007423729745521509  
[(1, 4)] = 0.05311970563797517  
[(1, 5)] = -0.013538436007847555  
[(3, 4)] = -0.020311916349793093  
[(4, 5)] = -0.06665814164582272

but when I run " branch\_vad = Dict( parse(Int,i) =\> bus\_va[branch[“f\_bus”]] - bus\_va[branch[“t\_bus”]] for (i,branch) in case[“branch”])" I get the following.

4 =\> 0.000742373  
2 =\> 0.0531197  
3 =\> -0.0135384  
5 =\> -0.0203119  
6 =\> -0.0666581  
1 =\> 0.0726892

As you can see branch indices in the outputs are not consistent. I do want to compare voltage angle difference variable (td) with its corresponding (va(f\_bus)-va(t\_bus)) value. I will appreciate it if you let me know about a way to rearrange one of these outputs to have a same branch index with another one. Thanks in advance and I am looking forward to hearing from 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:** [September 23, 2018, 10:25pm UTC](https://discourse.julialang.org/t/getting-variable-values-in-powermodels-jl/15192/18 "2018-09-23T22:25:53Z")

</div>

There is a many-to-one relationship between the indices of `:td` and the branch ids (e.g. consider parallel branches).

My recommendation would be to build your code around the branch ids used in the later example. If you also want easy access to the bus ids, give this a try,

```julia
branch_vad = Dict( (parse(Int,i), branch[“f_bus”], branch[“t_bus”]) => bus_va[branch[“f_bus”]] - bus_va[branch[“t_bus”]] for (i,branch) in case[“branch”])

```

---

<div class="post-metadata">

**Author:** ![Rasoul](https://avatars.discourse-cdn.com/v4/letter/r/bb73d2/32.png) [@Rasoul](https://discourse.julialang.org/u/Rasoul)\
**Post date:** [September 24, 2018, 1:18am UTC](https://discourse.julialang.org/t/getting-variable-values-in-powermodels-jl/15192/19 "2018-09-24T01:18:04Z")

</div>

Thanks for the instructive recommendation. I took your word and put the piece of code that returns voltage angle difference values, in a for loop in order to have voltage angle difference for all branches including parallel ones. The issue is resolved and now voltage angle difference array obtain by “td” and (“va(f\_bus)-va(t\_bus)”) have a same length (i.e., the length of branch matrix).

aa = getvalue(var(pm, :td))  
for (l,branch) in case[“branch”]  
i = branch[“f\_bus”]  
j = branch[“t\_bus”]  
bpp= Dict( (parse(Int,i), branch[“f\_bus”] , branch[“t\_bus”]) =\> aa[(branch[“f\_bus”],branch[“t\_bus”])] for (i,branch) in case[“branch”])  
end

Thank you so much once again!

---

<div class="post-metadata">

**Author:** ![Rasoul](https://avatars.discourse-cdn.com/v4/letter/r/bb73d2/32.png) [@Rasoul](https://discourse.julialang.org/u/Rasoul)\
**Post date:** [September 25, 2018, 8:20pm UTC](https://discourse.julialang.org/t/getting-variable-values-in-powermodels-jl/15192/20 "2018-09-25T20:20:34Z")

</div>

I have an array (w) including binaries that determines weather or not a specific action should taken on each branch. In other words if w[i] =1 then the action would applicable on ith branch. For instance, for “nesta\_case5\_pjm” I have w=[0;1;1;0;0;0]. I want to convert this array to a dictionary in which values in s can be callable by “bp”. I tried to do that by following command

```julia
bpp= Dict( (branch["f_bus"] , branch["t_bus"]) => w[i] for (i,branch) in case["branch"])    

```

but I got the following error. Can you please guide me on doing this? Thanks in advance!

ERROR: ArgumentError: invalid index: 4

[Next page](https://discourse.julialang.org/t/getting-variable-values-in-powermodels-jl/15192.md?page=2)
