Search code examples
arraysjuliaconstraintsmathematical-optimizationjulia-jump

How to initialize an empty array of constraints related to a model in JuMP?


In my algorithm, I add 3 different sets of constraints dynamically to a model (m1), which then I keep in 3 ConstraintRef[] arrays (namely a,b,c). The number of constraints added differ from iteration to iteration. I also need to access the dual values of these constraints after solving it, which for this model is not a problem (for example, dual.(a)). However, at some point in my algorithm, I need to solve a copy of my model (m2). After solving it, I cannot query the duals, since the arrays a,b,c do not have any information about m2.

Is there a way of linking/registering the array names to the model, so that when the model is copied, I can access the dual values efficiently?

Update, here a small example:

using JuMP
using GLPK

a = ConstraintRef[]
m1 = Model(GLPK.Optimizer)
@variable(m1,x)
con1 = @constraint(m1, x==2)
push!(a,con1)
optimize!(m1)
d = dual.(a)

# (..) I keep adding constraints to 'a'

m2 = copy(m1)
set_optimizer(m2, GLPK.Optimizer)

# (..) I keep populating the set of constraints in 'a'

optimize!(m2)

What I want to be able to do is to get the duals of all constraints included in a, but for m2. Obviously, dual.(a) does not work. I add the constraints as anonymous because I don't know beforehand how many do I need to add at each iteration.


Solution

  • It's easier to provide help if you provide a minimal working example of what you are trying to achieve.

    However, you are probably looking for the reference_map that is returned from copy_model, which maps between objects in the original model and the copied object in the new model:

    model = Model()
    @variable(model, x)
    @constraint(model, cref, x == 2)
    
    new_model, reference_map = copy_model(model)
    x_new = reference_map[x]
    cref_new = reference_map[cref]
    

    Docs: https://jump.dev/JuMP.jl/dev/reference/models/#JuMP.copy_model