lemon-project-template-glpk

annotate deps/glpk/examples/sorting.mod @ 9:33de93886c88

Import GLPK 4.47
author Alpar Juttner <alpar@cs.elte.hu>
date Sun, 06 Nov 2011 20:59:10 +0100
parents
children
rev   line source
alpar@9 1 /* sorting.mod - how to sort arrays in MathProg */
alpar@9 2
alpar@9 3 /* Written in GNU MathProg by Andrew Makhorin <mao@gnu.org> */
alpar@9 4
alpar@9 5 # Sometimes it is necessary to print parameters or variables in the
alpar@9 6 # order of ascending or descending their values. Suppose, for example,
alpar@9 7 # that we have the following subscripted parameter:
alpar@9 8
alpar@9 9 set I := 1..12;
alpar@9 10
alpar@9 11 param a{i in I} := Uniform(2, 7);
alpar@9 12
alpar@9 13 # If we print all its members:
alpar@9 14
alpar@9 15 printf{i in I} "a[%d] = %g\n", i, a[i];
alpar@9 16
alpar@9 17 # the output may look like follows:
alpar@9 18 #
alpar@9 19 # a[1] = 2.64156
alpar@9 20 # a[2] = 2.04798
alpar@9 21 # a[3] = 2.14843
alpar@9 22 # a[4] = 4.76896
alpar@9 23 # a[5] = 6.09132
alpar@9 24 # a[6] = 3.27780
alpar@9 25 # a[7] = 4.06113
alpar@9 26 # a[8] = 4.05898
alpar@9 27 # a[9] = 6.63120
alpar@9 28 # a[10] = 6.50318
alpar@9 29 # a[11] = 3.46065
alpar@9 30 # a[12] = 4.69845
alpar@9 31 #
alpar@9 32 # However, we would like the parameter members to appear in the order
alpar@9 33 # of ascending their values.
alpar@9 34 #
alpar@9 35 # Introduce the following auxiliary parameter:
alpar@9 36
alpar@9 37 param pos{i in I} :=
alpar@9 38 1 + card({j in I: a[j] < a[i] or a[j] = a[i] and j < i});
alpar@9 39
alpar@9 40 # where pos[i] = k means that in the sorted list member a[i] would
alpar@9 41 # have k-th position, 1 <= k <= |I|. Then introduce another auxiliary
alpar@9 42 # parameter:
alpar@9 43
alpar@9 44 param ind{k in 1..card(I)} := sum{i in I: pos[i] = k} i;
alpar@9 45
alpar@9 46 # where ind[k] = i iff pos[k] = i.
alpar@9 47 #
alpar@9 48 # Now, the following statement:
alpar@9 49
alpar@9 50 printf{k in 1..card(I)} "a[%d] = %g\n", ind[k], a[ind[k]];
alpar@9 51
alpar@9 52 # prints the parameter members in the desired order:
alpar@9 53 #
alpar@9 54 # a[2] = 2.04798
alpar@9 55 # a[3] = 2.14843
alpar@9 56 # a[1] = 2.64156
alpar@9 57 # a[6] = 3.27780
alpar@9 58 # a[11] = 3.46065
alpar@9 59 # a[8] = 4.05898
alpar@9 60 # a[7] = 4.06113
alpar@9 61 # a[12] = 4.69845
alpar@9 62 # a[4] = 4.76896
alpar@9 63 # a[5] = 6.09132
alpar@9 64 # a[10] = 6.50318
alpar@9 65 # a[9] = 6.63120
alpar@9 66
alpar@9 67 end;