gams:get_an_ordered_set_from_a_set_which_is_not_ordered

`Ord()`

only works on ordered sets. Sets with elements, which have been introduced earlier, may not be ordered. Below is an example, how to get an ordered set in such a situation.

Note: The functions `sameas`

, `.pos`

, and `.val`

will also work, if the set is not ordered.

set i ordered / 1*10 / j not ordered / 0*9 /; * j is not ordered since UEL (unique element) '0' which is first in j * enters the universe out of order (after 1 to 9). parameter ordj(j) faked ord(j); * this does not work * ordj(j) = ord(j); * this always works scalar cnt /0/; loop(j, cnt=cnt+1; ordj(j) = cnt); set first_j(j) first element of j last_j(j) last element of j; first_j(j) = ordj(j) = 1; last_j(j) = ordj(j) = card(j); * This has 1 as first and 0 as last element. If the actual order is of no * importance this is just fine display first_j, last_j; alias (*,u); * In case you need the actual order, the following piece of code does it cnt=0; loop((u,j)$sorteduels(u,j), cnt=cnt+1; ordj(j) = cnt); first_j(j) = ordj(j) = 1; last_j(j) = ordj(j) = card(j); * Now we have 0 as first and 9 as last element. display first_j, last_j;

Output:

... ---- 21 SET first_j first element of j 1 ---- 21 SET last_j last element of j 0 ---- 32 SET first_j first element of j 0 ---- 32 SET last_j last element of j 9

gams/get_an_ordered_set_from_a_set_which_is_not_ordered.txt · Last modified: 2008/10/15 12:17 by support