# JuMP Multiple Models Design Question

**URL:** https://discourse.julialang.org/t/jump-multiple-models-design-question/36230
**Category:** Optimization (Mathematical)
**Tags:** jump
**Created:** [March 19, 2020, 9:40pm UTC](https://discourse.julialang.org/t/jump-multiple-models-design-question/36230 "2020-03-19T21:40:26Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![torgo](https://avatars.discourse-cdn.com/v4/letter/t/94ad74/32.png) [@torgo](https://discourse.julialang.org/u/torgo)
#### Post date: [March 19, 2020, 9:40pm UTC](https://discourse.julialang.org/t/jump-multiple-models-design-question/36230/1 "2020-03-19T21:40:26Z")

</div>

What is the rationale behind throwing an error in this?

```julia
using JuMP

m1 = Model()
@variable(m1, x >= 0)

m2 = Model()
@variable(m2, x >= 0)

@objective(m1, Min, x) # this will error w/ variable not owned

```

gives

```julia
ERROR: LoadError: VariableNotOwned{VariableRef}(x)

```

I am telling `@objective` that the model I want it to operate on is `m1`. So shouldn’t it know that the `x` I am referring to is the one that is in `m1`?

I know the workaround:

```julia
using JuMP

m1 = Model()
@variable(m1, x >= 0)

m2 = Model()
@variable(m2, x >= 0)

# @objective(m1, Min, x) # this will error w/ variable not owned

x = m1[:x]
@objective(m1, Min, x)
print(m1)

```

gives

```julia
Min x
Subject to
 x ≥ 0.0

```

But I don’t get why the workaround is necessary. I am interested just so I have a better idea of how JuMP is working under the hood.

---

<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: [March 19, 2020, 10:57pm UTC](https://discourse.julialang.org/t/jump-multiple-models-design-question/36230/2 "2020-03-19T22:57:16Z")

</div>

Here is the documentation which explains the difference: [Variables · JuMP](https://www.juliaopt.org/JuMP.jl/stable/variables/).

Essentially,

```nohighlight
using JuMP

m1 = Model()
@variable(m1, x >= 0) # Makes a Julia variable "x" which points to the JuMP variable "x" in m1.

m2 = Model()
@variable(m2, x >= 0) # Changes the Julia variable "x" to point to the JuMP variable "x" in m2.

m1[:x] # The JuMP variable "x" in m1
m2[:x] # The JuMP variable "x" in m2

m1[:x] == x # false
m2[:x] == x # true

```

---

<div class="post-metadata">

### Author: ![torgo](https://avatars.discourse-cdn.com/v4/letter/t/94ad74/32.png) [@torgo](https://discourse.julialang.org/u/torgo)
#### Post date: [March 19, 2020, 11:08pm UTC](https://discourse.julialang.org/t/jump-multiple-models-design-question/36230/3 "2020-03-19T23:08:20Z")

</div>

My question is _why_ it is like this  
When I say

```julia
@objective(m1, Min, x)

```

I am already being clear about which `x`, I am talking about since I also passed `m1`.  
I’m trying to understand where the ambiguity would arise.

---

<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: [March 19, 2020, 11:49pm UTC](https://discourse.julialang.org/t/jump-multiple-models-design-question/36230/4 "2020-03-19T23:49:25Z")

</div>

`x` is not the name of the variable. It is a Julia binding. Consider

```nohighlight
using JuMP
model = Model()
@variable(model, x)
y = x
@objective(model, Min, y)

julia> println(model)
Min x
Subject to

```

Or even more confusingly

```nohighlight
using JuMP
model = Model()
@variable(model, x)
@variable(model, y)
y = x
@objective(model, Min, y)
@constraint(model, y <= model[:y])

julia> println(model)
Min x
Subject to
 x - y ≤ 0.0

```

---

<div class="post-metadata">

### Author: ![torgo](https://avatars.discourse-cdn.com/v4/letter/t/94ad74/32.png) [@torgo](https://discourse.julialang.org/u/torgo)
#### Post date: [March 20, 2020, 1:04am UTC](https://discourse.julialang.org/t/jump-multiple-models-design-question/36230/5 "2020-03-20T01:04:45Z")

</div>

Thanks, that makes sense as to why this is done the way it is.

---

<div class="post-metadata">

### Author: ![martincornejo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/martincornejo/32/13436_2.png) [@martincornejo](https://discourse.julialang.org/u/martincornejo)
#### Post date: [March 22, 2020, 12:26am UTC](https://discourse.julialang.org/t/jump-multiple-models-design-question/36230/6 "2020-03-22T00:26:35Z")

</div>

My workaround for such cases is using Dictionaries to pack the models

```julia
m = Dict() # models dictionary
x = Dict() # x variables dictionary

# example with 2 models
for i = 1:2
    m[i] = Model()
    x[i] = @variable(m[i], base_name="x$i")
    @constraint(m[i], x[i] >= 0)
    # ... continue describing your model
end

```

The `base_name="x$i"` keyword argument is a nice addition, as you get to name the variables

If anyone has a better alternative I’m open to it 🙂
