nested summations
nested summations
Hi,
I have a syntax problem about nested summations.
my code:
set face_number /1*8/;
set old_face_vector_length /1*5/;
parameter face_vectors(face_number, old_face_vector_length) /
$ondelim
$include face_vectors.txt
$offdelim
/;
parameter cost;
cost = sum((face_number, face_number),
sum(old_face_vector_length,
face_vector(face_number, old_face_vector_length)-
face_vector(face_number, old_face_vector_length)));
display cost;
The compiler says "Set is under control already" (With some testing I
found that the problem comes from sum( (face_number, face_number), ).
What I would like to do is to calculate the sum of distances between
any given face in face_vectors (which is is the form of [face_number]
[face_vector]). I want to do something like:
cost = 0;
for(int i = 1, i <= 8; i++){
for(int j = 1; j <=8; j++){
cost +=
sum(old_face_vector_length,
face_vector(i, old_face_vector_length)-
face_vector(j, old_face_vector_length)));
}
}
I suppose I might translate the code above into GAMS, but somehow I
wonder if there is a better way to write it because using external
indexes like i, j to enumerate the sets doesn't seem to be a good idea
when the langauge tries to support more power ways to enumerate sets.
Many thanks to any help.
Eunice Chen
--~--~---------~--~----~------------~-------~--~----~
To post to this group, send email to gamsworld@googlegroups.com
To unsubscribe from this group, send email to gamsworld+unsubscribe@googlegroups.com
For more options, visit this group at http://groups.google.com/group/gamsworld?hl=en
-~----------~----~----~----~------~----~------~--~---
Re: nested summations
Eunice,
Similar to your C style example you need a second index for
face_number. In GAMS terms we call this an 'alias'. So the following
piece of code will work and probably make it more readable:
alias (face_number,f1,f2);
cost = sum((f1, f2),
sum(old_face_vector_length,
face_vector(f1, old_face_vector_length)-
face_vector(f2, old_face_vector_length)));
alternatively you could write
cost = sum((f1, f2,old_face_vector_length),
face_vector(f1, old_face_vector_length)-
face_vector(f2, old_face_vector_length));
Hope this helps,
Michael Bussieck - GAMSWorld Coordinator
On Aug 22, 12:20 am, lweunice wrote:
> > Hi,
> > I have a syntax problem about nested summations.
> >
> > my code:
> >
> > set face_number /1*8/;
> > set old_face_vector_length /1*5/;
> >
> > parameter face_vectors(face_number, old_face_vector_length) /
> > $ondelim
> > $include face_vectors.txt
> > $offdelim
> > /;
> >
> > parameter cost;
> > cost = sum((face_number, face_number),
> > sum(old_face_vector_length,
> > face_vector(face_number, old_face_vector_length)-
> > face_vector(face_number, old_face_vector_length)));
> >
> > display cost;
> >
> > The compiler says "Set is under control already" (With some testing I
> > found that the problem comes from sum( (face_number, face_number), ).
> >
> > What I would like to do is to calculate the sum of distances between
> > any given face in face_vectors (which is is the form of [face_number]
> > [face_vector]). I want to do something like:
> >
> > cost = 0;
> > for(int i = 1, i > for(int j = 1; j > cost +=
> > sum(old_face_vector_length,
> > face_vector(i, old_face_vector_length)-
> > face_vector(j, old_face_vector_length)));
> > }
> >
> > }
> >
> > I suppose I might translate the code above into GAMS, but somehow I
> > wonder if there is a better way to write it because using external
> > indexes like i, j to enumerate the sets doesn't seem to be a good idea
> > when the langauge tries to support more power ways to enumerate sets.
> >
> > Many thanks to any help.
> >
> > Eunice Chen
--~--~---------~--~----~------------~-------~--~----~
To post to this group, send email to gamsworld@googlegroups.com
To unsubscribe from this group, send email to gamsworld+unsubscribe@googlegroups.com
For more options, visit this group at http://groups.google.com/group/gamsworld?hl=en
-~----------~----~----~----~------~----~------~--~---