rank04.gms : Repeated computation of percentiles within a loop

Description

Dear Prof Rutherford,

I am sorry to bother you again. If you recall I was having difficulty
with the ‘rank’ function. I wasn’t able to run it in loop and use the
95th percentile value as a constraint in the next run of the loop,
while also retaining a value of the 95th percentile for every
iteration of the loop. [model below].

In the loop $libInclude rank call I ask GAMS to place the ranked
values in pct, it only runs for 1 iteration before the ‘user error’ in
rank. Whereas if I ask gams to put the ranked values in pct2, it runs
for 2 iterations before saying ‘user error’.

This must mean that the rank function only allows pct to be
over-written once, after which it ‘fills up’ and generates a user
error? How can I over come this?

In your last email you though the problem was that the original values
are being over written. So I introduced xparam95(iter). I think rank
does not allow them to be overwritten.

I am only a novice and have been stuck on this for sometime, your help
will be invaluable.

Kind regards,
Ashar


Category : GAMS Data Utilities library


Main file : rank04.gms   includes :  rank04.gms

$title Repeated computation of percentiles within a loop (rank04,SEQ=138)

$onText
Dear Prof Rutherford,

I am sorry to bother you again. If you recall I was having difficulty
with the ‘rank’ function. I wasn’t able to run it in loop and use the
95th percentile value as a constraint in the next run of the loop,
while also retaining a value of the 95th percentile for every
iteration of the loop. [model below].

In the loop $libInclude rank call I ask GAMS to place the ranked
values in pct, it only runs for 1 iteration before the ‘user error’ in
rank. Whereas if I ask gams to put the ranked values in pct2, it runs
for 2 iterations before saying ‘user error’.

This must mean that the rank function only allows pct to be
over-written once, after which it ‘fills up’ and generates a user
error? How can I over come this?

In your last email you though the problem was that the original values
are being over written. So I introduced xparam95(iter). I think rank
does not allow them to be overwritten.

I am only a novice and have been stuck on this for sometime, your help
will be invaluable.

Kind regards,
Ashar
$offText

    
Set
   iter             'Iterations'                 / iter1*iter10   /
   week             'Weeks in the year'          / 1*52           /
   percentile       'Percentiles (all of them)'  / 1*100          /
   pctl(percentile) 'Percentiles to be computed' / 50, 75, 80, 95 /;

Parameter
   z(week)        'Values to be sorted'
   rnk(week)      'Rank values'
   pct(*)         'Percentiles to be computed (input) and those values (output)'
   pct0(*)        'Percentiles to be computed'
   pctval(iter,*) 'Percentile values in successive iterations';

* Generate a "permanent copy" of the percentiles to be computed.
* This will be used to initialize pct before each call to rank;
pct0(percentile)$pctl(percentile) = ord(percentile);

* Assume that a model solution delivers the values;
z(week) = uniform(0,1);

* Assign the percentile values to be computed here:
pct(pctl) = pct0(pctl);

display 'Here are the INPUT values of PCT0 and PCT prior to the call to rank:', pct0, pct;

$libInclude rank z week rnk pct

display 'Here are the values of PCT0 and PCT after the call to rank:', pct0,
        'Note that rank has changed the OUTPUT value of pct', pct;

* Do several iterations, computing percentiles in each step:

loop(iter,
*  Substitute a call to the NLP solver by a call to the random
*  number generator.  In many applications, this substitution
*  produces profoundly more sensible results.
*
*  solve catchment using nlp maximizing max;

   z(week) = uniform(0,1);

*  If you want to retrieve percentile values, you need to reassign
*  the percentiles that you wish to retrieve at this point in the
*  program.  If pct() were not reassigned at this point, the INPUT
*  values would correspond to the OUTPUTs from the previous call.

   pct(pctl) = pct0(pctl);

$  libInclude rank z week rnk pct

   pctval(iter,pctl) = pct(pctl);
);

display pctval;