Looping over file names

682 views
Skip to first unread message

Alison

unread,
Feb 24, 2009, 3:23:41 PM2/24/09
to gamsworld
Hi,

I've written a GAMS program using the xls2gms function to upload data
and the execute_unload function to save the final result. However, I
need to run the same GAMS code for 50 different countries, which means
I have 50 different xls input files.

Is there a way to loop over file names so that I can change the input
file and the output file each time (e.g. Input_file_CountryX.xls,
Results_CountryX.gdx) ?

A simplified version of my code is below.

Thanks for your help-
Alison

-----------------------
*Read data from external spreadsheet
$call =xls2gms i=Input_file_Country1.xls r1=Sets!set_i
o1=industries.set r2=Sets!set_o o2=occupations.set r3=Wages!matrixB
o3=matrixB.inc r4=Sets!MatrixA o4=Average.inc

set i /
$include industries.set
/;
set o /
$include occupations.set
/;
**************
*Define B from external spreadsheet
Table B(o,i) degree of belief
$include matrixB.inc
;
Parameter avg(i) average by industry/
$include Average.inc
/;
**************

Equations, Objective Function, etc.

**************
*Run the model to get results
Model WAGES /objective/;
Solve WAGES using dnlp minimizing find_min;
Display w.l, w.m;
**************
*Export results to Excel by creating a .gdx file
execute_unload "Results_Country1.gdx" w.L w.M
execute 'gdxxrw.exe Results_Country1.gdx var=w.L'

Gamsworld Admin

unread,
Feb 26, 2009, 7:42:05 AM2/26/09
to gamsworld
Alison,

you could use GAMS as a platform independent scripting language. For
example you could change your program to parametrize the file names:

Change the following lines:

$call =xls2gms i=Input_file_Country1.xls r1=Sets!set_i...
...
execute_unload "Results_Country1.gdx" w.L w.M
execute 'gdxxrw.exe Results_Country1.gdx var=w.L'

to

$call =xls2gms i=Input_file_%instance%.xls r1=Sets!set_i...
...
execute_unload "Results_%instance%.gdx" w.L w.M
execute 'gdxxrw.exe Results_%instance%.gdx var=w.L'

Now, when you call your GAMS program you supply the instance name by
adding '--instance=Country1' to the GAMS call. Create a second GAMS
model e.g. run.gms that contains the following information:

$call gams mymodel.gms --instance=Country1
$if errorlevel 1 $abort 'problems with instance Country1'
$call gams mymodel.gms --instance=Country2
$if errorlevel 1 $abort 'problems with instance Country2'
...
$call gams mymodel.gms --instance=Country50
$if errorlevel 1 $abort 'problems with instance Country50'

If you want to keep the log and lst file from the individual runs, use
GAMS parameters lo, o and lf:

$call gams mymodel.gms --instance=Country1 lo=2 o=Country1.lst
lf=Country1.log
$if errorlevel 1 $abort 'problems with instance Country1'
...

Moreover, you could use GAMS to create the model run.gms: Create
another GAMS model makerun.gms:

set i instances /1*50/;

file frun / run.gms /; put frun '* Run file to run ' card(i):0 '
instances of mymodel';
loop(i,
put / '$call gams mymodel.gms --instance=Country' i.tl:0 ' lo=2
o=Country' i.tl:0 '.lst lf=Country' i.tl:0 '.log'
/ "$if errorlevel 1 $abort 'problems with instance Country"
i.tl:0 "'";
);

Hope this helps,
Michael Bussieck - GAMSWorld Coordinator

Renger van Nieuwkoop

unread,
Mar 8, 2009, 6:58:49 AM3/8/09
to gams...@googlegroups.com
i Alison
You could use the put utility and run a loop over your set of countries:.
 
LOOP(countries,
 
* Generate an include file (countries.inc) with the code for the specific country with the PUT utility and using
 
* include the generated file with the code to import the country specific data
 
* Solve the model
 
* Generate an inlcude file (results.inc) with the code for the specific country with the PUT utility
 
* include the generated file
 
);
 
Hope this helps
 
Renger

--
Renger van Nieuwkoop

Alexander Strenge

unread,
Apr 30, 2015, 2:01:14 AM4/30/15
to gams...@googlegroups.com
is I try this - I got the message: "Starting execution - empty program"

When I start the model without the extra file and the line "$call gams MasterarbeitModel_instance.gms --instance=3" it is working.

What is wrong? The GDX-Input is working and refreshed but the model is not solved.

Alexander Strenge

