Clearing the declarations between solving the models.

Problems with modeling
Post Reply
Ei_Mori
User
User
Posts: 2
Joined: 3 years ago

Clearing the declarations between solving the models.

Post by Ei_Mori »

Hi, I would like to seek your advice on how to clear the sets, macros etc. between the solves. Probably this is a very simple issue, but I could not find an answer.

I am trying to run several policy simulations based on the same model but with different policy parameters. The core model has several ‘macro’ definitions inside, so simply loop over the simulation variables returns error such as ‘symbols redefined’.

Essentially the same thing, I wrote a simple script and run the whole code such as below. “Model.gms” includes set, parameter and equation definitions and can solve the model based on either policy parameters of “base_policy” and “alt_policy”. Of course, normally we should do it over the loop, but I just want to make my point simpler.
----
$set sim base_policy
$include "model.gms"

$set sim alt_policy
$include "model.gms"
----
But of course, running this gms file returns the same error since it has declaration of sets, scalers, and macros. So, I assume that I need to clear the set or macro declarations after solving the first “base_policy” simulation and run the fresh model by the “alt_policy”.

I searched manuals and web and thought “option solveopt = clear” could be something close, which did not work (returned the same error).

Any help from you would be really appreciated. Thanks!
User avatar
bussieck
Moderator
Moderator
Posts: 1038
Joined: 7 years ago

Re: Clearing the declarations between solving the models.

Post by bussieck »

Hi,

There is no good way to start from scratch after you compiled a significant piece of code. Your scenarios might be close enough (in terms of domain sets etc) that this can be accomplished with lots of $clear and $onMulti (see https://www.gams.com/latest/docs/UG_Dol ... tions.html) but it is usually not the right approach. You either build the scenario loop into your model loading the explicit data changes at runtime (via execute_load) or just automate your batch scenario jobs. There are many way to do the latter. The easiest is to make a script that calls the model with parameterized input:

Create a script runall.gms with the following line:

Code: Select all

$call gams model.gms --sim base_policy lo=%gams.lo% gdx=base_policy
$call gams model.gms --sim alt_policy  lo=%gams.lo% gdx=alt_policy
...
Not sure how you want to handle the output from the different runs. With this you at least capture the entire run in a separate GDX file (base_policy.gdx, alt_policy.gdx, ...)

If there are hundreds of scenarios files you can automate this also using a GAMS script:

Code: Select all

set scen /base_policy, policy1*policy100/;
loop(scen ,
  put_utility 'exec' / 'gams model --sim=' scen.tl:0 ' lo=%gams.lo% gdx=' scen.tl:0;
);
-Michael
Ei_Mori
User
User
Posts: 2
Joined: 3 years ago

Re: Clearing the declarations between solving the models.

Post by Ei_Mori »

Thank you! Your first script worked very well.
I should work on building loop correctly, of course, but until then, I will use this approach.
Thanks again.
Post Reply