# PowerModelsAnnex.jl obtaining duals

**URL:** https://discourse.julialang.org/t/powermodelsannex-jl-obtaining-duals/43535
**Category:** Optimization (Mathematical)
**Created:** [July 23, 2020, 3:04am UTC](https://discourse.julialang.org/t/powermodelsannex-jl-obtaining-duals/43535 "2020-07-23T03:04:30Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![fnbillimoria](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fnbillimoria/32/16518_2.png) [@fnbillimoria](https://discourse.julialang.org/u/fnbillimoria)
#### Post date: [July 23, 2020, 3:04am UTC](https://discourse.julialang.org/t/powermodelsannex-jl-obtaining-duals/43535/1 "2020-07-23T03:04:30Z")

</div>

I have downloaded the PowerModelsAnnex.jl files. I am using the build\_qc\_opf functions, and wish to obtain dual prices from the solution. The specific dual prices I wish to obtain are the duals of the power balance or KCL constraints formed in the function build\_qc\_opf(). A sample of what I am trying to do is below.

1. Do i need to name the constraints? and if so, how would i name constraints that are called within a for loop within a function. For example, there would be one constraint per bus, so just including a name within the @constraint call would mean that I replace the name at each iteration.
2. In any case not sure whether just calling the resultdual or getdual functions would work outside of the function, where the model is defined within the function? when i have tried getdual(constraintname) it has not recognised the variable.
3. is there a constraint dictionary I can access somehow?

model = Model()  
pm = build\_qc\_opf(data,model)  
set\_optimizer(model,Ipopt.Optimizer)  
result = optimize!(model)  
vars = all\_variables(model)  
has\_duals(model) [this returns a true]  
dual\_price = dual(model)

Thoughts greatly appreciated @ccoffrin

---

<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 23, 2020, 3:58am UTC](https://discourse.julialang.org/t/powermodelsannex-jl-obtaining-duals/43535/2 "2020-07-23T03:58:40Z")

</div>

This is more of a JuMP question than a PowerModels question. You will need to modify `build_qc_opf` to return some pointers to the power balance constraints and then call the `dual` function on those constraints after the `optimize!` call.

At the JuMP level something along these lines should work,

```julia
using JuMP

m = Model(Some.Optimizer)
@variable(m, x)
@objective(m, Min, 2.0*x^2)
c = @constraint(m, x == 1.23)
optimize!(m)
value(x)
dual(c)

```

---

<div class="post-metadata">

### Author: ![fnbillimoria](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fnbillimoria/32/16518_2.png) [@fnbillimoria](https://discourse.julialang.org/u/fnbillimoria)
#### Post date: [July 23, 2020, 5:18am UTC](https://discourse.julialang.org/t/powermodelsannex-jl-obtaining-duals/43535/3 "2020-07-23T05:18:08Z")

</div>

Thank you @ccoffrin

My follow up is probably a more JuMP question then… For constraints that are defined within a for loop, we would need a multi-dimensional pointer, and the pointer would need to be initialised. How would one intialise the pointer? i.e. would you have to declare the type, and what would that type be?

---

<div class="post-metadata">

### Author: ![fnbillimoria](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fnbillimoria/32/16518_2.png) [@fnbillimoria](https://discourse.julialang.org/u/fnbillimoria)
#### Post date: [July 23, 2020, 5:20am UTC](https://discourse.julialang.org/t/powermodelsannex-jl-obtaining-duals/43535/4 "2020-07-23T05:20:15Z")

</div>

I have seen solutions that modify incorporate multi-dimensionality into @constraint. However that would not be suitable if one is manipulating data and expressions earlier in the for loop prior to creating the constraint.

---

<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: [July 23, 2020, 1:08pm UTC](https://discourse.julialang.org/t/powermodelsannex-jl-obtaining-duals/43535/5 "2020-07-23T13:08:08Z")

</div>

You can store constraint in a vector, for example.

```nohighlight
model = Model()
@variable(model, x)
cons = []
for a = 1:4
    c = @constraint(model, x <= a)
    push!(cons, c)
end

```

You can use any Julia data structures you like. You aren’t restricted to the JuMP provided ones.

---

<div class="post-metadata">

### Author: ![fnbillimoria](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fnbillimoria/32/16518_2.png) [@fnbillimoria](https://discourse.julialang.org/u/fnbillimoria)
#### Post date: [July 29, 2020, 4:17am UTC](https://discourse.julialang.org/t/powermodelsannex-jl-obtaining-duals/43535/6 "2020-07-29T04:17:10Z")

</div>

many thanks!
