The FOR statement is used in order to loop over a block of statements.
Syntax
The syntax is:
FOR ( i = start TO|DOWNTO end [By incr], statements; );
Warning: One cannot make declarations or define equations inside
a FOR statement.
Example 1
One can use FOR statements to control the SOLVE statement. For instance, consider the following bit of GAMS code that randomly searches for a global optimum of a nonconvex model:
scalar count ; for (i = 1 to 1000, x.l(j) = uniform(0,1) ; option bratio = 1 ; solve ml using nlp minimizing obj ; if (obj.l le globmin, globmin = obj.l ; globinit(j) = x.l(j) ; ) ; ) ;
In this example, a non-convex model is solved from 1000 random starting points, and the global solution is tracked.
Example 2
The following GAMS code is illegal since one cannot define equations inside a FOR statement.
For (s = 1 to 5 by 1, eq.. sum(i,x(i)) =g= 2 ; );
Example 3
The following GAMS code is illegal since one cannot make declarations inside a FOR statement.
For (s=1 to 5 by 1, scalar y ; y = 5 ; );