# Building JuMP models through functions

**URL:** <https://discourse.julialang.org/t/building-jump-models-through-functions/24636>\
**Category:** Optimization (Mathematical)\
**Created:** [May 27, 2019, 7:37am UTC](https://discourse.julialang.org/t/building-jump-models-through-functions/24636 "2019-05-27T07:37:59Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![iharjunk](https://avatars.discourse-cdn.com/v4/letter/i/e0b2c6/32.png) [@iharjunk](https://discourse.julialang.org/u/iharjunk)\
**Post date:** [May 27, 2019, 7:37am UTC](https://discourse.julialang.org/t/building-jump-models-through-functions/24636/1 "2019-05-27T07:37:59Z")

</div>

Hello all,

I would like to make my Julia/JuMP code more flexible and prepare it for larger test cases. For this I would need to

1. Read in data from a given directory
2. Build a model based on it - here since the model is large and has different functional parts I would like to use separate functions for this
3. Solve the model and extract key result data
4. Write the results into file
5. Change the directory and start again (in a loop)

So this should be done for about 100+ test cases… and would really need to do this type of testing in loops (automatically). What I already tried was to create a function

```julia
#function 1: basic math model
function CreateMathModel(parameters)
    create variables and constraints
return m

#function 2: add special variables and constraints
function AddMathModelFeatures(m,parameters)
   create some new variables and constraints - also involving already created variables
return m

#later I want to optimize it and read and store the values in another function

```

But the main problem is that in function 2, it does not recognize the variables I created in function 1… and I get errors such as “type Model has no field x” What is the best way to deal with this? There are quite a few variables involved so it can get complex. I googled for help but it is not trivial to find solutions following the new format (JuMP 0.19).

Thankful for any tips!

---

<div class="post-metadata">

**Author:** ![mike\_k](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mike_k/32/211864_2.png) [@mike\_k](https://discourse.julialang.org/u/mike_k)\
**Post date:** [May 27, 2019, 7:59am UTC](https://discourse.julialang.org/t/building-jump-models-through-functions/24636/2 "2019-05-27T07:59:58Z")

</div>

This should work - I assume you are not accessing variables `x` the right way. Suppose you want to add a knapsack constraint in your second function. You should use something like:

```julia
@constraint( m, dot(w, m[:x]) <= C )

```

At least it works like that with JuMP 0.18. If this is not your problem please provide a more specific example.

---

<div class="post-metadata">

**Author:** ![iharjunk](https://avatars.discourse-cdn.com/v4/letter/i/e0b2c6/32.png) [@iharjunk](https://discourse.julialang.org/u/iharjunk)\
**Post date:** [May 27, 2019, 10:43am UTC](https://discourse.julialang.org/t/building-jump-models-through-functions/24636/3 "2019-05-27T10:43:22Z")

</div>

Hello,  
Thank you for the note! How would I then use multidimensional variables? For instance

```julia
function1()
    @variable(m, x[1:50,1:24])
     ...
    return(m)
 end

function2(m)
    @variable(m, y[1:24],bin)
    MaxValue=100
    for i = 1:50
        for j =1:48
            @constraint(m, x[i,j] <= MaxValue*y[j])
        end
    end
    return(m)
end

model = function1()
expanded_model = function2(model)
...

```

Here it is not clear to me if I could do it just like m[:x[i,j]]…

Thank you in advance!

---

<div class="post-metadata">

**Author:** ![mike\_k](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mike_k/32/211864_2.png) [@mike\_k](https://discourse.julialang.org/u/mike_k)\
**Post date:** [May 27, 2019, 10:58am UTC](https://discourse.julialang.org/t/building-jump-models-through-functions/24636/4 "2019-05-27T10:58:21Z")

</div>

I’m not sure, but I think that

```julia
@constraint(m, m[:x][i][j] <= MaxValue*y[j])

```

should work. At least if `x` was one-dimensional you could access a single coordinate with `m[:x][i]`. So if the first suggestion does not work you could also try it with `m[:x][i,j]`

---

<div class="post-metadata">

**Author:** ![leethargo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/leethargo/32/6004_2.png) [@leethargo](https://discourse.julialang.org/u/leethargo)\
**Post date:** [May 27, 2019, 11:14am UTC](https://discourse.julialang.org/t/building-jump-models-through-functions/24636/5 "2019-05-27T11:14:57Z")

</div>

I think you should also be able to do `x = m[:x]` and then use `x` directly from then on.

What I do in my code is to return not only the model but also the variables from the function that creates them. When there are too many variables, I will create a `struct` to hold all of them, e.g.:  
[https://github.com/rschwarz/PipeLayout.jl/blob/master/src/models/gndstruct\_discdiam/itergbd.jl#L31-L46](https://github.com/rschwarz/PipeLayout.jl/blob/master/src/models/gndstruct_discdiam/itergbd.jl#L31-L46) (for JuMP v0.18).

---

<div class="post-metadata">

**Author:** ![iharjunk](https://avatars.discourse-cdn.com/v4/letter/i/e0b2c6/32.png) [@iharjunk](https://discourse.julialang.org/u/iharjunk)\
**Post date:** [May 29, 2019, 10:48am UTC](https://discourse.julialang.org/t/building-jump-models-through-functions/24636/6 "2019-05-29T10:48:15Z")

</div>

Thank you!

I implemented the suggestion by leethargo and it works like a charm 🙂