unread,
May 5, 2015, 2:26:26 AM5/5/15
to gams...@googlegroups.com
I really need help here.

It's just not working and I don't know why. Is this loop "trick" just working with "=xls2gms" and NOT with gdxxrw?

Renger

unread,
May 5, 2015, 3:55:33 AM5/5/15
to gams...@googlegroups.com
Hi Alexander
Hard to say without your code.
Renger

Alexander Strenge

unread,
May 5, 2015, 11:30:57 AM5/5/15
to gams...@googlegroups.com
Hi Renger,

attached you will find my code.
I want to run this model with several input files and create several output files.

The input is described in EXCEL and should be read from one sheet:  line 10 for instance = 1 and from line 20 for instance = 2 and so on.

The same should happen for the Output. It should be written to one sheet:  line 10 for instance = 1 and to line 20 for instance = 2 and so on.


I want to work with "run.gms" and "makerun.gms" files like described here.


Model in GAMS:

*LCC MODEL
sets
i                work-brakedown structure
subi1(i)         all R&D processes
subi2(i)         all manufacturin processes
subi3(i)         all usage processes
subi4(i)         all EOL processes
;
alias (i,j);

parameters
* Parameters
balance(i)             balance for an activity i if alternative o is chosen
capacity(i,j)          max amount of products or pieces for activity j if alternative o is chosen
************************** PHASE: Research & Development *********************************************************************
******* VARIABLE COST TYPES **************************************************************************************************
c_RD_v1(j)           variable cost type 1 for activity j if alternative o is chosen in phase "Research & Development"
c_RD_v2(j)           variable cost type 2 for activity j if alternative o is chosen in phase "Research & Development"
c_RD_v3(j)           variable cost type 3 for activity j if alternative o is chosen in phase "Research & Development"
c_RD_v4(j)           variable cost type 4 for activity j if alternative o is chosen in phase "Research & Development"
c_RD_v5(j)           variable cost type 5 for activity j if alternative o is chosen in phase "Research & Development"
c_RD_v6(j)           variable cost type 6 for activity j if alternative o is chosen in phase "Research & Development"
******* FIX COST TYPES ******************************************************************************************************
c_RD_f1(j)           fix cost type 1 for activity j if alternative o is chosen in phase "Research & Development"
c_RD_f2(j)           fix cost type 2 for activity j if alternative o is chosen in phase "Research & Development"
c_RD_f3(j)           fix cost type 3 for activity j if alternative o is chosen in phase "Research & Development"
c_RD_f4(j)           fix cost type 4 for activity j if alternative o is chosen in phase "Research & Development"
c_RD_f5(j)           fix cost type 5 for activity j if alternative o is chosen in phase "Research & Development"
c_RD_f6(j)           fix cost type 6 for activity j if alternative o is chosen in phase "Research & Development"
************************** PHASE: MANUFACTURING *********************************************************************
******* VARIABLE COST TYPES **************************************************************************************************
c_M_v1(j)           variable cost type 1 for activity j if alternative o is chosen in phase "Manufacturing"
c_M_v2(j)           variable cost type 2 for activity j if alternative o is chosen in phase "Manufacturing"
c_M_v3(j)           variable cost type 3 for activity j if alternative o is chosen in phase "Manufacturing"
c_M_v4(j)           variable cost type 4 for activity j if alternative o is chosen in phase "Manufacturing"
c_M_v5(j)           variable cost type 5 for activity j if alternative o is chosen in phase "Manufacturing"
c_M_v6(j)           variable cost type 6 for activity j if alternative o is chosen in phase "Manufacturing"
******* FIX COST TYPES ******************************************************************************************************
c_M_f1(j)           fix cost type 1 for activity j if alternative o is chosen in phase "Manufacturing"
c_M_f2(j)           fix cost type 2 for activity j if alternative o is chosen in phase "Manufacturing"
c_M_f3(j)           fix cost type 3 for activity j if alternative o is chosen in phase "Manufacturing"
c_M_f4(j)           fix cost type 4 for activity j if alternative o is chosen in phase "Manufacturing"
c_M_f5(j)           fix cost type 5 for activity j if alternative o is chosen in phase "Manufacturing"
c_M_f6(j)           fix cost type 6 for activity j if alternative o is chosen in phase "Manufacturing"
************************** PHASE: USAGE *********************************************************************
******* VARIABLE COST TYPES **************************************************************************************************
c_U_v1(j)           variable cost type 1 for activity j if alternative o is chosen in phase "Usage"
c_U_v2(j)           variable cost type 2 for activity j if alternative o is chosen in phase "Usage"
c_U_v3(j)           variable cost type 3 for activity j if alternative o is chosen in phase "Usage"
c_U_v4(j)           variable cost type 4 for activity j if alternative o is chosen in phase "Usage"
c_U_v5(j)           variable cost type 5 for activity j if alternative o is chosen in phase "Usage"
c_U_v6(j)           variable cost type 6 for activity j if alternative o is chosen in phase "Usage"
******* FIX COST TYPES ******************************************************************************************************
c_U_f1(j)           fix cost type 1 for activity j if alternative o is chosen in phase "Usage"
c_U_f2(j)           fix cost type 2 for activity j if alternative o is chosen in phase "Usage"
c_U_f3(j)           fix cost type 3 for activity j if alternative o is chosen in phase "Usage"
c_U_f4(j)           fix cost type 4 for activity j if alternative o is chosen in phase "Usage"
c_U_f5(j)           fix cost type 5 for activity j if alternative o is chosen in phase "Usage"
c_U_f6(j)           fix cost type 6 for activity j if alternative o is chosen in phase "Usage"

