I have an optimization problem where i want to change the RHS of a set of constraints and solve the model each time.

The RHS Demand_specific is the input to the model. I want to test the model for different fluctuations in demand values, so i am changing the demand matrix iteratively.

```
using JuMP, Gurobi
for t in 1:n_echelons
for g in 1:n_customers
for k in 1:n_customers
for p in 1:n_products
@constraint(IMTLP, Demandsconst, Direct_shipping[k,g,p,t]+sum(Endhaul_shipping[j,g,k,p,t] for j in 1:n_hubs)>=Demand_specific[g,k,p,t])
end
end
end
end
OptimalCosts = zeros(n_instances)
for i in 1:n_instances
JuMP.setRHS(Demandsconst, (2i/n_instances)*Demand_specific)
status = solve(IMTLP)
z = getobjectivevalue(IMTLP)
OptimalCosts[i] = z
end
```

I am getting the following warning:

Warning: A variable or constraint named Demandsconst is already attached to this model. If creating constraints programmatically, use the anonymous constraint syntax con = @constraint(m, …).

Please try to format your code to make it more readable. This is explained in PSA: make it easier to help you.

It looks like you are create many constraints in nested loops, but always reuse the same identifier `Demandsconst`

. You can not refer to all of these constraints with a single name.

Try moving the loops “into” the `@constraint`

macro call and storing the constraint references in an array, like `@constraint(IMTLP, Demandsconst[t in 1:n_echelons, ...], ...)`

as described in the JuMP docs.

Later you will have to update the right-hand side of all these constraints individually, I guess.

1 Like

Maybe it would also make sense to introduce a dummy variable that you place as the right-hand side. Then you could fix that variable to different values and would not have to touch the constraints.

i can’t use the first approach due to my problem structure, i will give the second method a try.

Thanks

You could just use anonymous constraints, Constraints · JuMP, after which you can store the generated constraint references in any container you like.

Thanks it did work, i shouldn’t have used nested loops,

I did it like, and then i can use JuMP.setRHS:

```
@constraint(model, con[i = 1:2, j = 2:3], i * x <= j + 1)
```

Thanks, the documentation made it clear to me how the @constraint macro works