Calling another GAMS code during execution

Problems with modeling
Post Reply
mohamed.omer
User
User
Posts: 5
Joined: 2 years ago

Calling another GAMS code during execution

Post by mohamed.omer »

Greetings

I have two separate GAMS codes and I would like to call the second code during the execution of the first one multiple times. Note that, some of the results from the first code is used in find the solution of the second code. Is that possible in GAMS? And if so, are there any materials that elaborate on this point further?
User avatar
bussieck
Moderator
Moderator
Posts: 1033
Joined: 7 years ago

Re: Calling another GAMS code during execution

Post by bussieck »

It is possible on a "job" level. You can can export the result from the first code to a GDX file, execute the second GAMS program via the execute statement. The second job also exports its results to a GDX file. In first code (after the execute statement) can read in these results and continue.

-Michael
mohamed.omer
User
User
Posts: 5
Joined: 2 years ago

Re: Calling another GAMS code during execution

Post by mohamed.omer »

Many Thanks for your reply Michael,
I would really appreciate it if you could help me with the syntax for the above problem.
I have two codes: Optimization Code & Real Time Dispatch Cod
1st iteration:
1. Run the optimization code for 24hours. The code has 3 output variables.
2. Take the results of the 3 output variables and include them as parameters in the real-time dispatch and run the dispatch code for hour 1. The code has 1 output variable.
2nd iteration:
1. Run the optimization code for the remaining 23 hours. Use the result of the output variable of the real time dispatch code for hour 1 (from iteration 1) as initial values to one of the variables in the optimization code). The code has the same 3 output variables in iteration 1.
2. Take the results of the 3 output variables and include them as parameters in the real-time dispatch and run the dispatch code for hour 2. The code has the same 1 output variable as in iteration 1.
Repeat the above iterations for further 22 iterations (total of iteration is 24).

I already have the two codes (optimization & real-time dispatch) developed.

I would appreciate your help with the syntax of the code that will be used to call each code in each iteration and save the results during execution to be used in the other code and so on.
User avatar
bussieck
Moderator
Moderator
Posts: 1033
Joined: 7 years ago

Re: Calling another GAMS code during execution

Post by bussieck »

Beyond what I already wrote there is little I can do. If you share your code and annotate the various inputs and output, then perhaps I can add some code to make it more obvious.

-Michael
mohamed.omer
User
User
Posts: 5
Joined: 2 years ago

Re: Calling another GAMS code during execution

Post by mohamed.omer »

Greetings Mr. Michael,
Thank you for all the help you are extending to me.
As per your request I included all the notes for each code in the code file.

Here I will explain the general syntax that I am trying to develop:
1. Run the “Opt_24h” Code
2. Run the “Dis_h1” Code using the variable values of (PP, Mn, Mx) from the above run “Opt_24h” as parameters
3. Run the “Opt_23h” Code using the last value of the variable (SOR) from the above run “Dis_h1” as a parameter
4. Run the “Dis_h2” Code using the variable values of (PP, Mn, Mx) from the above run “Opt_23h” as parameters
5. Run the “Opt_22h” Code using the last value of the variable (SOR) from the above run “Dis_h2” as a parameter
6. Run the “Dis_h3” Code using the variable values of (PP, Mn, Mx) from the above run “Opt_22h” as parameters
…….
…..
…..
And so on until
47. Run the “Opt_1h” Code using the last value of the variable (SOR) from the above run “Dis_h23” as a parameter
48. Run the “Dis_h24” Code using the variable values of (PP, Mn, Mx) from the above run “Opt_1h” as parameters

Appreciate your help in helping me with a syntax for writing the GAMS code that will execute all the above without me having to run each code independently.
If you have any examples for similar approaches I would highly appreciate that as well

BR,
Opt & Dis Codes.zip
(631.7 KiB) Downloaded 184 times
User avatar
bussieck
Moderator
Moderator
Posts: 1033
Joined: 7 years ago

Re: Calling another GAMS code during execution

Post by bussieck »

Hi,

Please find attached the modified GAMS models and the new "driver" new2.gms. New2.gms calls the different GAMS programs iteratively. Since the first program of Opt and Dis are different from the others, new2.gms calls these explicitly. I changed these also somewhat, so they are attached here again. The remaining Opt and Dis programs are pretty much identical with the exception that they have different data for t and h as well as read and write files where the hour number is part of the filename. This can all be easily parameterized with double dash parameters (--HOUR) and a fixed GAMS program Opt_Nh.gms and Dis_hN.gms. While Dis counted upwards, Opt counted downwards. I changed this so Opt also counts upwards. Nevertheless, the logic of the models is unchanged. Besides the data and file parameterization I also copied variables into parameters and renamed when when the GDX file is written. I did not read and write any Excel files anymore (only for Opt_24h.gms).

Just run new2.gms.

-Michael
Dis_h1.gms
(3.96 KiB) Downloaded 196 times
Dis_hN.gms
(3.79 KiB) Downloaded 190 times
Opt_24h.gms
(4.21 KiB) Downloaded 188 times
Opt_Nh.gms
(3.86 KiB) Downloaded 180 times
new2.gms
(373 Bytes) Downloaded 201 times
mohamed.omer
User
User
Posts: 5
Joined: 2 years ago

Re: Calling another GAMS code during execution

Post by mohamed.omer »

Greetings Michael.

Many thanks for your tremendous assistance on this code.

when I ran new2.gms "driver" code, I got the following error. Error 772: "execute.async or execute.sync expected" for both of the first two execute statements.

Could you please let me know how I can fix this issue and many thanks in advance.

BR,
User avatar
bussieck
Moderator
Moderator
Posts: 1033
Joined: 7 years ago

Re: Calling another GAMS code during execution

Post by bussieck »

I suggest to download a recent GAMS version. The .checkErrorLevel is not essential (and there are other ways to see that the GAMS run succeeded), but there might be other things that I used in the models that are newer than what you have installed. Try to always work with recent software.

-Michael
Post Reply