************************** PHASE: END-OF-LIFE *********************************************************************
******* VARIABLE COST TYPES **************************************************************************************************
c_E_v1(j)           variable cost type 1 for activity j if alternative o is chosen in phase "End-of-Life"
c_E_v2(j)           variable cost type 2 for activity j if alternative o is chosen in phase "End-of-Life"
c_E_v3(j)           variable cost type 3 for activity j if alternative o is chosen in phase "End-of-Life"
c_E_v4(j)           variable cost type 4 for activity j if alternative o is chosen in phase "End-of-Life"
c_E_v5(j)           variable cost type 5 for activity j if alternative o is chosen in phase "End-of-Life"
c_E_v6(j)           variable cost type 6 for activity j if alternative o is chosen in phase "End-of-Life"
******* FIX COST TYPES ******************************************************************************************************
c_E_f1(j)           fix cost type 1 for activity j if alternative o is chosen in phase "End-of-Life"
c_E_f2(j)           fix cost type 2 for activity j if alternative o is chosen in phase "End-of-Life"
c_E_f3(j)           fix cost type 3 for activity j if alternative o is chosen in phase "End-of-Life"
c_E_f4(j)           fix cost type 4 for activity j if alternative o is chosen in phase "End-of-Life"
c_E_f5(j)           fix cost type 5 for activity j if alternative o is chosen in phase "End-of-Life"
c_E_f6(j)           fix cost type 6 for activity j if alternative o is chosen in phase "End-of-Life"
;
********************************************************************************
********************************************************************************
********************************************************************************
scalar
lotsize
;
lotsize = 200;

