Archive for diciembre 2012

How to read a excel file in GAMS


As a beginner, I've started to learn GAMS doing the examples published in “McCarl GAMS User Guide”. The first model shown in the guide is a simple optimization problem like this:

                                               
Writing that problem in GAMS:

SET   j  /maiz,trigo,algodon/
          i  /tierra,trabajo/;

PARAMETER
      c(j)  /maiz 109, trigo 90, algodon 115/
      b(i)  /tierra 100, trabajo 500/;

TABLE a(i,j)
*table must be aligned
             maiz    trigo     algodon
tierra       1        1         1
trabajo     6       4         8    ;

Placing the numbers on a table in the same code can be useful in some cases but in general we build the tables in programs like EXCEL:

    
If we want to import this table to GAMS, EXCEL must be installed in our system and the .xls file should be saved in “projdir” folder (in this case results.xls). Then we can use the GDXXRW tool. This tool converts .xls to .gdx files which can be read by GAMS. Writing the code:

$CALL GDXXRW.EXE results.xls par=data rng=sheet1!A1:D3

parameter data(i,j);
$gdxin results.gdx
$load data
display data


The first line allows to read results.xls and generates a .gdx file. par=data defines that the table is a parameter named "data". Finally we specify the range and the sheet that we want to read.
The second line defines the set of the parameter data.
The third line loads the .gdx file.
The last line loads the parameter.

And the entire code:

SET   j  /maiz,trigo,algodon/
          i  /tierra,trabajo/;

PARAMETER
      c(j)  /maiz 109, trigo 90, algodon 115/
      b(i)  /tierra 100, trabajo 500/;

$CALL GDXXRW.EXE results.xls par=data rng=sheet1!A1:D3
parameter data(i,j);
$gdxin results.gdx
$load data
display data

POSITIVE VARIABLES  x(j);

VARIABLES   BENEFICIO ;
EQUATIONS     objetivo ,  restriccion(i) ;
  objetivo.. BENEFICIO =e= SUM(J,(c(J))*x(J)) ;
  restriccion(i)..  SUM (J,data(i,J)*x(J))  =L= b(i);

MODEL RESALLOC /ALL/;
SOLVE RESALLOC USING LP MAXIMIZING BENEFICIO;

Good luck!


References:

Corong, Erwin (2007). “Input_Output_1.gms:  The accompanying GAMS code for the note Aggregating Input-Output Table in GAMS”. Poverty and Economic Policy (PEP) Research Network.

McCarl, B., et al. (2011). McCarl GAMS User Guide. GAMS Development Corporation.



Posted in , | Leave a comment
Con la tecnología de Blogger.

Search

Swedish Greys - a WordPress theme from Nordic Themepark. Converted by LiteThemes.com.