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);alias(d,day);alias(tp,tradingperiod);setk /k1*k10000/;

parameterNatlLoadByHH(month,day,tradingperiod);parameterNatlLoadByHHsorted(month,k);

setdt(day,tradingperiod);

dt(d,tp)$sum(m,NatlLoadByHH(m,d,tp)) =yes;

* map (d,tp) onto a 1 dim set k.setmap(day,tradingperiod,k);optionmap(dt:k);

setmonthsub(month) 'only months that are really used';

monthsub(m)$(sum(dt,NatlLoadByHH(m,dt)))=yes;

* gdxrank only sorts one dimensional parameters so we use mappingparameterunsorted(k),sorted(k);parameterindx(k);

$libinclude rankloop(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));

);