*Loading data from Excel'
$Call 'gdxxrw.exe i=RunTest_01.XLS cmerge=1 se=0 o=LCCModel_%instance%.gdx UpdLinks=3 index=GamsInput!B%instance%0'
$gdxin LCCModel_%instance%.gdx
$load i
$loadM i<subi1.dim1
$loadM i<subi2.dim1
$loadM i<subi3.dim1
$loadM i<subi4.dim1
$loadM o<capacity.dim1
$load subi1
$load subi2
$load subi3
$load subi4
$load capacity
$load balance
$load c_RD_v1
$load c_RD_v2
$load c_RD_v3
$load c_RD_v4
$load c_RD_v5
$load c_RD_v6
$load c_RD_f1
$load c_RD_f2
$load c_RD_f3
$load c_RD_f4
$load c_RD_f5
$load c_RD_f6
$load c_M_v1
$load c_M_v2
$load c_M_v3
$load c_M_v4
$load c_M_v5
$load c_M_v6
$load c_M_f1
$load c_M_f2
$load c_M_f3
$load c_M_f4
$load c_M_f5
$load c_M_f6
$load c_U_v1
$load c_U_v2
$load c_U_v3
$load c_U_v4
$load c_U_v5
$load c_U_v6
$load c_U_f1
$load c_U_f2
$load c_U_f3
$load c_U_f4
$load c_U_f5
$load c_U_f6
$load c_E_v1
$load c_E_v2
$load c_E_v3
$load c_E_v4
$load c_E_v5
$load c_E_v6
$load c_E_f1
$load c_E_f2
$load c_E_f3
$load c_E_f4
$load c_E_f5
$load c_E_f6
$gdxin
;
balance(i) = lotsize * balance(i);
capacity(i,j) = lotsize * capacity(i,j)
positive variables
* Variables (positive)
f(i,j)          flow from activity i to j
;
variables
* Objective variable to find minimum LCC
z               LCC
;
********************************************************************************
************** Equations *******************************************************
********************************************************************************
equations
* Equations to solve model for each alternative described in EXCEL
equa_cap(i,j)      capacity constraint for an edge from activity i to j for each alternative
equa_bal(i)        balance constraint to ensure that a product goes through the whole life-cycle
equa_objective     objective function to minimize the life-cycle costs for each alternative
;
equa_cap(i,j)..
f(i,j) =l= capacity(i,j);
equa_bal(i)..
sum(j, f(i,j)) - sum(j, f(j,i)) =e= balance(i);
equa_objective..
z =e= sum ((i,j),
f(i,j)*(c_RD_v1(j)+ c_RD_v2(j) + c_RD_v3(j) + c_RD_v4(j) + c_RD_v5(j) + c_RD_v6(j)) +
(f(i,j)/lotsize**2)*(c_RD_f1(j) + c_RD_f2(j) + c_RD_f3(j) + c_RD_f4(j) + c_RD_f5(j) + c_RD_f6(j)) +
f(i,j)*(c_M_v1(j) + c_M_v2(j) + c_M_v3(j) + c_M_v4(j) + c_M_v5(j) + c_M_v6(j)) +
(f(i,j)/lotsize**2)*(c_M_f1(j) + c_M_f2(j) + c_M_f3(j) + c_M_f4(j) + c_M_f5(j) + c_M_f6(j)) +
f(i,j)*(c_U_v1(j) + c_U_v2(j) + c_U_v3(j) + c_U_v4(j) + c_U_v5(j) + c_U_v6(j)) +
(f(i,j)/lotsize**2)*(c_U_f1(j) + c_U_f2(j) + c_U_f3(j) + c_U_f4(j) + c_U_f5(j) + c_U_f6(j)) +
f(i,j)*(c_E_v1(j) + c_E_v2(j) + c_E_v3(j) + c_E_v4(j) + c_E_v5(j) + c_E_v6(j)) +
(f(i,j)/lotsize**2)*(c_E_f1(j) + c_E_f2(j) + c_E_f3(j) + c_E_f4(j) + c_E_f5(j) + c_E_f6(j))
);

model LCC_MODEL /all/;
*with this option gams puts the results of this model into a file with the name LCC_MODEL_p.gdx
OPTION Savepoint = 1;
Option LP = Cplex; 
solve LCC_MODEL minimizing z using LP;
execute_unload 'GAMSOutput_%instance%.gdx';
execute 'GDXXRW.exe i=GAMSOutput_%instance%.gdx o=RunTest_01.XLS skipempty=0 zeroout=0 trace=0 index=GamsOutput!B%instance%0'

Thank you, 
Alexander 

Renger van Nieuwkoop

unread,
May 5, 2015, 11:42:12 AM5/5/15
to gams...@googlegroups.com

Can you also send the excel file (either here or to my personal email address)

Renger

--
You received this message because you are subscribed to the Google Groups "gamsworld" group.
To unsubscribe from this group and stop receiving emails from it, send an email to gamsworld+...@googlegroups.com.
To post to this group, send email to gams...@googlegroups.com.
Visit this group at http://groups.google.com/group/gamsworld.
For more options, visit https://groups.google.com/d/optout.

Renger van Nieuwkoop

unread,
May 5, 2015, 11:47:18 AM5/5/15
to gams...@googlegroups.com

Or better: send me a zip-file with all your stuff so I can check what is going on.

Renger

 

From: gams...@googlegroups.com [mailto:gams...@googlegroups.com] On Behalf Of Alexander Strenge
Sent: Dienstag, 5. Mai 2015 17:26
To: gams...@googlegroups.com
Subject: Re: Looping over file names

 

Hi Renger,

--

Renger van Nieuwkoop

unread,
May 5, 2015, 11:49:15 AM5/5/15
to gams...@googlegroups.com

Or better: send me a zip-file with all your stuff so I can check what is going on.

Renger

 

From: gams...@googlegroups.com [mailto:gams...@googlegroups.com] On Behalf Of Alexander Strenge
Sent: Dienstag, 5. Mai 2015 17:26
To: gams...@googlegroups.com
Subject: Re: Looping over file names

 

Hi Renger,

--

Alexander Strenge

