Automated calculation of several example instances

I have modelled a MIP and want to solve several instances. So far, I have read and analysed each file from Excel by hand. I would like to automate this process as follows:
The different Excel files with the instances (instance 1, instance 2 etc). should be read from Excel into GAMS one after the other and the model should be solved for the corresponding instance in each case. The results of each instance are to be output in a separate row in Excel.

At the moment I read in an instance as follows:

[code]
$call gdxxrw Instance1.xlsx o=Data.gdx @text file
$gdxin Data.gdx
$load a, b, c, d
$gdxin
;
[\code]
Is there a way to automate this process? I’ve tried it with a loop, but unfortunately I can’t get any further. I would be grateful for a tip.

I would not do that in a loop. I would parameterize your model (the name of the Excel instance file) and then have a script that calls GAMS for each instance:

In your model you have

$if not set instance $abort Need to set --instance=filename
$call gdxxrw %instance%.xlsx o=Data.gdx @textfile
...

Then in a separate script (he I use a GAMS script, but you can do this in a cmd or sh script too):

$call gams mymodel --instance=instance1 fileStem=instance1 lo=2
$call gams mymodel --instance=instance2 fileStem=instance2 lo=2
...

Appending to the output with Excel is a little tricky. Perhaps, you can just write the results to a CSV file (that opens also nicely in Excel). With that you can use the GAMS put facility to append to your result file. So in your model you have, e.g.

file fr / results.csv /; fr.ap=1;
put fr '%instance%,' obj.l;

Make sure to remove the results.csv when you start a new set of runs. You could automate this by adding $call rm -f results.csv to the top of the script that runs the various instances.

Hope this helps,
-Michael

Hello,
thank you very much for your reply and your help.

I have changed my GAMS code as follows:

**GAMS Code: mymodel**
Sets
i
j
w/1*3/; **here is w the number of iterations (Instances)

Parameter:
A(i,j)
b(i);

$if not set instance $abort w --instance=filename
$call gdxxrw %instance%.xlsx o=Data.gdx @textfile
$gdxin Data.gdx
$load i, j, A,b
$gdxin
;

Binary Variable
x(i,j);

Free variable
z;

equations
Obj..
Eq1..
Eq2...

model mymodel /all/;

solve mymodel using mip maximizing z;

file fr / results.csv /; fr.ap=1;

put fr '%instance%,' z.l;

In a separate GAMS file called new1, I have the following code:

$call rm -f results.csv 
$call gams mymodel --instance=instance1 fileStem=instance1 lo=2
$call gams mymodel --instance=instance2 fileStem=instance2 lo=2
$call gams mymodel --instance=instance3 fileStem=instance3 lo=2

Unfortunately, when I now run ‘mymodel’, I get error messages. For example, in the line

$if not set instance $abort w --instance=filename

I would be very grateful if someone could explain to me what I did wrong or what I need to improve to make it work.

I guess you get this now when you run the model outside the new script. The easiest is to add $set instance instanceN before the $if not set instance ... line . But don’t forget to comment * $set instance instanceN then line when you run the batch script.

-Michael

Thank you very much for your help. Now it works the way I want it to.