# How to change the RHS for a set of constraints and solve the problem iteratively using JuMP?

**URL:** <https://discourse.julialang.org/t/how-to-change-the-rhs-for-a-set-of-constraints-and-solve-the-problem-iteratively-using-jump/22069>\
**Category:** General Usage\
**Tags:** jump\
**Created:** [March 19, 2019, 7:40pm UTC](https://discourse.julialang.org/t/how-to-change-the-rhs-for-a-set-of-constraints-and-solve-the-problem-iteratively-using-jump/22069 "2019-03-19T19:40:23Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lonewolf92](https://avatars.discourse-cdn.com/v4/letter/l/87869e/32.png) [@Lonewolf92](https://discourse.julialang.org/u/Lonewolf92)\
**Post date:** [March 19, 2019, 7:40pm UTC](https://discourse.julialang.org/t/how-to-change-the-rhs-for-a-set-of-constraints-and-solve-the-problem-iteratively-using-jump/22069/1 "2019-03-19T19:40:23Z")

</div>

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.

> ```julia
> 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, …).

---

<div class="post-metadata">

**Author:** ![leethargo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/leethargo/32/6004_2.png) [@leethargo](https://discourse.julialang.org/u/leethargo)\
**Post date:** [March 19, 2019, 8:02pm UTC](https://discourse.julialang.org/t/how-to-change-the-rhs-for-a-set-of-constraints-and-solve-the-problem-iteratively-using-jump/22069/2 "2019-03-19T20:02:21Z")

</div>

Please try to format your code to make it more readable. This is explained in [PSA: make it easier to help you](https://discourse.julialang.org/t/psa-make-it-easier-to-help-you/14757).

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.

---

<div class="post-metadata">

**Author:** ![leethargo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/leethargo/32/6004_2.png) [@leethargo](https://discourse.julialang.org/u/leethargo)\
**Post date:** [March 19, 2019, 8:03pm UTC](https://discourse.julialang.org/t/how-to-change-the-rhs-for-a-set-of-constraints-and-solve-the-problem-iteratively-using-jump/22069/3 "2019-03-19T20:03:09Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Lonewolf92](https://avatars.discourse-cdn.com/v4/letter/l/87869e/32.png) [@Lonewolf92](https://discourse.julialang.org/u/Lonewolf92)\
**Post date:** [March 19, 2019, 11:00pm UTC](https://discourse.julialang.org/t/how-to-change-the-rhs-for-a-set-of-constraints-and-solve-the-problem-iteratively-using-jump/22069/4 "2019-03-19T23:00:36Z")

</div>

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

Thanks

---

<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:** [March 20, 2019, 2:10am UTC](https://discourse.julialang.org/t/how-to-change-the-rhs-for-a-set-of-constraints-and-solve-the-problem-iteratively-using-jump/22069/5 "2019-03-20T02:10:39Z")

</div>

You could just use anonymous constraints, [Constraints · JuMP](http://www.juliaopt.org/JuMP.jl/v0.19.0/constraints/#The-@constraint-macro-1), after which you can store the generated constraint references in any container you like.

---

<div class="post-metadata">

**Author:** ![Lonewolf92](https://avatars.discourse-cdn.com/v4/letter/l/87869e/32.png) [@Lonewolf92](https://discourse.julialang.org/u/Lonewolf92)\
**Post date:** [March 20, 2019, 4:49pm UTC](https://discourse.julialang.org/t/how-to-change-the-rhs-for-a-set-of-constraints-and-solve-the-problem-iteratively-using-jump/22069/6 "2019-03-20T16:49:54Z")

</div>

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

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

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

---

<div class="post-metadata">

**Author:** ![Lonewolf92](https://avatars.discourse-cdn.com/v4/letter/l/87869e/32.png) [@Lonewolf92](https://discourse.julialang.org/u/Lonewolf92)\
**Post date:** [March 20, 2019, 4:50pm UTC](https://discourse.julialang.org/t/how-to-change-the-rhs-for-a-set-of-constraints-and-solve-the-problem-iteratively-using-jump/22069/7 "2019-03-20T16:50:38Z")

</div>

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