unread,
May 5, 2015, 4:14:45 PM5/5/15
to gams...@googlegroups.com
The input and output via excel is working for one instance if I don't use %instance% and call gams via run.gms. I just don't understand how to run gams from another gams file and add a number or text via %instance% like here in the example.

The excel data has no data information. 

You don't have to run the model for me, I just want to understand the line the trick with %instance%
run.gms
Model_03.gms
RunTest_02.xls

Renger van Nieuwkoop

unread,
May 6, 2015, 1:33:39 AM5/6/15
to gams...@googlegroups.com

HI Alex

 

The trick with %instance% is that Gams replaces %instance% in your code with the value  of instance in the call ($call gams mymodel –instance=1), which works fine if I look at the output.

 

If you want to test your model you could add

 

$if not setglobal instance $set instance 1

 

to the model_03.gms-file. Then you could test the model, running model_03.gms directly without run.gms.

 

As you said, the excel file does not contain data, so I can’t check what is going on. If you want, I can check if you send me at least the gdx files (they shouldn’t be that big) for your test model.

 

Cheers

Alexander Strenge

unread,
May 6, 2015, 10:28:54 AM5/6/15
to gams...@googlegroups.com
Hi Renger,

thank you.

I know that the input gdx file is created but the model will not be solved and the (output) excel file is not changed. Do I have to type "execute" instead of "call"?

I sent you a working gams and excel file for one instance without the %instance% and run.gms file.


Alex
GAMS.zip

Renger van Nieuwkoop

unread,
May 6, 2015, 10:31:18 AM5/6/15
to gams...@googlegroups.com

The gdx file is missing

Cheers

Renger

--

Alexander Strenge

unread,
May 6, 2015, 11:48:09 AM5/6/15
to gams...@googlegroups.com
You don't need the gdx file. The excel file is containing data now.

Alexander Strenge

unread,
May 6, 2015, 11:48:56 AM5/6/15
to gams...@googlegroups.com
You just need the "Model_05.gms" and "RunTest_2D_1.XLS" in one folder and it should work with reading and also writing to excel


Am Mittwoch, 6. Mai 2015 16:31:18 UTC+2 schrieb Renger van Nieuwkoop:

Renger van Nieuwkoop

unread,
May 6, 2015, 11:49:42 AM5/6/15
to gams...@googlegroups.com

There is only one file: the model_05.gms file

Renger

Alexander Strenge

unread,
May 6, 2015, 11:57:54 AM5/6/15
to gams...@googlegroups.com
Not my day... Sorry! And thank you again for your help.

Alexander
GAMS2.zip

Renger van Nieuwkoop

unread,
May 6, 2015, 12:13:00 PM5/6/15
to gams...@googlegroups.com

Hi Alex

 

Runs fine on my computer. Here the output in my dos window.

What might happen is that you have the excel file open (see in the gdxutils.pdf for closing it using XLStalk.exe).

I deleted the results in the excel file and when I run the model, the sheet is filled with data. I adjusted the file bringing back “instance” and it also solves fine.

 

Iteration:     1   Dual objective     =          2000.035000

LP status(1): optimal

Cplex Time: 0.02sec (det. 0.51 ticks)

 

Optimal solution found.

Objective :        7200.160000

 

--- Restarting execution

--- Reading solution for model LCC_MODEL

--- GDX Point d:\Inbox\alex\LCC_MODEL_p.gdx

 

GDXXRW           24.4.1 r50296 Released Dec 20, 2014 VS8 x86 32bit/MS Windows

Input file : d:\Inbox\alex\GAMSOutput.gdx

Output file: d:\Inbox\alex\RunTest_2D_1.XLS

Substituting Index

Total time = 3171 Ms

*** Status: Normal completion

--- Job Model_05.gms Stop 05/06/15 17:58:46 elapsed 0:00:05.608

 

If you add trace = 3 to the gdx-line I get a more usable output for debugging.

 

GDXXRW           24.4.1 r50296 Released Dec 20, 2014 VS8 x86 32bit/MS Windows

Excel version 15.0

Input file : d:\Inbox\alex\GAMSOutput.gdx

Output file: d:\Inbox\alex\RunTest_2D_1.XLS

Substituting Index

Loading sheet GAMSOutput

Reading data from Excel: Width = 13  Height = 22

var=z.l Rng=GAMSResults!A4:B5

var=f.l Rng=GAMSResults!A6:BN100 rdim=1 cdim=1

Squeeze value :  0.00000000000000E+0000

Squeeze value :  0.00000000000000E+0000

Type Symbol       Dim     Sheet                Data          RowHeader     ColHeader    

