# Accessing dual variables

**URL:** <https://discourse.julialang.org/t/accessing-dual-variables/112719>\
**Category:** Optimization (Mathematical)\
**Tags:** jump\
**Created:** [April 9, 2024, 10:58am UTC](https://discourse.julialang.org/t/accessing-dual-variables/112719 "2024-04-09T10:58:05Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![heiwie](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/heiwie/32/208105_2.png) [@heiwie](https://discourse.julialang.org/u/heiwie)\
**Post date:** [April 9, 2024, 10:58am UTC](https://discourse.julialang.org/t/accessing-dual-variables/112719/1 "2024-04-09T10:58:05Z")

</div>

I am wondering how I can name constraints I define in a for-loop so I later could access the dual variables. My code is the following:

```julia
for (m, parentnode) in enumerate(lattice_edges[2])
    for (n, childnode) in enumerate(parentnode)
       @constraint(model, "$(lattice_edges[2][m][n])", B[3] * w_mB[lattice_nodes[2][m]] + eta[childnode] + (prices[2, n] - cost) * x[childnode] >= 0)              
    end
end

```

Ideally, I would like to name the constraints with the string obtained by indexing the lattice\_edges array as showed in the code. Unfortunately, this does not seem to be possible with my current formulation. The constraints need to be defined with a for loop since there are far too many to be defined individually. Thank you for any and all guidance!

---

<div class="post-metadata">

**Author:** ![jd-foster](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jd-foster/32/35824_2.png) [@jd-foster](https://discourse.julialang.org/u/jd-foster)\
**Post date:** [April 9, 2024, 12:25pm UTC](https://discourse.julialang.org/t/accessing-dual-variables/112719/2 "2024-04-09T12:25:40Z")

</div>

It may be that the `base_name` keyword is useful here?

> **[Constraints · JuMP](https://jump.dev/JuMP.jl/stable/manual/constraints/#Constraint-names)**
>
> Documentation for JuMP.

You can also always store constraint references in standard Julia data structures, such as

```julia
latticeCons = Dict()

for # etc…
…
    latticeCons[m, n] = @constraint(…)

```

Then retrieve the dual values after solving as

```julia
dual.(values(latticeCons))

```

---

<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:** [April 9, 2024, 7:44pm UTC](https://discourse.julialang.org/t/accessing-dual-variables/112719/3 "2024-04-09T19:44:54Z")

</div>

Hi @heiwie, the key thing to remember is that you are not restricted to the data structures given by default in JuMP; you can (and often should) construct things that make sense for your problem.

I would do:

```julia
lattice_constraints = Dict()
for (m, parentnode) in enumerate(lattice_edges[2])
    lattice_constraints[m] = Dict()
    for (n, childnode) in enumerate(parentnode)
        lattice_constraints[m][n] = @constraint(
            model,
            B[3] * w_mB[lattice_nodes[2][m]] + eta[childnode] + (prices[2, n] - cost) * x[childnode] >= 0
        )
    end
end
lattice_constraints

```

or

```julia
lattice_constraints = Dict()
for (m, parentnode) in enumerate(lattice_edges[2])
    for (n, childnode) in enumerate(parentnode)
        lattice_constraints[m => n] = @constraint(
            model,
            B[3] * w_mB[lattice_nodes[2][m]] + eta[childnode] + (prices[2, n] - cost) * x[childnode] >= 0
        )
    end
end
lattice_constraints

```
