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?
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.
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
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])