1 /* sorting.mod - how to sort arrays in MathProg */
3 /* Written in GNU MathProg by Andrew Makhorin <mao@gnu.org> */
5 # Sometimes it is necessary to print parameters or variables in the
6 # order of ascending or descending their values. Suppose, for example,
7 # that we have the following subscripted parameter:
11 param a{i in I} := Uniform(2, 7);
13 # If we print all its members:
15 printf{i in I} "a[%d] = %g\n", i, a[i];
17 # the output may look like follows:
32 # However, we would like the parameter members to appear in the order
33 # of ascending their values.
35 # Introduce the following auxiliary parameter:
38 1 + card({j in I: a[j] < a[i] or a[j] = a[i] and j < i});
40 # where pos[i] = k means that in the sorted list member a[i] would
41 # have k-th position, 1 <= k <= |I|. Then introduce another auxiliary
44 param ind{k in 1..card(I)} := sum{i in I: pos[i] = k} i;
46 # where ind[k] = i iff pos[k] = i.
48 # Now, the following statement:
50 printf{k in 1..card(I)} "a[%d] = %g\n", ind[k], a[ind[k]];
52 # prints the parameter members in the desired order: