How to build a constraint with summation?

I need to mount this restriction

Restricao

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

I’m doing like this

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))

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.

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

@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))))

Adding another ) produces this error:

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.

@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?

@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

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:

@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])
2 Likes

@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.

@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])

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

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.

1 Like

@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

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

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.

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?

@chisquared

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

@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

The issue is that

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> 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

1 Like

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.

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.

1 Like