# How to copy variables from one optimized JuMP model into another model?

**URL:** <https://discourse.julialang.org/t/how-to-copy-variables-from-one-optimized-jump-model-into-another-model/123103>\
**Category:** Optimization (Mathematical)\
**Created:** [November 26, 2024, 1:39pm UTC](https://discourse.julialang.org/t/how-to-copy-variables-from-one-optimized-jump-model-into-another-model/123103 "2024-11-26T13:39:55Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Realife\_Brahmin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/realife_brahmin/32/50796_2.png) [@Realife\_Brahmin](https://discourse.julialang.org/u/Realife_Brahmin)\
**Post date:** [November 26, 2024, 1:39pm UTC](https://discourse.julialang.org/t/how-to-copy-variables-from-one-optimized-jump-model-into-another-model/123103/1 "2024-11-26T13:39:55Z")

</div>

Hello everyone,

I’m working on a multi-period optimization problem using Julia, JuMP, and Ipopt. My approach involves solving individual time-step optimization subproblems as separate JuMP models (`sub_model_t0`), then intending to “copy” the solved decision variables from these sub-models into a “full” multi-period model (`full_model`).

I’ve run into some challenges with transferring variable values from the sub-models to the full model without re-defining variables or causing conflicts. I want to be able to access the variable values in the full model as if it had been optimized, even though I’ve only solved the sub-models.

Here’s a Minimal Working Example (MWE) illustrating the issue:

```julia
using JuMP
using Ipopt

# Time step
t0 = 3

# Build and solve sub_model_t0 for time step t0
sub_model_t0 = Model(Ipopt.Optimizer)
@variable(sub_model_t0, x1[t0] >= 0)
@variable(sub_model_t0, x2[t0] >= 0)
@objective(sub_model_t0, Min, (x1[t0] - 2)^2 + (x2[t0] - 5)^2)
optimize!(sub_model_t0)

# Adding variables from sub_model_t0 into full_model. 
# Let's assume it hasn't been created yet
full_model = Model(Ipopt.Optimizer)
# Unknown steps to copy the sub_model_t0 variables x1[t0], x2[t0] into full_model

# Intended outcome: I'm able to access the variables in full_model
# and their values (assuming we've somehow copied them over)
println("Value of x1[$(t0)] = $(value(full_model[:x1][t0]))")
println("Value of x2[$(t0)] = $(value(full_model[:x2][t0]))")

# Expected output:
# Value of x1[3] = 2.0
# Value of x2[3] = 5.0

# Note: full_model has not been optimized; it's used as a storage structure.

```

---

<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:** [November 26, 2024, 6:44pm UTC](https://discourse.julialang.org/t/how-to-copy-variables-from-one-optimized-jump-model-into-another-model/123103/2 "2024-11-26T18:44:20Z")

</div>

You cannot do this, and there is no work-around.

You must use a different data structure. For example, something like this:

```julia
variables = Dict{Symbol,Any}()
variables[:x1] = sub_model_t0[:x]
variables[:x2] = sub_model_t1[:x]

```
