# How to build a constraint with summation?

**URL:** <https://discourse.julialang.org/t/how-to-build-a-constraint-with-summation/41618>\
**Category:** Optimization (Mathematical)\
**Tags:** question, jump\
**Created:** [June 17, 2020, 6:20pm UTC](https://discourse.julialang.org/t/how-to-build-a-constraint-with-summation/41618 "2020-06-17T18:20:39Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![RaquelSantos](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raquelsantos/32/3668_2.png) [@RaquelSantos](https://discourse.julialang.org/u/RaquelSantos)\
**Post date:** [June 17, 2020, 6:20pm UTC](https://discourse.julialang.org/t/how-to-build-a-constraint-with-summation/41618/1 "2020-06-17T18:20:39Z")

</div>

I need to mount this restriction

![Restricao](https://global.discourse-cdn.com/julialang/original/3X/e/e/ee1186555fcfcc415a4dd076ad7da00925638ba6.jpeg)

But the following error is appearing when I compile the function:  
`syntax: incomplete: premature end of input`

I’m doing like this

```julia
using JuMP, Cbc
Model1 = Model(with_optimizer(Cbc.Optimizer)) 

a = 79
t = 18
w = 1

A = 1:79 
T = 1:18
Q = [8780; 11310; 8600; 12500; 925; 1918; 4026; 5076]
RHE = [150; 150; 150; 150; 150]
NE = [14; 53; 24; 58; 115]
OHE = [25; 25; 25; 25; 25]
WH = [2486; 1447; 795; 583; 232; 53; 11073; 5414] 

@variable(Model1,s[A,T], lower_bound=0)
@variable(Model1,x[A,T], lower_bound=0)

@constraint(Model1,sum(Q[a,w]*x[a,t]-sum(s[a,t].<=(RHE[w]+OHE[w])*NE[w]-WH[w,t] for a in A))

```

---

<div class="post-metadata">

**Author:** ![klaff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/klaff/32/7637_2.png) [@klaff](https://discourse.julialang.org/u/klaff)\
**Post date:** [June 17, 2020, 6:28pm UTC](https://discourse.julialang.org/t/how-to-build-a-constraint-with-summation/41618/2 "2020-06-17T18:28:30Z")

</div>

You appear to be missing a final parenthesis on the last line. May I ask what editor you use? Most have a feature to indicate matching pairs of parentheses, often as little bars that show under both parentheses if you place the cursor on one of them.

---

<div class="post-metadata">

**Author:** ![RaquelSantos](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raquelsantos/32/3668_2.png) [@RaquelSantos](https://discourse.julialang.org/u/RaquelSantos)\
**Post date:** [June 17, 2020, 6:38pm UTC](https://discourse.julialang.org/t/how-to-build-a-constraint-with-summation/41618/3 "2020-06-17T18:38:39Z")

</div>

@klaff  
I put the paraentesis before where it is being indicated and the error continues. I use Atom as an editor.

```julia
@constraint(Model1,sum(Q[a,w]*x[a,t]-sum((s[a,t]<= (RHE[w]+OHE[w])*NE[w]-WH[w,t] for a in A))))

```

---

<div class="post-metadata">

**Author:** ![anon37204545](https://avatars.discourse-cdn.com/v4/letter/a/439d5e/32.png) [@anon37204545](https://discourse.julialang.org/u/anon37204545)\
**Post date:** [June 17, 2020, 6:39pm UTC](https://discourse.julialang.org/t/how-to-build-a-constraint-with-summation/41618/4 "2020-06-17T18:39:15Z")

</div>

> [@klaff](#):
>
> You appear to be missing a final parenthesis on the last line. May I ask what editor you use? Most have a feature to indicate matching pairs of parentheses, often as little bars that show under both parentheses if you place the cursor on one of them.

Adding another `)` produces this error:

```julia
ERROR: LoadError: In `@constraint(Model1, sum(Q[a, w] * x[a, t] - sum((s[a, t] .<= (RHE[w] + OHE[w]) * NE[w] - WH[w, t] for a = A))))`: Constraints must be in one of the following forms:
       expr1 <= expr2
       expr1 >= expr2
       expr1 == expr2
       lb <= expr <= ub

```

I guess what he needs is something like this, although it also produces an out-of-index error.

```julia
@constraint(Model1, sum(Q[a,w]*x[a,t]) - sum(s[a,t]) <= (RHE[w] + OHE[w]) * NE[w] - WH[w,t])

```

@RaquelSantos do you want to put a constraint like the one I put above, just for every a in A?

---

<div class="post-metadata">

**Author:** ![RaquelSantos](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raquelsantos/32/3668_2.png) [@RaquelSantos](https://discourse.julialang.org/u/RaquelSantos)\
**Post date:** [June 17, 2020, 6:46pm UTC](https://discourse.julialang.org/t/how-to-build-a-constraint-with-summation/41618/5 "2020-06-17T18:46:53Z")

</div>

@anon37204545  
I put the constraint `for a in A`, because of the sums that are indexed to `a`, Do you know if it really is done like this?  
I’ve been trying to reproduce this retribution  
 ![Restricao](https://global.discourse-cdn.com/julialang/original/3X/e/e/ee1186555fcfcc415a4dd076ad7da00925638ba6.jpeg)

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [June 17, 2020, 6:48pm UTC](https://discourse.julialang.org/t/how-to-build-a-constraint-with-summation/41618/6 "2020-06-17T18:48:50Z")

</div>

The code is a little confusing:

- `a` is both a constant defined before and the elements you iterate with the comprehension `for a in A`.
- There is a `.<=` but both sides are scalars in that context, no?
- The constraint should be `left-hand-expression <= right-hand-expression` but the `<=` is inside a comprehension what makes no sense.

Seem to me that what is wanted is something like:

```julia
@constraint(Model1, sum(Q[a,w]*x[a,t] for a in A) - sum(s[a,t] for a in A) <= (RHE[w] + OHE[w]) * NE[w] - WH[w,t])

```

---

<div class="post-metadata">

**Author:** ![RaquelSantos](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raquelsantos/32/3668_2.png) [@RaquelSantos](https://discourse.julialang.org/u/RaquelSantos)\
**Post date:** [June 17, 2020, 7:53pm UTC](https://discourse.julialang.org/t/how-to-build-a-constraint-with-summation/41618/7 "2020-06-17T19:53:27Z")

</div>

@Henrique_Becker  
Yes, both sides are scalar  
Now the constraint has the following error: `Bounds Error: attempet to access 79 elements Array{Int64,1} at index [1.5]`

I think the function is correct, but there is an inconsistency in the size of the matrix that I cannot solve.

```julia
@constraint(Model1,sum(Q[a,w]*x[a,t] for a in A)-sum(s[a,t] for a in A)<=(RHE[w]+OHE[w])*NE[w]-WH[w,t])

```

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [June 17, 2020, 8:45pm UTC](https://discourse.julialang.org/t/how-to-build-a-constraint-with-summation/41618/8 "2020-06-17T20:45:59Z")

</div>

Running the code of the first post with the constraint I suggested in my last reply gives me:

```julia
ERROR: LoadError: BoundsError: attempt to access 8-element Array{Int64,1} at index [9, 1]

```

That comes from the fact that `Q` is a 1D array of 8 elements, and therefore `sum(Q[a,w]*x[a,t] for a in A)` will not work, `Q` needs to be a 2D array (a matrix) of `79x?` where `?` is at least `1` (but essentially goes up to any value `w` can assume). The same problem exists with `WH` that is also a 1D array of 8 elements but is indexed like `WH[w,t] for a in A` (remembering that `t` is 18). Replacing both `Q` and `WH` by `Q = ones(79, 1); WH = ones(1, 18)` makes the code execute with no error (but, obviously, probably with the wrong result).

The error message you are giving me say that you tried to index some array with a floating point. I cannot reproduce it with the code you posted here.

---

<div class="post-metadata">

**Author:** ![RaquelSantos](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raquelsantos/32/3668_2.png) [@RaquelSantos](https://discourse.julialang.org/u/RaquelSantos)\
**Post date:** [June 18, 2020, 4:14pm UTC](https://discourse.julialang.org/t/how-to-build-a-constraint-with-summation/41618/9 "2020-06-18T16:14:00Z")

</div>

@Henrique_Becker  
It really is showing this error that you put above. I don’t know how to resolve this restriction  
Thank you for your help.

The only thing I saw that is different from the code I put here, is `w` , which is actually `w = 5`, but I believe that this will not change anything in relation to the error

```julia
using JuMP, Cbc
Model1 = Model(with_optimizer(Cbc.Optimizer)) 

#size
a = 79 #activity
t = 18 #period of time
w = 5 #work center

A = 1:79 
T = 1:18 

#parameters
Q = [8780; 11310; 8600; 12500; 925; 1918; 4026; 5076]
RHE = [150; 150; 150; 150; 150]
NE = [14; 53; 24; 58; 115]
OHE = [25; 25; 25; 25; 25]
WH = [2486; 1447; 795; 583; 232; 53; 11073; 5414] 

@variable(Model1,s[A,T], lower_bound=0)
@variable(Model1,x[A,T], lower_bound=0) 
@constraint(Model1,sum(Q[a,w]*x[a,t] for a in A)-sum(s[a,t] for a in A)<=(RHE[w]+OHE[w])*NE[w]-WH[w,t])

```

---

<div class="post-metadata">

**Author:** ![chisquared](https://avatars.discourse-cdn.com/v4/letter/c/87869e/32.png) [@chisquared](https://discourse.julialang.org/u/chisquared)\
**Post date:** [June 18, 2020, 5:10pm UTC](https://discourse.julialang.org/t/how-to-build-a-constraint-with-summation/41618/10 "2020-06-18T17:10:03Z")

</div>

Try replacing your definition of `Q` with `Q = rand(79,18)` and `WH` with `WH = rand(5,18)`. Do you still get the same error?

Also, are you sure the error you’re getting isn’t something like `ERROR: LoadError: BoundsError: attempt to access 8-element Array{Int64,1} at index [1, 5]`? Note the `[1, 5]` at the end and not the `[1.5]` as you wrote in a previous post.

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [June 18, 2020, 7:58pm UTC](https://discourse.julialang.org/t/how-to-build-a-constraint-with-summation/41618/11 "2020-06-18T19:58:49Z")

</div>

Two things.

First, prefer the term constraint to restriction, I do believe you are instinctively translating “restrição” from Portuguese in your mind.

Second,

> The only thing I saw that is different from the code I put here, is `w` , which is actually `w = 5` , but I believe that this will not change anything in relation to the error

If @chisquared guess is correct, and the error is `[1, 5]` not `[1.5]`, then `w = 5` is exactly what is causing the error. The whole problem you are having is that the constants you are using (`Q`, `WH`, …) are incomplete, where their values come from? Seems clear to me that lists of 8 values cannot be indexed with `[79, 5]` or `[5, 18]`, there are not such positions in a simple 1D list of 8 values. Where come the constraint that you are trying to replicate? It does not describe all of `Q` and `WH` values?

---

<div class="post-metadata">

**Author:** ![RaquelSantos](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raquelsantos/32/3668_2.png) [@RaquelSantos](https://discourse.julialang.org/u/RaquelSantos)\
**Post date:** [June 29, 2020, 8:16pm UTC](https://discourse.julialang.org/t/how-to-build-a-constraint-with-summation/41618/12 "2020-06-29T20:16:00Z")

</div>

@chisquared

The error that appears is exactly this `BoundsError: attempt to access 8-element Array{Int64,1} at index [1,5]`

---

<div class="post-metadata">

**Author:** ![RaquelSantos](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raquelsantos/32/3668_2.png) [@RaquelSantos](https://discourse.julialang.org/u/RaquelSantos)\
**Post date:** [June 29, 2020, 8:28pm UTC](https://discourse.julialang.org/t/how-to-build-a-constraint-with-summation/41618/13 "2020-06-29T20:28:37Z")

</div>

@Henrique_Becker

All constants are columns in a table, `Q` is a column of 79 values ​​and `WH` is a column of 30 values, I tried to reduce the values ​​to put here in julia groups.  
All constants of the model are files in the .CSV that I am exporting to Julia

---

<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 29, 2020, 8:57pm UTC](https://discourse.julialang.org/t/how-to-build-a-constraint-with-summation/41618/14 "2020-06-29T20:57:04Z")

</div>

The issue is that

```nohighlight
julia> Q = [8780; 11310; 8600; 12500; 925; 1918; 4026; 5076]
8-element Array{Int64,1}:
  8780
 11310
  8600
 12500
   925
  1918
  4026
  5076

julia> Q[1, 5]
ERROR: BoundsError: attempt to access 8-element Array{Int64,1} at index [1, 5]
Stacktrace:
 [1] getindex(::Array{Int64,1}, ::Int64, ::Int64) at ./array.jl:745
 [2] top-level scope at REPL[2]:1

```

`Q` is a vector with one dimension of length `8`:

```julia
julia> size(Q)
(8,)

```

You can index it `Q[5, 1]` (fifth row, first column), but not `Q[1, 5]` (first row, fifth column).

Here is the Julia documentation for arrays: [Multi-dimensional Arrays · The Julia Language](https://docs.julialang.org/en/v1/manual/arrays/)

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [June 29, 2020, 9:17pm UTC](https://discourse.julialang.org/t/how-to-build-a-constraint-with-summation/41618/15 "2020-06-29T21:17:56Z")

</div>

If `Q` is a column of 79 values, then why it is indexed this way `sum(Q[a,w]*x[a,t] for a in A)` inside the constraint (where `A` is `1:79` and `w` is `5`)? If `Q` is a single column then it can be indexed with two indexes only if one of them is always `1`, like pointed by odow in his last reply.

---

<div class="post-metadata">

**Author:** ![chisquared](https://avatars.discourse-cdn.com/v4/letter/c/87869e/32.png) [@chisquared](https://discourse.julialang.org/u/chisquared)\
**Post date:** [June 29, 2020, 9:19pm UTC](https://discourse.julialang.org/t/how-to-build-a-constraint-with-summation/41618/16 "2020-06-29T21:19:34Z")

</div>

To paraphrase the previous two posts:

You are trying to access an element in the fifth column of a column vector.

That’s impossible, because column vectors only have one column, not five.

Setting `w = 5` changes everything, because that’s where you are essentially asking Julia to look in columns that don’t exist.
