Help Regarding creating gdx/xls files inside a loop with different file name

Problems with modeling
Post Reply
shamik
User
User
Posts: 3
Joined: 4 years ago

Help Regarding creating gdx/xls files inside a loop with different file name

Post by shamik »

Dear All,
I want to create GDX or xls file with different names for different instances. Please find the sample code below.

parameter
sel(i,j,tt) selection parameter
bat(i,j,tt) batch size paramter
inv(k,tt) inventory paramter
end(i,j,tt) internal parameter
l;
$setglobal inc l

loop n$(ord(n) <= count1) do
l = ord(n);
loop i do
loop j do
loop t do
sel(i,j,t) = Xin(n,i,j,t);
bat(i,j,t) = Bin(n,i,j,t);
endloop;
endloop;
endloop;
loop k do
loop t do
inv(k,t) = Sin(n,k,t);
endloop
endloop;

display sel, bat, l, count1;

execute_unload 'ganttsolution_%inc%.gdx'sel bat inv end l;
endloop

however, each time it is creating only one file titled "ganttsolution_l.gdx", which is getting overwritten by the last values of the loop. I want to create different files for each instance of n. I have tried passing the value using 'n.tl' and 'n.te(n)' also, but that also didn't work.

Kindly advice me regarding this as soon as possible.

Thank you for your help.
User avatar
Renger
Posts: 639
Joined: 7 years ago

Re: Help Regarding creating gdx/xls files inside a loop with different file name

Post by Renger »

Hi
Why do you need separate gdx files? You could easily write a report parameter with dimension n and save them after all loops in a gdx file.

Code: Select all

parameter resultsloops(n,i,j,k,"Myresults");

loop(n, loop(i, loop(j, loop(k,
   solve mymodel...
   resultsloop(n,i,j,k,s,"Output") = X.L(s);
););););
* Write resultsloop to gdx file.
Cheers
Renger
____________________________________
Enjoy modeling even more: Read my blog on modeling at The lazy economist
shamik
User
User
Posts: 3
Joined: 4 years ago

Re: Help Regarding creating gdx/xls files inside a loop with different file name

Post by shamik »

Thank you Renger for your reply. However, I do need to create different gdx file (to be accurate .xls files) for the next procedure. I have tried put-utility facilities to create new directories, however, unable to place the .xls file in that directory.

put_utility 'exec' / 'mkdir ' 'test-':0 n.tl:0;
Execute 'gdxxrw Input=ganttData.gdx Output=GanttChart.xls @write.txt'

could you please help me with this ??

Thanks & Regards,
Shamik
User avatar
Renger
Posts: 639
Joined: 7 years ago

Re: Help Regarding creating gdx/xls files inside a loop with different file name

Post by Renger »

Hi Shamik
Perhaps this is an idea: generate an empty file "test.inc", so in compile phase Gams finds a file around and in the loop generate a new version of this file and include it.
If you save your data in the same gdx file in the loop, you can write the data to an excel file with the the name "results" plus the label of the iterator:

Code: Select all

$onecho  > test.inc
* Just empty file
$offecho

set n /n1/;
file test /test.inc/;
loop(n, 
    ....
        run mymodel and generate the results and save them to the file results.gdx
     ...
    put test;
    put 'Execute "gdxxrw Input=results.gdx Output=results,n.tl:0,'"';
    putclose test;
$include test.inc
);
Cheers
Renger
____________________________________
Enjoy modeling even more: Read my blog on modeling at The lazy economist
shamik
User
User
Posts: 3
Joined: 4 years ago

Re: Help Regarding creating gdx/xls files inside a loop with different file name

Post by shamik »

Hi, Renger
I have tried it. but it does not work. It says file can not be created as the filename has ':' in it. it does not recognize the index and take it as a string command.
I have tried this as well,

*put_utilities 'exec' / 'mkdir ' 'test-':0 n.tl:0;
*put_utilities 'ren' / 'test-':0 n.tl:0 '%system.dirSep%test-':0 n.tl:0 '.xls':0 ;
*put 'gdxout ganttData.gdx @write.txt';

It creates separate .xls files in separate folders. however, in that files this line 'gdxout ganttData.gdx @write.txt' comes as a text. whether the following command does not work.
EXECUTE 'gdxxrw Input=ganttData.gdx Output=GanttChart.xls @write.txt'

Thanks and regards,
Shamik
User avatar
Renger
Posts: 639
Joined: 7 years ago

Re: Help Regarding creating gdx/xls files inside a loop with different file name

Post by Renger »

Hi Shamik

Try another way: save the results in "results" indexed over the loops and write a stand-alone file which assigns the values of each loop to a parameter without the index (resultsi) with the put command. After the run that produces your results, you can then run the stand-alone file
Here is an example. You will have to adjust it, so it saves the files where you want it to be.

Code: Select all

parameter results, resultsi;
set  i /i1*i4/;

.. run your model and produce the results saved in results(i);

execute_unload 'results.gdx', results; 

file test /writeresults.gms/;
put test;
put 'parameter results, resultsi;'/;
put 'set i /i1*i4/;'/;
put 'execute_unload "results.gdx,", ', 'results;'/; 
loop(i, 
   ...
   put 'resultsi = results("':0, i.tl:0, '");' /;
   put 'execute_unload "results':0, i.tl:0,'.gdx", ',  'resultsi;' /; 
   put 'execute "gdxxrw Input=results':0, i.tl:0, '.gdx Output= test-':0, i.tl:0 ,'.xlsx"'/;
);
This code produces the stand alone file with the following code in it:

Code: Select all

parameter results, resultsi;
set i /i1*i4/;
execute_unload "results.gdx,", results;
resultsi = results("i1");
execute_unload "resultsi1.gdx", resultsi;
execute "gdxxrw Input=resultsi1.gdx Output= test-i1.xlsx"
resultsi = results("i2");
execute_unload "resultsi2.gdx", resultsi;
execute "gdxxrw Input=resultsi2.gdx Output= test-i2.xlsx"
resultsi = results("i3");
execute_unload "resultsi3.gdx", resultsi;
execute "gdxxrw Input=resultsi3.gdx Output= test-i3.xlsx"
resultsi = results("i4");
execute_unload "resultsi4.gdx", resultsi;
execute "gdxxrw Input=resultsi4.gdx Output= test-i4.xlsx"
Hope this helps

Renger
____________________________________
Enjoy modeling even more: Read my blog on modeling at The lazy economist
Post Reply