Var  z              0     GAMSResults          A4:A4         --:--         --:--        

Clear range: A4:A4

Write range: A4:A4  CellCount = 1

Var  f              2     GAMSResults          B7:BN100      A7:A100       B6:BN6       

Clear range: A6:BN100

Write range: A7:A100  CellCount = 94

Write range: B6:BN6  CellCount = 65

Write range: B7:N19  CellCount = 169

Total time = 3234 Ms

*** Status: Normal completion

 

 

Hope this helps

Alexander Strenge

unread,
May 6, 2015, 12:23:06 PM5/6/15
to gams...@googlegroups.com
I guess you misunderstood me.
I know that the model is running fine. That was not the issue.

The issue is that my model is not solving if I use the %instance% approach.
Could you maybe send me the adjusted file back? 
"I adjusted the file bringing back “instance” and it also solves fine."

Alex

Renger van Nieuwkoop

unread,
May 6, 2015, 1:40:04 PM5/6/15
to gams...@googlegroups.com

Hi Alex

 

Now I understand what you want to do…

 

I assume that you have scenarios numbered 1 to 3 (as example).

Where to send the output in the excel file is defined in the index in the excel file.

Let us assume you have two lines for two variables you want to send to the output file in the index, starting ab B4 ending at F6, the next at B7  ending at B9 (both with headers rdim and cdim).

 

You could then do the following by adding this at the top of your model file

 

$if not setglobal instance $set instance 1

$setglobal endrow

 

** If I have 2 variables I use the instance

$eval instance 3 * %instance% + 1

$eval endrow  %instance% + 2

 

And then have your gdxxrw line like this

 

execute 'GDXXRW.exe i=GAMSOutput_%instance%.gdx o=RunTest_2D_1.XLS skipempty=0 index=GamsOutput!B%instance%:F%endrow % trace=3'

 

 

This would do the trick, if you run your run.gms with “instance” defined to be 1 and then 2 and 3.

 

I personally would do this using the gdxmerge utility (see gdx documentation) and then produce a pivot table where you can “scroll” easily through all scenarios.

 

Cheers

Alexander Strenge

unread,
May 6, 2015, 3:11:25 PM5/6/15
to gams...@googlegroups.com
I'm trying to explain my problem again:

I want to load parametres with a dimension like 3 or 5  (e.g. costs(p,c,a,i,j) ) from excel.
I will reduce the parametres by several loops (p, c, a) and optimize an objective variable z in a 2 dimension level for several p, c, a. (e.g. costs2(i,j))
I want to have these results of the 2 dimension optimization for each run as an output in excel.

Then I want to use the results in the outter loops p, c to find the minimum objective variable there of the 2 dimension results for each a, then for each c and then for each p.

So first I have the objective variable z(p,c,a) then z(p,c) then z(p) and then z.

M. O.

unread,
Apr 1, 2016, 6:26:22 AM4/1/16
to gamsworld

Hello,

I'm pretty new to GAMS, I have a model which gives a different solution each time it runs through its loop, therefore I would like to get the data of each solution in an excel-file. I'm using the Mavrots-algorithm AUGMECON.

I have my excel-sheet, copy the data of the solution into the file and finally rename the xlsx-file to %RUN%.xlsx. I would like to change the value of "RUN" each time it runs through the loop, but I'm not able to or don't know how. (First Run --> RUN=1,..., Last Run --> RUN=20)

One more question: Should I place my calculations for the output-data somewhere different?

I'd very much appreciate any help on this matter.

$STitle eps-constraint method

***NEU**************************************************************************
$if
not set RUN                  $set RUN        0
********************************************************************************

Set k1(k) the first element of k, km1(k) all but the first elements of k;
k1
(k)$(ord(k)=1) = yes; km1(k)=yes; km1(k1) = no;
Set kk(k)     active objective function in constraint allobj
Parameter
   rhs
(k)     right hand side of the constrained obj functions in eps-constraint
   maxobj
(k)  maximum value from the payoff table
   minobj
(k)  minimum value from the payoff table
Variables
   a_objval   auxiliary variable
for the objective function
   obj        auxiliary variable during the construction of the payoff table
Positive Variables
   sl
(k)      slack or surplus variables for the eps-constraints
Equations
   con_obj
(k) constrained objective functions
   augm_obj   augmented objective
function to avoid weakly efficient solutions
   allobj     all the objective functions
in one expression;

con_obj
(km1)..   z(km1) - dir(km1)*sl(km1) =e= rhs(km1);

