Updating data in JuMP constraints iteratively?

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

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?

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.

1 Like

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.

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?

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
2 Likes

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:

@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. :slight_smile:

Coefficient changes will be possible following the transition to MathOptInterface.

4 Likes

Woohoo!

1 Like

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 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?

1 Like