sasoptpy.actions.for_loop

for_loop(*args)[source]

Creates a for-loop container to be executed on the server

Parameters
argssasoptpy.abstract.Set objects

Any number of sasoptpy.abstract.Set objects can be given

Returns
set_iteratorsasoptpy.abstract.SetIterator, sasoptpy.abstract.SetIteratorGroup

Set iterators to be used inside for-loop

Notes

For tasks that can be run concurrently, consider using sasoptpy.actions.cofor_loop()

Examples

Regular for loop:

>>> with so.Workspace('w') as w:
>>>     r = so.exp_range(1, 11)
>>>     x = so.VariableGroup(r, name='x')
>>>     for i in for_loop(r):
>>>         x[i] = 1
>>> print(so.to_optmodel(w))
proc optmodel;
    var x {{1,2,3,4,5,6,7,8,9,10}};
    for {o13 in 1..10} do;
        x[o13] = 1;
    end;
quit;

Nested for loops:

>>> from sasoptpy.actions import put_item
>>> with so.Workspace('w') as w:
>>>     for i in for_loop(range(1, 3)):
>>>         for j in for_loop(['a', 'b']):
>>>             put_item(i, j)
>>> print(so.to_optmodel(w))
proc optmodel;
    for {o2 in 1..2} do;
        for {o5 in {'a','b'}} do;
            put o2 o5;
        end;
    end;
quit;

Multiple set for-loops:

>>> with so.Workspace('w') as w:
>>>     r = so.Set(name='R', value=range(1, 11))
>>>     c = so.Set(name='C', value=range(1, 6))
>>>     a = so.ParameterGroup(r, c, name='A', ptype=so.number)
>>>     for (i, j) in for_loop(r, c):
>>>         a[i, j] = 1
>>> print(so.to_optmodel(w))
proc optmodel;
    set R = 1..10;
    set C = 1..5;
    num A {R, C};
    for {o5 in R, o7 in C} do;
        A[o5, o7] = 1;
    end;
quit;