* We optimize the first objective function and put the others as constraints
* the second term is for avoiding weakly efficient points
augm_obj
..
  sum
(k1,dir(k1)*z(k1))+1e-3*sum(km1,sl(km1)/(maxobj(km1)-minobj(km1))) =e= a_objval;

allobj
..  sum(kk, dir(kk)*z(kk)) =e= obj;

Model mod_payoff    / EMMA, allobj / ;
Model mod_epsmethod / EMMA, con_obj, augm_obj / ;

option limrow
=0, limcol=0;
option solprint
=off, solvelink=%solvelink.CallModule%;

Parameter
  payoff
(k,k)  payoff tables entries;
Alias(k,kp);

* Generate payoff table applying lexicographic optimization
loop
(kp,
  kk
(kp)=yes;
  repeat
    solve mod_payoff
using lp maximizing obj;
    payoff
(kp,kk) = z.l(kk);
    z
.fx(kk) = z.l(kk);
    kk
(k++1) = kk(k);
 
until kk(kp); kk(kp) = no;
* release the fixed values of the objective functions for the new iteration
  z
.up(k) = inf; z.lo(k) =-inf;
);
if (mod_payoff.modelstat<>%modelstat.Optimal% and mod_payoff.modelstat<>%modelstat.FeasibleSolution%, abort 'no feasible solution for mod_payoff');


display payoff
;
minobj
(k)=smin(kp,payoff(kp,k));
maxobj
(k)=smax(kp,payoff(kp,k));

$set fname p
.%gams.scrext%
File fx solution points from eps-method / "%gams.scrdir%%fname%" /;

$if
not set gridpoints $set gridpoints 10
Set g grid points /g0*g%gridpoints%/
    grid(k,g) grid
Parameter
    gridrhs(k,g) rhs of eps-constraint at grid point
    maxg(k) maximum point in grid for objective
    posg(k) grid position of objective
    firstOffMax, lastZero some counters
    numk(k) ordinal value of k starting with 1
    numg(g) ordinal value of g starting with 0;
lastZero=1; loop(km1, numk(km1)=lastZero; lastZero=lastZero+1); numg(g) = ord(g)-1;

grid(km1,g) = yes; /
/ Here we could define different grid intervals for different objectives
maxg
(km1) = smax(grid(km1,g), numg(g));
gridrhs
(grid(km1,g))$(%min%=dir(km1)) = maxobj(km1) - numg(g)/maxg(km1)*(maxobj(km1)- minobj(km1));
gridrhs
(grid(km1,g))$(%max%=dir(km1)) = minobj(km1) + numg(g)/maxg(km1)*(maxobj(km1)- minobj(km1));
display gridrhs
;

* Walk the grid points and take shortcuts if the model becomes infeasible
posg
(km1) = 0;
repeat
  rhs
(km1) = sum(grid(km1,g)$(numg(g)=posg(km1)), gridrhs(km1,g));
  solve mod_epsmethod maximizing a_objval
using lp;
 
if (mod_epsmethod.modelstat<>%modelstat.Optimal%,  // not optimal is in this case infeasible
    lastZero
= 0; loop(km1$(posg(km1)>0 and lastZero=0), lastZero=numk(km1));
    posg
(km1)$(numk(km1)<=lastZero) = maxg(km1); // skip all solves for more demanding values of rhs(km1)
 
else
    loop
(k, put fx z.l(k):12:2); put /);

* Proceed forward in the grid
  firstOffMax
= 0;
  loop
(km1$(posg(km1)<maxg(km1) and firstOffMax=0), posg(km1)=posg(km1)+1; firstOffMax=numk(km1));
  posg
(km1)$(numk(km1)<firstOffMax) = 0;

**********************************************************

*All equations for output-data.

* Create GDX files

*execute_UNLOAD 'all.gdx';

execute_UNLOAD
'res.gdx'
r_s
r_r
r_r2
r_capa
r_capa2
r_gene
r_vf
r_vf2
r_ms
r_ms2
r_tec
r_sto
r_NTC
r_set
r_set2
r_mo
r_mod
;

execute_UNLOAD
'res_mv.gdx'
r_vf3
r_mod2
;

execute_UNLOAD
'res_t.gdx'
r_tG
r_tP
r_day_year
r_week_year
r_hour_day
;


* Write put files for excel export

