TRY NOW!
AMPL > >Sets and Indexing > >How can I apply AMPL’s ordered-set functions to pairs and higher-dimensional objects?

AMPL does not define any ordering on pairs, triples, or objects of higher dimension. Thus next, prev, and other functions that apply to objects in ordered sets cannot be applied to pairs, triples, or tuples of higher dimension. For the same reason, first, last and other functions of ordered sets may not be applied to multi-dimensional sets.

You may apply these functions to individual indices of a multi-dimensional parameter or variable, however. The following examples are from steelT2.mod (Figure 5-3) of the AMPL book:

subject to balance0 {j in PROD}:
   Make[j,first(WEEKS)] + inv0[j]
      = Sell[j,first(WEEKS)] + Inv[j,first(WEEKS)];
subject to balance {j in PROD, t in WEEKS: ord(t) > 1}:
   Make[j,t] + Inv[j,prev(t)] = Sell[j,t] + Inv[j,t];

Most models that involve both ordered and multidimensional sets can be handled by some variation of this approach.

Posted in: Sets and Indexing