User Tools

Site Tools


gams:structure_a_large_gams_model

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
gams:structure_a_large_gams_model [2021/05/06 22:10]
Atharv Bhosekar removed
— (current)
Line 1: Line 1:
-====== How do I structure a large GAMS model? ====== 
- 
-It makes sense to split a large GAMS model into different files in order to have a better overview of the GAMS program. A natural separation is to have the model algebra, input data and solution reporting in separate files. For example, it might be comfortable to know that the model algebra remains unchanged regardless of the modifications to data input file(s) and solution report file(s). Below you can find one suggestion that demonstrates the idea ({{:​gams:​main.zip|download}}). 
- 
-File main.gms: 
-<​code>​ 
-$ontext 
- A large model can be split up into parts in order to make the program 
- ​easier to maintain and debug. This can be done in different ways and in here 
- we use model library example trnsport.gms to illustrate one way. 
- 
- ​Master file "​main.gms"​ divides the execution into steps: 
- (1) model.gms : Define algebraic model independently from data 
- (2) input.gms : Prepare input data 
- (3) report.gms: Report solution 
-$offtext 
- 
-* GLOBAL SETTINGS 
-*) Set command line parameters 
-$set clp lo=%gams.lo% 
- 
-*) Compile model algebra. 
-*) Note that you can distribute the workfile .g00 instead of source file .gms 
-$call gams model.gms a=c s=model %clp% 
-*) abort on errors in all three "call gams" statements 
-$if errorlevel 1 $abort errors in model.gms 
- 
-* PREPARE INPUT DATA 
-*) Remove intermediate and old files (input.gdx, report.xlsx,​ ...) 
-$call rm -f solution.gdx 
-*) Read data (database, Excel, ...), run model and save workfile 
-$call gams input.gms r=model s=wf1 %clp% 
-$if errorlevel 1 $abort errors in input.gms 
- 
-* REPORT SOLUTION 
-$call gamside report.gms r=wf1 %clp% 
-$if errorlevel 1 $abort errors in report.gms 
-$log 
-$log --------- EXECUTION SUCCESSFUL --------- 
-$log 
-</​code>​ 
- 
-File model.gms 
-<​code>​ 
-* We need to define the data (empty data is ok) to allow data checks etc.. 
-$ onempty 
-  Sets 
-       ​i(*) ​  ​canning plants ​  / ​ / 
-       ​j(*) ​  ​markets ​         /  / ; 
-  Parameters 
-       ​a(i) ​  ​capacity of plant i in cases                    /    / 
-       ​b(j) ​  ​demand at market j in cases                     / ​   / 
-       ​d(i,​j) distance in thousands of miles                  /    / 
-       ​c(i,​j) transport cost in thousands of dollars per case /    /; 
- 
-  Scalar f  freight in dollars per case per thousand miles    / na / ; 
- 
-  Variables 
-       ​x(i,​j) ​ shipment quantities in cases 
-       ​z ​      total transportation costs in thousands of dollars ; 
- 
-  Positive Variable x ; 
- 
-  Equations 
-       ​cost ​       define objective function 
-       ​supply(i) ​  ​observe supply limit at plant i 
-       ​demand(j) ​  ​satisfy demand at market j ; 
- 
-  cost ..        z  =e=  sum((i,j), c(i,​j)*x(i,​j)) ; 
- 
-  supply(i) ..   ​sum(j,​ x(i,​j)) ​ =l=  a(i) ; 
- 
-  demand(j) ..   ​sum(i,​ x(i,​j)) ​ =g=  b(j) ; 
- 
-  Model transport /all/ ; 
- 
-* Data check: verify that input is acceptable, for example: 
-  abort$(smin(i,​ a(i))<​=0) '​Invalid input: Plant capacity is negative or zero'; 
- 
-  c(i,j) = f * d(i,j) / 1000 ; 
- 
-  solve transport min z using lp; 
-</​code>​ 
- 
-File input.gms: 
-<​code>​ 
-* We allow redefinition of sets, parameters, etc.. 
-$onmulti 
- 
-  Sets 
-       ​i ​  ​canning plants ​  / seattle, san-diego / 
-       ​j ​  ​markets ​         / new-york, chicago, topeka / ; 
- 
-  Parameters 
-       ​a(i) ​ capacity of plant i in cases 
-         / ​   seattle ​    350 
-              san-diego ​  ​600 ​ / 
-       ​b(j) ​ demand at market j in cases 
-         / ​   new-york ​   325 
-              chicago ​    300 
-              topeka ​     275  / ; 
- 
-  Table d(i,​j) ​ distance in thousands of miles 
-                    new-york ​      ​chicago ​     topeka 
-      seattle ​         2.5           ​1.7 ​         1.8 
-      san-diego ​       2.5           ​1.8 ​         1.4  ; 
- 
-  Scalar f  freight in dollars per case per thousand miles  /90/ ; 
- 
-* alternatively we could read the data from a GDX file 
-</​code>​ 
- 
-File report.gms: 
-<​code>​ 
-* use command line parameter r=wf1 to execute only file report.gms 
- 
- ​Display x.l, x.m ; 
- 
- ​Execute_unload '​solution';​ 
-</​code>​ 
-