An indexing expression is an expression that describes a collection of tuples. A tuple is an ordered list of objects, which can contain one object, two (a pair), three (a triplet), four (a quartet), five (a quintet), etc.
In algebraic notation, collection of tuples are indicated by phrases such as this:
The collection of tuples indicated by this phrase are all pairs
Any set expression in an indexing expression can be preceded by an iterator of the resulting set. An iterator is basically an object that represents any and all members of a set:
There are two main occasions when we want to use iterators. First, when we want to filter out the tuples of an indexing expression according to a logical condition. Second, when subsequent expressions in the same scope of a given set expression will need to refer to the members of the resulting set of that expression.
In the indexing expression above, the iterators are only needed because we want to filter out
the resulting pairs
We also use iterators to refer to the members of the set in subsequent expressions in the same scope, as exemplified below:
The above expression returns all pairs
Indexing expressions are used for a variety of different purposes. We highlight five common use cases in this section.
In declaration statements, indexing expressions are used to declare collections of model entities. We can have collections of any model entity: parameters, sets, variables, objective functions, contraints and more (see Model Entities).
The above example shows a parameter collection declaration. The indexing expression indicates that the collection is indexed over all members os set E, meaning that for every member e of that set we have a cost[e] associated with it.
Math expression reduction operators, such as
In the above example you can see the AMPL expression and the same expression in algebraic notation.
The indexing expression implements the part underneath the
In for-loops, indexing expressions are used to indicate what are the tuples associated with each iteration of the loop.
The above example shows a loop that will iterate over the set
Some commands, such as
In general, iterated commands are shorthands for for-loops with a single statement. For instance, the above example is a more compact version of the following loop:
This general rule does not apply to printing commands, whose iterated versions don't produce the exact same result as its equivalent for-loop.
Indexing expressions are set defining expressions themselves, and can appear wherever set expressions are required. The set defined by an indexing expression is the set of tuples indicated in it.
In the above example, we are declaring the set S and initializing it with the expression