# RHS and LHS inconsistency in indexing over constraints

**URL:** <https://discourse.julialang.org/t/rhs-and-lhs-inconsistency-in-indexing-over-constraints/41409>\
**Category:** Optimization (Mathematical)\
**Tags:** question, jump\
**Created:** [June 14, 2020, 9:17pm UTC](https://discourse.julialang.org/t/rhs-and-lhs-inconsistency-in-indexing-over-constraints/41409 "2020-06-14T21:17:13Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![barguzin](https://avatars.discourse-cdn.com/v4/letter/b/a9adbd/32.png) [@barguzin](https://discourse.julialang.org/u/barguzin)\
**Post date:** [June 14, 2020, 9:17pm UTC](https://discourse.julialang.org/t/rhs-and-lhs-inconsistency-in-indexing-over-constraints/41409/1 "2020-06-14T21:17:13Z")

</div>

Hi folks,

This is my first time using Julia for math optimization (was previously running Xpress FICO).  
I am trying to run a relatively easy anti-covering model. But I am running into issue with indices (or something else). I was hoping you could help me figure it out.

The following command:

```julia
@constraint(aclp, con[j in 1:length(jsites)], length(omega[j]) * x[j] + sum(x[k] for k in omega[j]) <= length(omega[j])) 

```

Produces erroneous constraints (see the part in **bold** ):  
con[1] : **8 x[1]** + x[2] + x[3] + x[5] + x[6] + x[8] + x[10] \<= **7.0**  
con[2] : **11 x[2]** + x[1] + x[3] + x[4] + x[5] + x[6] + x[8] + x[9] + x[10] + x[11] \<= **10.0**  
con[3] : **11 x[3]** + x[1] + x[2] + x[4] + x[5] + x[6] + x[8] + x[10] + x[11] + x[12] \<= **10.0**  
con[4] : **10 x[4]** + x[2] + x[3] + x[5] + x[6] + x[7] + x[8] + x[9] + x[11] \<= **9.0**  
con[5] : **13 x[5]** + x[1] + x[2] + x[3] + x[4] + x[6] + x[8] + x[9] + x[10] + x[11] + x[12] + x[13] \<= **12.0**

So for con[1] the command length(omega[j]) in LHS produces 8, when in actuality it should be 7 (RHS). This repeats for all other constraints.

To work around the issue I have to manually deduct 1 from length(jsites) in LHS.

This could me an indexing issues or something else, or maybe I am using the Julia wrong. The following code produces correct constraints.

```julia
@constraint(aclp, con[j in 1:length(jsites)], **(length(omega[j])-1)** * x[j] + sum(x[k] for k in omega[j]) <= length(omega[j])) 

```

con[1] : **7 x[1]** + x[2] + x[3] + x[5] + x[6] + x[8] + x[10] \<= **7.0**  
con[2] : **10 x[2]** + x[1] + x[3] + x[4] + x[5] + x[6] + x[8] + x[9] + x[10] + x[11] \<= **10.0**  
con[3] : **10 x[3]** + x[1] + x[2] + x[4] + x[5] + x[6] + x[8] + x[10] + x[11] + x[12] \<= **10.0**  
con[4] : **9 x[4]** + x[2] + x[3] + x[5] + x[6] + x[7] + x[8] + x[9] + x[11] \<= **9.0**  
con[5] : **12 x[5]** + x[1] + x[2] + x[3] + x[4] + x[6] + x[8] + x[9] + x[10] + x[11] + x[12] + x[13] \<= **12.0**

Full runnable code with data can be downloaded from [here](https://yadi.sk/d/bvm63QkQnoVAZw).

I checked different solvers and the error is the same, so this is not solver-related.  
I am running Julia v1.4 inside VS Code. JuMP v0.21.2. Windows 10.

---

<div class="post-metadata">

**Author:** ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)\
**Post date:** [June 15, 2020, 12:16am UTC](https://discourse.julialang.org/t/rhs-and-lhs-inconsistency-in-indexing-over-constraints/41409/2 "2020-06-15T00:16:22Z")

</div>

x-ref: [Constraint macro RHS and LHS with length(dict[i]) command returns different values · Issue #2254 · jump-dev/JuMP.jl · GitHub](https://github.com/jump-dev/JuMP.jl/issues/2254)

You should read: [Please read: make it easier to help you - #19](https://discourse.julialang.org/t/psa-make-it-easier-to-help-you/14757/19). In particular, it’s easier to help if you can provide a small example that we can copy-paste, rather than downloading and extracting a zip from a different site.

I’ll repeat my suggestion from the JuMP issue for posterity:

> `sum(x[k] for k in omega[j])` probably includes `x[j]` . Which would account for the extra coefficient in front of `x[j]` .

---

<div class="post-metadata">

**Author:** ![barguzin](https://avatars.discourse-cdn.com/v4/letter/b/a9adbd/32.png) [@barguzin](https://discourse.julialang.org/u/barguzin)\
**Post date:** [June 15, 2020, 1:33am UTC](https://discourse.julialang.org/t/rhs-and-lhs-inconsistency-in-indexing-over-constraints/41409/3 "2020-06-15T01:33:34Z")

</div>

Thanks for reposting, @odow!

I check the dictionary of omegas and it did include the reference to self. Thank you for pointing it out!  
I will make sure to follow the PSA guidelines next time I post.

Thanks again!
