Dualization.jl icon indicating copy to clipboard operation
Dualization.jl copied to clipboard

Implement `JuMP.copy_extension_data`

Open klamike opened this issue 8 months ago • 2 comments

┌ Warning: Model contains extension data of type Dualization.PrimalDualMap{Float64} that we do not know how to copy.
│ 
│ If you are using a JuMP extension and you did not add data to the `model.ext` dictionary. Please open an issue on the GitHub repository of the JuMP extension and tell them to implement `JuMP.copy_extension_data`.
│ 
│ If you added things to `model.ext`, they have not been copied.
└ @ JuMP JuMP.jl/src/copy.jl:23

It is not obvious to me how to do this nicely since we don't have the ReferenceMap in JuMP.copy_extension_data. Ideally it would work like

m = Model()
@variable m x
@constraint m y x ≥ 1
@objective m Min x
dual_model = dualize(model)
copied_dual, ref_map = JuMP.copy_model(dual_model)

# manually propagate keys (current work-around)
y1 = getindex(ref_map, Dualization._get_dual_variables(dual_model, model[:y]))

# proposed behavior -- map from original primal to copied dual
@assert y1 == Dualization._get_dual_variables(copied_dual, model[:y])

Alternatively, set it missing or something that throws to show the above workaround?

klamike avatar Jun 09 '25 17:06 klamike

Why do you want to copy the model? In most cases, I really really recommend against copying a model. There's usually a different way you can achieve your objective.

odow avatar Jun 09 '25 20:06 odow

Here is what I am using copy_model for: https://github.com/LearningToOptimize/L2ODLL.jl/blob/fe32671fba0ac0fbd005bb49a1944dfebf13b276/src/layers/generic.jl#L14-L56

Basically I need to dualize a model then delete some constraints and mark some of the dual variables as parameters, then make a function that sets those parameter values, solves, and reports the optimal objective. I am copying the dual model before doing that since I figured it would be better to not modify the dual_model directly, though I suppose that is not necessary -- just lets me keep the original dual_model around.

klamike avatar Jun 09 '25 20:06 klamike