# Updating data in JuMP constraints iteratively?

**URL:** <https://discourse.julialang.org/t/updating-data-in-jump-constraints-iteratively/5467>\
**Category:** Optimization (Mathematical)\
**Tags:** question, jump\
**Created:** [August 19, 2017, 8:34pm UTC](https://discourse.julialang.org/t/updating-data-in-jump-constraints-iteratively/5467 "2017-08-19T20:34:14Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [August 19, 2017, 8:34pm UTC](https://discourse.julialang.org/t/updating-data-in-jump-constraints-iteratively/5467/1 "2017-08-19T20:34:14Z")

</div>

Suppose I have a data matrix `A` that is used in one of the constraints:

```julia
using JuMP

nx, ny = 150, 100

A = rand(nx, ny)

m = Model()

@variable(m, X[1:nx,1:ny])

@constraint(m, [i=1:nx,j=1:ny], X[i,j]-A[i,j] ≤ tol)
# more constraints and objective...

for i=1:100
  # solve optimization
  solve(m)

  # update data matrix A
  A += ...

  # update constraints?
end

```

Is it possible to modify the data in a loop and update the constraint with the new data or I need to recreate the entire model inside of the loop?

---

<div class="post-metadata">

**Author:** ![tkoolen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkoolen/32/1603_2.png) [@tkoolen](https://discourse.julialang.org/u/tkoolen)\
**Post date:** [August 19, 2017, 9:23pm UTC](https://discourse.julialang.org/t/updating-data-in-jump-constraints-iteratively/5467/2 "2017-08-19T21:23:39Z")

</div>

[http://www.juliaopt.org/JuMP.jl/0.18/probmod.html#modifying-constraints](http://www.juliaopt.org/JuMP.jl/0.18/probmod.html#modifying-constraints)

> JuMP does not currently support changing constraint coefficients. For less-than and greater-than constraints, the right-hand-side can be changed.

Since you’re only changing right-hand-sides in this particular case, you should be good. The ability to change constraint coefficients has been on top of my wish list for a while.

---

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [August 19, 2017, 9:31pm UTC](https://discourse.julialang.org/t/updating-data-in-jump-constraints-iteratively/5467/3 "2017-08-19T21:31:53Z")

</div>

Thank you @tkoolen, that is already very handy. I have read that page, but for some reason I assumed my use case didn’t fiit in the RHS. I will give it a try.

---

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [August 19, 2017, 10:09pm UTC](https://discourse.julialang.org/t/updating-data-in-jump-constraints-iteratively/5467/4 "2017-08-19T22:09:20Z")

</div>

For completeness, could you please write down the code for the use case with a matrix that I explained? I am not sure how to change the RHS for all [i,j], should I loop?

---

<div class="post-metadata">

**Author:** ![tkoolen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkoolen/32/1603_2.png) [@tkoolen](https://discourse.julialang.org/u/tkoolen)\
**Post date:** [August 19, 2017, 10:18pm UTC](https://discourse.julialang.org/t/updating-data-in-jump-constraints-iteratively/5467/5 "2017-08-19T22:18:05Z")

</div>

```julia
using JuMP

nx, ny = 150, 100

A = rand(nx, ny)
tol = 1e-3

m = Model()

@variable(m, X[1:nx,1:ny])

@constraint(m, foo_constraints[i=1:nx,j=1:ny], X[i,j] ≤ A[i,j] + tol) # move over to RHS just for clarity
# more constraints and objective...

for i=1:100
    # solve optimization
# solve(m)

    # update data matrix A
    A .+= 0.1

    # update constraints
    JuMP.setRHS.(foo_constraints, A .+ tol)
end

```

---

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [August 19, 2017, 10:22pm UTC](https://discourse.julialang.org/t/updating-data-in-jump-constraints-iteratively/5467/6 "2017-08-19T22:22:35Z")

</div>

Thanks! Just to confirm, in my actual formulation there is an additional matrix variable Y in the constraint in place of the tol, but I believe the answer holds the same:

```julia
@variable(m, Y[1:nx,1:ny])

@constrant(m, mycon[i=1:nx,j=1:ny], X[i,j] - A[i,j] ≤ Y[i,j])

# for clarity we can write
@constrant(m, mycon[i=1:nx,j=1:ny], X[i,j] - Y[i,j] ≤ A[i,j])

```

Trying it now. 🙂

---

<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:** [August 23, 2017, 11:20am UTC](https://discourse.julialang.org/t/updating-data-in-jump-constraints-iteratively/5467/7 "2017-08-23T11:20:05Z")

</div>

Coefficient changes will be possible following the transition to MathOptInterface.

---

<div class="post-metadata">

**Author:** ![tkoolen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkoolen/32/1603_2.png) [@tkoolen](https://discourse.julialang.org/u/tkoolen)\
**Post date:** [August 23, 2017, 2:07pm UTC](https://discourse.julialang.org/t/updating-data-in-jump-constraints-iteratively/5467/8 "2017-08-23T14:07:31Z")

</div>

Woohoo!

---

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [October 18, 2017, 10:35pm UTC](https://discourse.julialang.org/t/updating-data-in-jump-constraints-iteratively/5467/9 "2017-10-18T22:35:44Z")

</div>

Currently, is there any way to update linear equality constraints? Even if it requires the master branch of JuMP, I’d like to give it a try.

Alternatively, suppose I have a named constraint and that I need to completely replace it, is it possible?

Also, in the [http://jump.readthedocs.io/en/latest/probmod.html](http://jump.readthedocs.io/en/latest/probmod.html) page, it is mentioned that some solvers do not support model updating, and JuMP just passes an entirely new model again. What are these solvers? Are they listed somewhere in the documentation?
