# GAMS Support Wiki

### Site Tools

gams:the_sum_of_consecutive_four_elements_in_a_vector_should_be_less_than_10

# Differences

This shows you the differences between two versions of the page.

 gams:the_sum_of_consecutive_four_elements_in_a_vector_should_be_less_than_10 [2008/10/01 14:41]support gams:the_sum_of_consecutive_four_elements_in_a_vector_should_be_less_than_10 [2020/05/18 12:02] (current)Lutz Westermann Correct link to docu; cosmentics in the code; show ++ more explicit Both sides previous revision Previous revision 2020/05/18 12:02 Lutz Westermann Correct link to docu; cosmentics in the code; show ++ more explicit2008/10/01 14:41 support 2008/10/01 14:40 support created 2020/05/18 12:02 Lutz Westermann Correct link to docu; cosmentics in the code; show ++ more explicit2008/10/01 14:41 support 2008/10/01 14:40 support created Line 3: Line 3: Q: //Say a vector A has ten values (decision variables) Objective: Maximize the sum of A. How do I model this constraint: The sum of consecutive four elements in the vector should be less than 10.// Q: //Say a vector A has ten values (decision variables) Objective: Maximize the sum of A. How do I model this constraint: The sum of consecutive four elements in the vector should be less than 10.// - You can use the lag and lead operator ​(chapter 13.4 in the GAMS User'​s + You can use the [[https://​www.gams.com/​latest/​docs/​UG_OrderedSets.html#​UG_OrderedSets_LagLeadOperators|lag and lead operator]] as in the following example: - Guide) ​as in the following example: + <​code>​ <​code>​ - set i /​1*10/; ​alias (i,j); + Set i /​1*10/; ​Alias (i,j); - set iconseq(i,​j);​ + Set iconseq(i,​j);​ - scalar ​cnt; + Scalar ​cnt; loop(i, for(cnt=0 to 3, iconseq(i,​i+cnt) = yes)); loop(i, for(cnt=0 to 3, iconseq(i,​i+cnt) = yes)); display iconseq; display iconseq; - variable ​x(i); + Variable ​x(i); - equation ​e(i); + Equation ​e(i); e(i).. sum(iconseq(i,​j),​ x(i)) =l= 10; e(i).. sum(iconseq(i,​j),​ x(i)) =l= 10; ​ - If you want your constraint to interpret "​consecutive"​ in a circular manner, you will have to replace ''​+''​ in the ''​loop''​-statement by ''​++''​. + + If you want your constraint to interpret "​consecutive"​ in a circular manner, you will have to replace ''​+''​ in the ''​loop''​-statement by ''​++'' ​like this: + + <​code>​ + loop(i, for(cnt=0 to 3, iconseq(i,​i++cnt) = yes)); + 