Friday, January 9, 2009

Sensitivity Analysis in a loop with GAMS/Cplex

> I have read the document but I don't
> see how I can do sensitivity analysis in a loop.

GAMS/Cplex writes sensitivity information to a text file (include file) and GAMS cannot read such a file at compile time. I.e. you cannot read a text file or include file to be specific inside a loop.

It would have been preferable in GAMS/Cplex would have been updated so that it can write sensitivity information to a GDX file (see also GAMS/Cplex wish list).

As a work around one can call a GAMS submodel from the GAMS main model. The submodel can then convert the include file to a GDX file, which then can be read by the main model. 

A complete example is below:


shows how to do sensitivity analysis inside a single
GAMS job by using GDX files.

Erwin Kalvelagen, december 2003, revised jan 2009


i canning plants / seattle, san-diego /
j markets / new-york, chicago, topeka / ;


a(i) capacity of plant i in cases
/ seattle 350
san-diego 600 /

b(j) demand at market j in cases
/ new-york 325
chicago 300
topeka 275 / ;

Table d(i,j) distance in thousands of miles
new-york chicago topeka
seattle 2.5 1.7 1.8
san-diego 2.5 1.8 1.4 ;

Scalar f freight in dollars per case per thousand miles /90/ ;

Parameter c(i,j) transport cost in thousands of dollars per case ;

c(i,j) = f * d(i,j) / 1000 ;

x(i,j) shipment quantities in cases
z total transportation costs in thousands of dollars ;

Positive Variable x ;

cost define objective function
supply(i) observe supply limit at plant i
demand(j) satisfy demand at market j ;

cost .. z =e= sum((i,j), c(i,j)*x(i,j)) ;

supply(i) .. sum(j, x(i,j)) =l= a(i) ;

demand(j) .. sum(i, x(i,j)) =g= b(j) ;

Model m /all/ ;

* set options to cplex writes an include file with sensitivity
* information
$onecho > cplex.opt
objrng all
rhsrng all


option lp=cplex;
Set rnglim /lo,up/;

* three different scenarios
set iter /iter1*iter3/;
parameter delta(iter);
delta(iter)$(ord(iter)>1) = 100;
display delta;

* write gams file that reads include file and
* writes a GDX file (specified on the command line)
$onecho > inc2gdx.gms
set i,j,rnglim
$gdxin sets.gdx
$load i j rnglim

* save the sets i,j,rnlim to gdx file sets.gdx
execute_unload 'sets.gdx',i,j,rnglim;

* parameters to hold ranges for equation DEMAND
parameter demandrange(j,rnglim);
parameter alldemandrange(iter,j,rnglim);

a(i) = a(i) + delta(iter);

SOLVE m USING LP minimizing z;

* store the sensitivity information in a GDX file
execute '=gams.exe inc2gdx.gms lo=3 gdx=dump.gdx';

* get data from GDX file
execute_load 'dump.gdx',demandrange=demandRNG;

alldemandrange(iter,j,rnglim) = demandrange(j,rnglim);

display alldemandrange;