help-glpk
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Help-glpk] sorting arrays in mathprog


From: glpk xypron
Subject: Re: [Help-glpk] sorting arrays in mathprog
Date: Wed, 02 Jun 2010 00:24:47 +0200

Hello Andrew,

the sorting example is quite helpful. Unfortunately it is only
applicable to numeric sets.

I changed your example a bit:
set ind{k in 1..card(I)} := setof{i in I: pos[i] = k} i;
...
printf{k in 1..card(I), l in ind[k]} "a[%s] = %g\n", l, a[l];

With this change sorting works both for symbolic and
numeric index sets I.

Best regards

Xypron



/* sorting_symbolic.mod - how to sort arrays in MathProg */

#  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 their values.
#
#  Introduce the following auxiliary parameter:

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

#  where pos[i] = k means that in the sorted list member a[i] would
#  have 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;

-- 
GRATIS für alle GMX-Mitglieder: Die maxdome Movie-FLAT!
Jetzt freischalten unter http://portal.gmx.net/de/go/maxdome01



reply via email to

[Prev in Thread] Current Thread [Next in Thread]