# JuMP : Adding variables/constraints to a model dynamically

**URL:** <https://discourse.julialang.org/t/jump-adding-variables-constraints-to-a-model-dynamically/3974>\
**Category:** Optimization (Mathematical)\
**Created:** [May 29, 2017, 11:28am UTC](https://discourse.julialang.org/t/jump-adding-variables-constraints-to-a-model-dynamically/3974 "2017-05-29T11:28:39Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Julie](https://avatars.discourse-cdn.com/v4/letter/j/4af34b/32.png) [@Julie](https://discourse.julialang.org/u/Julie)\
**Post date:** [May 29, 2017, 11:28am UTC](https://discourse.julialang.org/t/jump-adding-variables-constraints-to-a-model-dynamically/3974/1 "2017-05-29T11:28:39Z")

</div>

Is it possible to add dynamically constraints and/or variables to a model?

I know there is a syntax to add variable (for column generation) but in the documentation I found that “Some solvers do not expose the ability to modify a model after creation - the model must be constructed from scratch each time”

How do we know which solvers are able to modify and which ones are not?

---

<div class="post-metadata">

**Author:** ![miles.lubin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/miles.lubin/32/279_2.png) [@miles.lubin](https://discourse.julialang.org/u/miles.lubin)\
**Post date:** [May 29, 2017, 1:19pm UTC](https://discourse.julialang.org/t/jump-adding-variables-constraints-to-a-model-dynamically/3974/2 "2017-05-29T13:19:00Z")

</div>

It’s useful to look at the [source code](https://github.com/JuliaOpt/JuMP.jl/blob/963886fa8a9b668630f0b3704ed22c58a5947ee8/src/JuMP.jl#L366) to see what’s actually going on. You’ll see that if the solver does not support adding variables, JuMP will print the following warning,

```
Solver does not appear to support adding variables to an existing model. JuMP's internal model will be discarded.

```

---

<div class="post-metadata">

**Author:** ![Julie](https://avatars.discourse-cdn.com/v4/letter/j/4af34b/32.png) [@Julie](https://discourse.julialang.org/u/Julie)\
**Post date:** [May 30, 2017, 8:24am UTC](https://discourse.julialang.org/t/jump-adding-variables-constraints-to-a-model-dynamically/3974/3 "2017-05-30T08:24:50Z")

</div>

Ok thank you I will check.

---

<div class="post-metadata">

**Author:** ![Julie](https://avatars.discourse-cdn.com/v4/letter/j/4af34b/32.png) [@Julie](https://discourse.julialang.org/u/Julie)\
**Post date:** [June 22, 2017, 8:17am UTC](https://discourse.julialang.org/t/jump-adding-variables-constraints-to-a-model-dynamically/3974/4 "2017-06-22T08:17:35Z")

</div>

Hi,

I would like to know if it will be possible to handle range constraints  
using JuMP and Mosek solver.

Best regards,

Julie

---

<div class="post-metadata">

**Author:** ![joaquimg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joaquimg/32/223_2.png) [@joaquimg](https://discourse.julialang.org/u/joaquimg)\
**Post date:** [June 22, 2017, 6:18pm UTC](https://discourse.julialang.org/t/jump-adding-variables-constraints-to-a-model-dynamically/3974/5 "2017-06-22T18:18:03Z")

</div>

If Mosek supports range constraints it is possible. Otherwise, JuMP can simply convert the range constraint into two regular constraints.

---

<div class="post-metadata">

**Author:** ![juliaG](https://avatars.discourse-cdn.com/v4/letter/j/e0b2c6/32.png) [@juliaG](https://discourse.julialang.org/u/juliaG)\
**Post date:** [October 26, 2017, 8:03pm UTC](https://discourse.julialang.org/t/jump-adding-variables-constraints-to-a-model-dynamically/3974/6 "2017-10-26T20:03:57Z")

</div>

Hello there,

I see you said, JuMP can simply convert the range constraint into two regular constraint. Did you simply meant something like :

@constraint(m, 0 \<= dummy \<= 1) \<=\>  
@constraint(m, dummy \<= 1)  
@constraint(m, dummy \>=0)

Or is there another way of doing it, because i’m having an issue solving an MIP with range constraints. It gives two different results for the left and right side of the equivalence i wrote. Do you have any idea why?

Thank you for your help,

JuliaG

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [October 26, 2017, 10:35pm UTC](https://discourse.julialang.org/t/jump-adding-variables-constraints-to-a-model-dynamically/3974/7 "2017-10-26T22:35:50Z")

</div>

Can you post a reproducible example? Ideally something minimal?

---

<div class="post-metadata">

**Author:** ![miles.lubin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/miles.lubin/32/279_2.png) [@miles.lubin](https://discourse.julialang.org/u/miles.lubin)\
**Post date:** [October 26, 2017, 11:06pm UTC](https://discourse.julialang.org/t/jump-adding-variables-constraints-to-a-model-dynamically/3974/8 "2017-10-26T23:06:46Z")

</div>

To clarify, JuMP does not convert range constraints into two one-sided constraints. These are passed directly to the solver. Not all solvers handle range constraints correctly, unfortunately. It’s usually safer just to replace them with an equality constraint involving a slack variable that has lower and upper bounds.

---

<div class="post-metadata">

**Author:** ![juliaG](https://avatars.discourse-cdn.com/v4/letter/j/e0b2c6/32.png) [@juliaG](https://discourse.julialang.org/u/juliaG)\
**Post date:** [October 27, 2017, 12:51pm UTC](https://discourse.julialang.org/t/jump-adding-variables-constraints-to-a-model-dynamically/3974/9 "2017-10-27T12:51:32Z")

</div>

> [@miles.lubin](#):
>
> all solvers handle range constraints correctly, unfortunately. It’s usually safer just to replace them with an equality constraint involving a slack variable that has lower and upper bounds.

I replaced the constraint with equality one and slack variables. It gives the same results now.

To be honest, I think my problem came from solving the same problem with two different solvers : GLPKSolverMIP and CPLEX, Cplex being faster but providing a less accurate solution. The second one not handling ranged constraint, I though the difference came from a mistake translating into equality constraint.
