loop in GAMS for scenario generation in excel

十年热恋 提交于 2020-05-13 07:50:47

问题


I have an optimization model and I try to solve this model for different input files which I exactly call as "solve the model under different scenarios". For that reason, I need a loop to read the data from excel for each different sheet. Let me make myself clear: For example, in the image below we have a data with 4 scenarios and the sheet names are increasing one by one for each scenario

screenshot This data has to be read as a table for each scenario, like in the excel file.

I try to read the data from different excel sheets with a loop. Can I do that in Gams?

In GAMS I could take datas from excel like in the below but this is just for one scenario. I want to make a GAMS code that read data for all scenarios from excel sheets in a loop statement

Table   n(t,b)
$call =xls2gms r="nonbooked!A2:I9" i="excelveri.xlsx" o="nbooked.inc"
$include nbooked.inc
;

回答1:


The GAMS put_utility (https://www.gams.com/latest/docs/UG_Put.html#UG_Put_PutUtil) together with the tool GDXXRW (https://www.gams.com/latest/docs/T_GDXXRW.html) is the key for this problem. Here is a self contained (and hopefully self explanatory) example:

set i      / i1*i3 /
    j      / j1*j4 /
    sheets / Sheet1*Sheet3 /;

parameter data(i,j);
file fx; put fx;

* To make this example self contained, first prepare some data (using same mechanics as the reading)
loop(sheets,
* Create random data
  data(i,j) = uniformInt(0,9);
* Write data to GDX
  execute_unload 'data.gdx', data;
* Write GDX data to excel
  put_utility 'exec' / 'gdxxrw.exe data.gdx par=data rng=' sheets.tl:0 '!a1';
);

* Clear data to start fresh
data(i,j) = 0;

* Load data
loop(sheets,
* Write Excel data to GDX
  put_utility 'exec' / 'gdxxrw.exe data.xlsx par=data rng=' sheets.tl:0 '!a1';
* Load data from GDX
  execute_load 'data.gdx', data;
* Work with the data, just display as an example
  display data;
);

I hope that helps, Lutz



来源:https://stackoverflow.com/questions/42859215/loop-in-gams-for-scenario-generation-in-excel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!