# How to merge two \`JuMP\` models?

**URL:** <https://discourse.julialang.org/t/how-to-merge-two-jump-models/113646>\
**Category:** Optimization (Mathematical)\
**Tags:** question, jump\
**Created:** [April 30, 2024, 11:36am UTC](https://discourse.julialang.org/t/how-to-merge-two-jump-models/113646 "2024-04-30T11:36:01Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![WuSiren](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wusiren/32/42529_2.png) [@WuSiren](https://discourse.julialang.org/u/WuSiren)\
**Post date:** [April 30, 2024, 11:36am UTC](https://discourse.julialang.org/t/how-to-merge-two-jump-models/113646/1 "2024-04-30T11:36:01Z")

</div>

For example, there are two `JuMP` models named `M1` and `M2`:

```julia
using JuMP

M1 = Model()
@variable(M1, x <= 3)
@variable(M1, y <= 3)
@objective(M1, Max, x + y)

M2 = Model()
@variable(M2, x >= 1)
@variable(M2, y >= 1)
@constraint(M2, x + y <= 3)

```

How to merge `M2` into `M1`, and if they have variables with the same names, rename the corresponding variables from `M2` (except the variable is `x`)?

Thanks!

---

<div class="post-metadata">

**Author:** ![nsajko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nsajko/32/221187_2.png) [@nsajko](https://discourse.julialang.org/u/nsajko)\
**Post date:** [April 30, 2024, 1:12pm UTC](https://discourse.julialang.org/t/how-to-merge-two-jump-models/113646/2 "2024-04-30T13:12:07Z")

</div>

I don’t know about JuMP.jl specifically, however I’ll note that JuMP is just a convenient higher-level interface for MathOptInterface.jl, and the latter might be better suited for your needs if you’re having these kinds of issues. Personally I never use JuMP, instead relying on MOI directly.

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [April 30, 2024, 1:42pm UTC](https://discourse.julialang.org/t/how-to-merge-two-jump-models/113646/3 "2024-04-30T13:42:55Z")

</div>

Just so I understand your problem, how exactly this would work? Only one of the models has an objective function? (Otherwise, you would need some way to merge objective functions.) But if only the variables of one model appear in the objective function, and the variables of the two models are disjunct (you rename so they are not the same), then what you have in reality is really two models (not one).

---

<div class="post-metadata">

**Author:** ![kellertuer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kellertuer/32/220707_2.png) [@kellertuer](https://discourse.julialang.org/u/kellertuer)\
**Post date:** [April 30, 2024, 3:02pm UTC](https://discourse.julialang.org/t/how-to-merge-two-jump-models/113646/4 "2024-04-30T15:02:12Z")

</div>

Could you elaborate a bit on what this should mathematically mean?  
As far as I understand you would like to rename the `x` in M2 “before marging”. So let’s call it `z`.

If I would “merge” these into M1, I would still be able to choose `y` and (now) `z` freely, since the objective does only depend on (M1s) `x`.  
So that merge would mathematically have zero effect compared to just considering M1.

---

<div class="post-metadata">

**Author:** ![WuSiren](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wusiren/32/42529_2.png) [@WuSiren](https://discourse.julialang.org/u/WuSiren)\
**Post date:** [April 30, 2024, 3:34pm UTC](https://discourse.julialang.org/t/how-to-merge-two-jump-models/113646/5 "2024-04-30T15:34:36Z")

</div>

I’m very sorry for not expressing my intention correctly. 🙏 🙏 I have edited the OP.

---

<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:** [May 1, 2024, 2:04am UTC](https://discourse.julialang.org/t/how-to-merge-two-jump-models/113646/6 "2024-05-01T02:04:16Z")

</div>

You cannot merge two JuMP models, and there is no work-around.

At a higher level, what are you trying to achieve? Why do you need to build to models and then merge them?

---

<div class="post-metadata">

**Author:** ![WuSiren](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wusiren/32/42529_2.png) [@WuSiren](https://discourse.julialang.org/u/WuSiren)\
**Post date:** [May 1, 2024, 3:03am UTC](https://discourse.julialang.org/t/how-to-merge-two-jump-models/113646/7 "2024-05-01T03:03:18Z")

</div>

For example, there may be a main model that is open to the user to allow them to add some other constraints w.r.t. some certain variable such as the model `M1` and the variable `x` in the OP.

Perhaps it can be written within the following function:

```julia
using JuMP

function fun(M2::Moldel)
    M1 = Model()
    @variable(M1, x <= 3)
    @variable(M1, y <= 3)
    @objective(M1, Max, x + y)

    # Merge M2 into M1 here.

    optimize!(M1)
    return value(x)
end

```

Of course, I can’t expect how the users would model the constraints w.r.t. `x`. Maybe they would introduce other variables like `y` as auxiliary variables. But do you think it would be the developer’s duty to make sure the auxiliary variables from the input model (`M2`) don’t conflict with the internal main model (`M1`)?

Won’t that be a legitimate need, or will there be any work-around?

Thanks for your attention.

---

<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:** [May 1, 2024, 3:14am UTC](https://discourse.julialang.org/t/how-to-merge-two-jump-models/113646/8 "2024-05-01T03:14:59Z")

</div>

You could write your example as:

```Julia
function fun(M::Model)
    x, y = M[:x], M[:y]
    @objective(M, Max, x + y)
    optimize!(M)
    return value(x)
end

```

> But do you think it would be the developer’s duty to make sure the auxiliary variables from the input model (`M2` ) don’t conflict with the internal main model (`M1` )?

Yes. JuMP supports anonymous variables for this use-case.

```Julia
function fun(M::Model)
    x = M[:x]
    y = @variable(M, upper_bound = 3)
    # This y is not the other y
    @objective(M, Max, x + y)
    optimize!(M)
    return value(x)
end

```

You should read the tutorial:

- [Design patterns for larger models · JuMP](https://jump.dev/JuMP.jl/stable/tutorials/getting_started/design_patterns_for_larger_models/#Generalize-constraints-and-objectives)