$onecho
> res.txt
par
=r_s          rng=sys!a1              cdim=2
par
=r_r          rng=countries!a1        cdim=2
par
=r_r2         rng=c2!a1               cdim=2
par
=r_vf         rng=vf!a1               cdim=2
par
=r_vf2        rng=vf!n1               cdim=2
par
=r_capa       rng=capa!a1             cdim=2
par
=r_capa2      rng=ca2!a1              cdim=2
par
=r_ms         rng=ms!a1               cdim=2
par
=r_ms2        rng=ms!n1               cdim=2
par
=r_gene       rng=gene!a1             cdim=2
par
=r_tec        rng=tec!a1              cdim=2
par
=r_sto        rng=storage!a1          cdim=2
par
=r_NTC        rng=NTC!a1              cdim=2
par
=r_set        rng=p-setting!a1        rdim=1
par
=r_set2       rng=p2!a1               rdim=1
par
=r_mod        rng=modelstats!a1
$offecho

$onecho
> res_mv.txt
par
=r_vf3        rng=out!a1              cdim=2
par
=r_mod2       rng=out!n1              cdim=2
$offecho

$onecho
> res_t.txt
par
=r_tG         rng=G!a3                rdim=1
par
=r_tP         rng=P!a3                rdim=1
par
=r_day_year   rng=day_year!a3         rdim=1
par
=r_week_year  rng=week_year!a3        rdim=1
par
=r_hour_day   rng=hour_day!a3         rdim=1
$offecho




$ontext
r_cal
r_cal2
r_cal3
r_cal4
-----
par
=r_mo         rng=merit-order!a20     rdim=2
par
=r_cal        rng=cal!a1              rdim=2
par
=r_cal2       rng=cal!a25             rdim=3
par
=r_cal3       rng=cal3!a1             rdim=4
par
=r_cal4       rng=cal4!a1             rdim=4
$offtext




* Load excel templates

execute
"move            res*.xlsx       °trash"
execute
"copy            °input\template_res.xlsx        res.xlsx"
execute
"copy            °input\template_res_mv.xlsx     res_mv.xlsx"
execute
"copy            °input\template_res_t.xlsx      res_t.xlsx"



* export to excel files

execute
"GDXXRW.EXE      res.gdx         EpsOut=0        o=res.xlsx      @res.txt"
execute
"GDXXRW.EXE      res_mv.gdx      EpsOut=0        o=res_mv.xlsx   @res_mv.txt"
execute
"GDXXRW.EXE      res_t.gdx       EpsOut=0        o=res_t.xlsx    @res_t.txt"



* Rename XLSX and GDX files


execute
"ren             res.xlsx                %RUN%.xlsx"
execute
"ren             res_mv.xlsx             %RUN%_mv.xlsx"
execute
"ren             res_t.xlsx              %RUN%_t.xlsx"
execute
"ren             all.gdx                 %PURPOSE%_%YDIM%_%HOURS%_%WINDPROFILE%_%SOLARPROFILE%_%SDIM%.gdx"


* Open excel file (for programming and testing)

$IF
%OPEN% == "1"
execute
"XLSTALK -o                              %PURPOSE%_%YDIM%_%HORIZON%_%HOURS%_%WINDPROFILE%_%SOLARPROFILE%_%SDIM%.xlsx"

********************************************************

until sum(km1$(posg(km1)=maxg(km1)),1)=card(km1) and firstOffMax=0;
putclose fx
; // close the point file

* Get unique solutions from the point file using some Posix Tools (awk, (g)sort, uniq) that come with GAMS
$set awkscript awk
.%gams.scrext%
file fa
/ "%gams.scrdir%%awkscript%" /; put fa 'BEGIN { printf("Table solutions(*,*)\n$ondelim\nsol';
loop
(k, put ',' k.tl:0); putclose '\n"); }' / '{ print NR,$0 }' / 'END { print ";" }';
$if    
%system.filesys% == UNIX execute 'cd "%gams.scrdir%" && sort %fname% | uniq | awk -f %awkscript% > g.%gams.scrext% && gams g.%gams.scrext% o=gx.%gams.scrext% lo=0 gdx=soleps';
$if NOT
%system.filesys% == UNIX execute 'cd "%gams.scrdir%" && gsort %fname% | uniq | awk -f %awkscript% > g.%gams.scrext% && gams g.%gams.scrext% o=gx.%gams.scrext% lo=0 gdx=soleps';
execute
'mv -f "%gams.scrdir%soleps.gdx" .';

Set sol Solutions /1*20/; Parameter solutions(sol,k) Unique solutions;
execute_load
'soleps', solutions; display solutions;

*$exit



Reply all
Reply to author
Forward
0 new messages