@SUM Set Looping Function
In this example, we will construct several summation expressions using the @SUM function in order to illustrate the features of set looping functions in general, and the @SUM function in particular.
Consider the model:
MODEL:
SETS:
VENDORS: DEMAND;
ENDSETS
DATA:
VENDORS, DEMAND = V1,5 V2,1 V3,3 V4,4 V5,6;
ENDDATA
END
Each vendor of the VENDORS set has a corresponding DEMAND. We could sum up the values of the DEMAND attribute by adding the following expression after the ENDDATA statement:
TOTAL_DEMAND = @SUM( VENDORS( J): DEMAND( J));
LINGO evaluates the @SUM function by first initializing an internal accumulator to zero. LINGO then begins looping over the members in the VENDORS set. The set index variable J is set to the first member of VENDORS (i.e., V1) and DEMAND( V1) is then added to the accumulator. This process continues until all DEMAND values have been added to the accumulator. The value of the sum is then stored into the variable TOTAL_DEMAND.
Since all the attributes in our expression list (in this case, only DEMAND appears in the expression list) are defined on the index set (VENDORS), we could have alternatively written our sum as:
TOTAL_DEMAND = @SUM( VENDORS: DEMAND);
In this case, we have dropped the superfluous index set list and the index on DEMAND. When an expression uses this shorthand, we say the index list is implied. Implied index lists are not allowed when attributes in the expression list have different parent sets.
Next, suppose we want to sum the first three elements of the attribute DEMAND. We can use a conditional qualifier on the set index to accomplish this as follows:
DEMAND_3 = @SUM( VENDORS( J) | J #LE# 3: DEMAND( J));
The #LE# symbol is called a logical operator. This operator compares the operand on the left (J) with the one on the right (3), and returns true if the left operand is less-than-or-equal-to the one on the right. Otherwise, it returns false. Therefore, when LINGO computes the sum this time, it plugs the set index variable J into the conditional qualifier J #LE# 3. If the conditional qualifier evaluates to true, DEMAND ( J) will be added to the sum. The end result is LINGO sums up the first three terms in DEMAND, omitting the fourth and fifth terms, for a total sum of 9.
Note: | Before leaving this example, one subtle aspect to note in this last sum expression is the value that the set index J is returning. Note we are comparing the set index variable to the quantity 3 in the conditional qualifier J #LE# 3. In order for this to be meaningful, J must represent a numeric value. Because a set index is used to loop over set members, one might imagine a set index is merely a placeholder for the current set member. In a sense, this is true, but what set indices really return is the index of the current set member in its parent primitive set. The index returned is one-based. In other words, the value 1 is returned when indexing the first set member, 2 when indexing the second, and so on. Given that set indices return a numeric value, they may be used in arithmetic expressions along with other variables in your model. |