Accessing dual variables

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:

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!

It may be that the base_name keyword is useful here?

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

latticeCons = Dict()

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

Then retrieve the dual values after solving as

dual.(values(latticeCons))
1 Like

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:

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

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
1 Like