# Help with JuMP extension

**URL:** <https://discourse.julialang.org/t/help-with-jump-extension/79485>\
**Category:** Optimization (Mathematical)\
**Tags:** question, package, jump\
**Created:** [April 14, 2022, 12:30pm UTC](https://discourse.julialang.org/t/help-with-jump-extension/79485 "2022-04-14T12:30:13Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![math\_opt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/math_opt/32/25317_2.png) [@math\_opt](https://discourse.julialang.org/u/math_opt)\
**Post date:** [April 14, 2022, 12:30pm UTC](https://discourse.julialang.org/t/help-with-jump-extension/79485/1 "2022-04-14T12:30:14Z")

</div>

I am trying to tweak JuMP so that I can specify and query some more specific stuff that I can not currently do with inbuilt JuMP functions and macros. See the example below:

First, I want to specify the ‘kind’ of variable I am defining. For example, in the snippet below, `x[1:2]` are `stage-1` variables, and `y[1:5]` are `stage-2` variables. So, I want to be able to specify these ‘kinds’ when declaring the variables. I understand one way is to do is as shown [here](https://jump.dev/JuMP.jl/stable/developers/extensions/#Extend-%5B@variable%5D(@ref)); however, I would also be want to able to retrieve the variables of particular ‘kind’ later, and of course, solve a JuMP model with these new variables involved. See the code snippet below; it summarizes what I am trying to do.

```julia
using JuMP
model = Model(CPLEX.Optimizer)

#Be able to declare the 'kind' of variable
@variable(model, x[1:2] >= 0, stage1)
@variable(model, y[1:5] >= 0, stage2)

#Be able to retrieve the variable and their 'kind'
stage1_var(model) #should return x[1], x[2]
stage2_var(model) #should return y[1],..., y[5]

@constraint(model, x[1]+y[3] <= 3)
@constraint(model, x[2]+y[5] <= 10)

@objective(model, Min, sum(x[i] for i=1:2) + sum(y[i] for i=1:5))

#Be able to solve a JuMP model with the above 'new' type of variables defined
optimize!(model) 

```

Additionally, after solving this model, I would want to use this JuMP `model` variables (not their values) in another JuMP model with additional variables and constraints. How do I ‘transfer’ these variables from one model to another?

Can someone guide me on how to proceed with this, and what would be a good approach to achieve this? Any help is appreciated.  
I have used JuMP to solve models before, but I am very new to extending JuMP models. I have found myself spending lots of time reading other packages and documentation. While I continue to do so, I would really appreciate ideas and feedback from people who have worked extensively with these kinds of problems. Thanks a lot!

---

<div class="post-metadata">

**Author:** ![mtanneau](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mtanneau/32/17787_2.png) [@mtanneau](https://discourse.julialang.org/u/mtanneau)\
**Post date:** [April 14, 2022, 12:37pm UTC](https://discourse.julialang.org/t/help-with-jump-extension/79485/2 "2022-04-14T12:37:59Z")

</div>

Hi, and welcome to Julia Discourse!

It looks like you are trying to build/solve stochastic optimization problems.  
If your goal is to do that, and not to build an entire JuMP extension around it, I can point you to existing JuMP extensions that cater to stochastic optimization.

For instance:

- StochasticPrograms (see [this example](https://martinbiel.github.io/StochasticPrograms.jl/stable/manual/model/)). I believe the name is explicit enough 🙂
- [ParameterJuMP](https://github.com/JuliaStochOpt/ParameterJuMP.jl) allows you to tag certain variables as parameter, which are automatically processed before passing the model to the solver.

If your goal is to build a new JuMP extension, then more involved JuMP developers will be more knowledgeable than me about how to go about that.

---

<div class="post-metadata">

**Author:** ![math\_opt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/math_opt/32/25317_2.png) [@math\_opt](https://discourse.julialang.org/u/math_opt)\
**Post date:** [April 14, 2022, 12:56pm UTC](https://discourse.julialang.org/t/help-with-jump-extension/79485/3 "2022-04-14T12:56:30Z")

</div>

Hi @mtanneau,

Thanks for the references. Yes, I am working with stochastic programs (but not just trying to build/solve them), and actually currently trying to understand the [StochasticPrograms.jl](https://github.com/martinbiel/StochasticPrograms.jl) as it seems the closest to what I am trying to achieve. Also, I never came across [ParameterJuMP](https://github.com/JuliaStochOpt/ParameterJuMP.jl), but it looks interesting, and maybe I could borrow some ideas from there.

That being said, I am actually trying to build a JuMP extension, so it goes without saying I need to understand all the machinery and workflow techniques. I hope I get some help here. 🙂

---

<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:** [April 14, 2022, 7:11pm UTC](https://discourse.julialang.org/t/help-with-jump-extension/79485/4 "2022-04-14T19:11:01Z")

</div>

- For two-stage problems, I second [https://github.com/martinbiel/StochasticPrograms.jl](https://github.com/martinbiel/StochasticPrograms.jl). Martin has gotten a real job, so he isn’t working on it anymore, but spending time updating StochasticPrograms is probably more efficient than writing something from scratch.
- For multistage problems, check out [https://github.com/odow/SDDP.jl](https://github.com/odow/SDDP.jl). We do exactly the stage trick that you mention: [https://github.com/odow/SDDP.jl/blob/master/src/JuMP.jl](https://github.com/odow/SDDP.jl/blob/master/src/JuMP.jl)

---

<div class="post-metadata">

**Author:** ![math\_opt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/math_opt/32/25317_2.png) [@math\_opt](https://discourse.julialang.org/u/math_opt)\
**Post date:** [April 14, 2022, 7:21pm UTC](https://discourse.julialang.org/t/help-with-jump-extension/79485/5 "2022-04-14T19:21:33Z")

</div>

These references can possibly give me a good start, especially the SDDP reference you gave. I will spend some time analyzing these codes, and see if I have further questions. Thank you.

---

<div class="post-metadata">

**Author:** ![bbrunaud](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bbrunaud/32/1737_2.png) [@bbrunaud](https://discourse.julialang.org/u/bbrunaud)\
**Post date:** [April 24, 2022, 12:08am UTC](https://discourse.julialang.org/t/help-with-jump-extension/79485/6 "2022-04-24T00:08:09Z")

</div>

With Plasmo.jl you can define a network of models for such applications.

---

<div class="post-metadata">

**Author:** ![math\_opt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/math_opt/32/25317_2.png) [@math\_opt](https://discourse.julialang.org/u/math_opt)\
**Post date:** [April 24, 2022, 10:25pm UTC](https://discourse.julialang.org/t/help-with-jump-extension/79485/7 "2022-04-24T22:25:14Z")

</div>

> [@bbrunaud](#):
>
> Plasmo.jl

Thanks @bbrunaud for directing me to Plasmo.jl. It looks interesting. I will take a deeper look into it.

---

<div class="post-metadata">

**Author:** ![bbrunaud](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bbrunaud/32/1737_2.png) [@bbrunaud](https://discourse.julialang.org/u/bbrunaud)\
**Post date:** [April 28, 2022, 1:47am UTC](https://discourse.julialang.org/t/help-with-jump-extension/79485/8 "2022-04-28T01:47:23Z")

</div>

I wrote PlasmoAlgorithms.jl automated decomposition algorithms starting from Plasmo model graphs

---

<div class="post-metadata">

**Author:** ![math\_opt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/math_opt/32/25317_2.png) [@math\_opt](https://discourse.julialang.org/u/math_opt)\
**Post date:** [April 28, 2022, 2:02am UTC](https://discourse.julialang.org/t/help-with-jump-extension/79485/9 "2022-04-28T02:02:05Z")

</div>

Thanks for the reference!
