In power generation analysts often want to see a Load Duration Curve (http://en.wikipedia.org/wiki/Load_duration_curve). This involves sorting, a task that is not always immediately obvious how to do in GAMS. Here is some optimized code to perform the creation of a load curve:
It uses some obscure GAMS syntax. The advantage is that they are fast but obviously this code is difficult to read: it is no longer intuitive what is happening here. The resulting graphs in the GAMS IDE and in Excel are:alias (st,month,m);
* map (d,tp) onto a 1 dim set k.
alias (d,day);
alias (tp,tradingperiod);
set k /k1*k10000/;
parameter NatlLoadByHH(month,day,tradingperiod);
parameter NatlLoadByHHsorted(month,k);
set dt(day,tradingperiod);
dt(d,tp)$sum(m,NatlLoadByHH(m,d,tp)) = yes;
set map(day,tradingperiod,k);
option map(dt:k);
set monthsub(month) 'only months that are really used';
monthsub(m)$(sum(dt,NatlLoadByHH(m,dt)))=yes;
* gdxrank only sorts one dimensional parameters so we use mapping
parameter unsorted(k),sorted(k);
parameter indx(k);
$libinclude rank
loop(monthsub(m),
unsorted(k) = sum(map(dt,k),NatlLoadByHH(m,dt));
$ libinclude rank unsorted k indx
sorted(k + (card(k) + 1 - indx(k) - ord(k)) ) = unsorted(k);
NatlLoadByHHsorted(m,k) = sum(map(dt,k),sorted(k));
);