Loop Optimization
The Loop optimization checkbox on the General Solver tab:
is used to either enable or disable LINGO's loop optimization step. Loop optimization reformulates expressions containing set looping functions in order to make them more efficient, while maintaining mathematical equivalency. The end goal of loop optimization is to minimize the number of passes through the inner loop of any nested loops in an expression.
As an example, consider the following transportation model fragment, that just contains constraints for satisfying customer demand:
MODEL:
! A transportation problem fragment;
SETS:
WAREHOUSE /1..50/ : CAPACITY;
CUSTOMER /1..5000/ : DEMAND;
ROUTES( WAREHOUSE, CUSTOMER) : COST, VOLUME;
ENDSETS
! The demand constraints;
@FOR( CUSTOMER( J):
@SUM( ROUTES( I, J): VOLUME( I, J)) >= DEMAND( J)
);
END
Transportation Model Fragment with Inefficient Constraints
In the demand constraints expression there are two loops - an outer @FOR() over the CUSTOMER set and an inner @SUM() over the ROUTES set. As written, the inner loop must be executed 5000*50*5000=1.25 billion times. Note that a valid reformulation of these demand constraints would be:
! The demand constraints;
@FOR( CUSTOMER( J):
@SUM( WAREHOUSE( I): VOLUME( I, J)) >= DEMAND( J)
);
With the expression rewritten in this manner, the inner loop will now only be executed 50*5000 times, reducing the total number of passes by a factor of 5000. LINGO's loop optimizer seeks out such inefficient loops and, invisible to the user, rewrites them before the model gets passed to the generator. Of course, the end result of such reformulations are faster generation times. In fact, the speedup from reformulation can be quite dramatic in many cases. In this particular example, the reformulated version ran over 400 times faster in the model generator.
Note that in some cases, particularly with models that primarily contain sparse sets, loop optimization may actually increase runtimes. So, you should compare runtimes with, and without, the feature being enabled.
By default, the Loop optimization feature is disabled.