Nested Set Looping Functions

The simple models shown in the last section use @FOR to loop over a single set. In larger models, you’ll encounter the need to loop over a set within another set looping function. When one set looping function is used within the scope of another, we call it nesting.

An example of a nested set looping function can be found in the Wireless Widgets shipping model. If you remember, each of WW's vendors had a demand for widgets that had to be met. The LINGO statement that enforces this condition is:

! The demand constraints;

  @FOR( VENDORS( J):

   @SUM( WAREHOUSES( I): VOLUME( I, J)) =

    DEMAND( J));

Specifically, for each vendor, we sum up the shipments going from all the warehouses to that vendor and set the quantity equal to the vendor's demand. In this case, we have nested an @SUM function within an @FOR function.

@SUM, @MAX, @MIN can be nested within any set looping function. @FOR functions, on the other hand, may only be nested within other @FOR functions.