|
Instances certainly arise where one wishes to include information from external programs into the current GAMS program. This cannot be done with $Include commands unless one uses save and restart since the $Include action only works at compile time and can only include a file that existed before the GAMS program began. Furthermore GAMS has not yet released general interface routines that can be called within programs written in other languages allowing one to extract and rewrite GDX files. However a trick may be employed to get around this where
| • | Data are saved using put for external file consumption |
| • | The external program is executed generating a potential include file |
| • | GAMS is executed from within the main GAMS program where in the subservient GAMS program |
| — | $include is used to bring in the include file data |
| — | Execute_unload is used to place those data in a GDX file |
| • | The main GAMS program loads those data using Execute_Load. |
Example:
Suppose for example I wish to invert a matrix and have a Delphi program (invert1.exe) for that. The first GAMS program (invert.gms) is as follows
set i /i1*i4 /;
alias(i,j);
table a(i,i)
i1 i2 i3 i4
i1 2 0 2 1
i2 0 1 1 1
i3 0 0 1 3
i4 1 0 0 1
file mymatrix;
put mymatrix;
mymatrix.pc=5;
put '';loop(j,put j.tl;); put /;
loop(i,put i.tl;loop(j,put a(i,j));put /);
putclose;
execute_unload 'mygdx.gdx',i
execute 'invert1 i=mymatrix.put o=myinverse.put';
execute 'gams inverse2';
parameter ainv(i,j)
execute_load 'mygdx.gdx', ainv;
display ainv;
This programs proceeds through several steps as color coded to the statements above.
| • | Writes a put file to pass the matrix to be inverted to the external program. |
| • | Write a GDX file that contains the sets needed by the second GAMS program. |
| • | Executes the external matrix inversion program (invert1.exe) which in turn generates a file in GAMS format which contains the inverse. |
| • | Executes another GAMS program which will incorporate the file containing the inverse and will write a GDX file as will be shown below. |
| • | Read the inverse from the GDX file with execute_load. |
Simultaneously in the second GAMS program (inverse2.gms) I
| • | Load in the sets I need from the main GAMS program. |
| • | Include the file with the inverse as generated by the external inverter program. |
| • | Save the inverse in a GDX file for inclusion in the original program. |
$gdxin mygdx.gdx
set i;
$load i
$gdxin
alias(i,j);
table ainv(i,j)
$include myinverse.put
display ainv;
execute_unload 'mygdx.gdx', ainv;
Finally in the Delphi program which is in the archive invert.zip I have three basic segments
| • | One that reads the information form GAMS, |
| • | One that does the work of the program inverting the matrix and |
| • | One that writes the information back to GAMS. |
Notes:
| • | This program allows one to include execution time information into GAMS using an $Include which is not ordinarily allowed. This is achieved by doing an execution time invocation of GAMS from within GAMS. This allows the second instance of GAMS to work with any files that have been created up to that point including the inverter created file with the inverse. |
| • | In turn then having the embedded GAMS program write to a GDX file and using Execute_Load in the original program allows the information to be put back in at execution time. |
|