The GLPK wiki discusses sorting - and I believe there is a typo in it.

Should the line:

        set ind{k in 1..card(I)} := setof{i in I: pos[i] = k} i;

really be:

        set ind{k in 1..card(I)} := setof{i in I: pos[i] = k-1} i;


A copy/paste of the article is below:


#  sorting_symbolic.mod - how to sort arrays in MathProg
#  based on code by Andrew Makhorin

#  Sometimes it is necessary to print parameters or variables in the
#  order of ascending or descending their values. Suppose, for example,
#  that we have the following subscripted parameter:

set I;

param a{i in I} := Uniform(2, 7);

#  If we print all its members:

printf{i in I} "a[%2s] = %g\n", i, a[i];

#  the output may look like follows:
#
#  a[a] = 2.64156
#  a[b] = 2.04798
#  a[c] = 2.14843
#  a[d] = 4.76896
#  a[e] = 6.09132
#  a[f] = 3.27780
#  a[g] = 4.06113
#  a[h] = 4.05898
#  a[i] = 6.63120
#  a[j] = 6.50318
#  a[k] = 3.46065
#  a[l] = 4.69845
#
#  However, we would like the parameter members to appear in the order
#  of ascending values.
#
#  Introduce the following auxiliary parameter:

param pos{i in I} :=
      card({j in I: a[j] < a[i] or a[j] = a[i] and j < i});

#  where pos[i] = k - 1 means that in the sorted list member a[i] has
#  k-th position, 1 <= k <= |I|. Then introduce another auxiliary
#  parameter:

set ind{k in 1..card(I)} := setof{i in I: pos[i] = k} i;

#  where ind[k] = {i} iff pos[k] = i.
#
#  Now, the following statement:

printf "\n";
printf{k in 1..card(I), l in ind[k]} "a[%2s] = %g\n", l, a[l];

#  prints the parameter members in the desired order:
#
#  a[b] = 2.04798
#  a[c] = 2.14843
#  a[a] = 2.64156
#  a[f] = 3.27780
#  a[k] = 3.46065
#  a[h] = 4.05898
#  a[g] = 4.06113
#  a[l] = 4.69845
#  a[d] = 4.76896
#  a[e] = 6.09132
#  a[j] = 6.50318
#  a[i] = 6.63120

solve;

data;
set I := a b c d e f g h i j k l;
#set I := 1 2 3 4 5 6 7 8 9 10 11 12;
end;


________________________________
This e-mail and any attachments may be confidential or legally privileged. If 
you received this message in error or are not the intended recipient, you 
should destroy the e-mail message and any attachments or copies, and you are 
prohibited from retaining, distributing, disclosing or using any information 
contained herein. Please inform us of the erroneous delivery by return e-mail. 
Thank you for your cooperation.
_______________________________________________
Help-glpk mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/help-glpk

Reply via email to