Set Looping Functions
We have mentioned the power of set-based modeling comes from the ability to apply an operation to all members of a set using a single statement. The functions in LINGO that allow you to do this are called set looping functions. If your models don't make use of one or more set looping functions, then you are missing out on the power of set-based modeling and, even worse, you're probably working too hard!
Set looping functions allow you to iterate through all the members of a set to perform some operation. There are currently four set looping functions in LINGO. The names of the functions and their uses are:
Function |
Function's Use |
The most powerful of the set looping functions, @FOR is primarily used to generate constraints over members of a set. @FOR may also be used in calc sections to assign values to attributes across the members of a set. |
|
Probably the most frequently used set looping function, @SUM computes the sum of an expression over all members of a set. |
|
Computes the minimum of an expression over all members of a set. |
|
Computes the maximum of an expression over all members of a set. |
|
Computes the product of an expression over all members of a set. |
The syntax for a set looping function is:
@function( setname [ ( set_index_list)
[ | conditional_qualifier]] : expression_list);
where @function corresponds to one of the four set looping functions listed in the table above. setname is the name of the set you want to loop over.
The set_index_list is optional. It is used to create a list of indices. Each index corresponds to one of the parent, primitive sets that form the set specified by setname. As LINGO loops through the members of the set setname, it will set the values of the indices in the set_index_list to correspond to the current member of the set setname.
The conditional_qualifier is optional, and may be used to limit the scope of the set looping function. When LINGO is looping over each member of setname, it evaluates the conditional_qualifier. If the conditional_qualifier evaluates to true, then the @function is performed for the set member. Otherwise, it is skipped.
The expression_list is a list of expressions that are to be applied to each member of the set setname. When using the @FOR function, the expression list may contain multiple expressions, separated by semicolons. These expressions will be added as constraints to the model. When using the remaining set looping functions (@SUM, @MAX, @MIN and @PROD), the expression list must contain one expression only. If the set_index_list is omitted, all attributes referenced in the expression_list must be defined on the set set name.
See the @SUM, @FOR, @MIN and @MAX and @PROD sections below for examples illustrating the use of set looping functions.