how to define a range of summation

261 views
Skip to first unread message

Luke Li

unread,
Jun 7, 2011, 3:28:37 AM6/7/11
to AMPL Modeling Language
Dear all,

Does anyone know how to define a range of summation? Either in
variable declaration or constraint declaration.

For example, I have a linear variable V[t,n,k,i,j], then I need
another variable C[t,n,k,i,j], which is a summation of V[t,n,k,i,j]
from i-1 to i+1 and j-1 to j+1.

Put numbers in this.
When i = j = 2, V[t,n,2,2,2] is a linear variable, and C[t,n,k,2,2] is
dependent on V.
C[t,n, k,2,2]=V[t,n, k,1,1]+V[t,n,k,1,2]+V[t,n,k,1,3]+ V[t,n,k,2,1] +
V[t,n,k,2,2] + V[t,n,k,2,3]+ V[t,n,k,3,1]+V[t,n,k,3,2]V[t,n,k,3,3];

Any help would be appreciated.

Regards
Luke

Paul

unread,
Jun 7, 2011, 5:20:51 PM6/7/11
to am...@googlegroups.com
s.t. SumConstraint {t in T, n in N, k in K, i in I, j in J : (i-1) in I and (i+1) in I and (j-1) in J and (j+1) in J}: C[t,n,k,i,j] = sum {ii in I, jj in J : abs(ii-i) <= 1 and abs(jj-j) <= 1} V[t,n,k,i,j];

You didn't indicate what happens when i-1, i+1, j-1 or j+1 is not a valid index, so I eliminated those cases.

Paul
 

Luke Li

unread,
Jun 7, 2011, 11:19:36 PM6/7/11
to AMPL Modeling Language
Dear Paul,

Thanks for your help.

To follow up the point 'when i-1, i+1, j-1 or j+1 is not a valid
index'. I think that might not happen because it is already stated
'(i-1) in I and
(i+1) in I and (j-1) in J and (j+1) in J}'

A bit further into this problem.
I would like to make C[t,n,k,i,j} work as a sum of its previous value
to T, but another variable is at T.
i.e. subject to {T,N,K,I,J}:
When t=1
C[1,n,k,i,j]-X[1,n,k,i,j]>=0
when t=2
C[1,n,k,i,j]+C[2,n,k,i,j] - X[2,n,k,i,j]>=0
when t=3
C[1,n,k,i,j]+C[2,n,k,i,j]+ C[3,n,k,i,j] - X[3,n,k,i,j]>=0

Thanks in advance.

Regards
Luke

Robert Fourer

unread,
Jun 8, 2011, 11:48:36 AM6/8/11
to am...@googlegroups.com
It appears that you are looking for a formulation like the following:

subject to YourConstraint {t in T, n in N, k in K, i in I, j in J}:
sum {tt in 1..t} C[tt,n,k,i,j] >= X[t,n,k,i,j];

This assumes T is a set whose members are a sequence of numbers. If T is an
integer-valued parameter, then you want something like "t in 1..T" rather
than "t in T".

Bob Fourer
4...@ampl.com

Paul

unread,
Jun 8, 2011, 3:20:33 PM6/8/11
to am...@googlegroups.com
Sorry, there's a typo in my answer ... the last term should be V[t,n,k,ii,jj], not V[t,n,k,i,j].

Paul

Luke Li

unread,
Jun 8, 2011, 10:34:46 PM6/8/11
to am...@googlegroups.com
Thanks to Paul and Bob.
Your help is greatly appreciated. 
I got the formulation works now. 

Luke

Reply all
Reply to author
Forward
0 new messages