Problems with saving a subset of variables/equations in gdx files within a loop

Hi, we are working with a model consisting of a MIP problem over a yearly time horizon, using a set, HOURS, made up of 8760 hours. To solve the problem more easily, we divided it into 52 weekly subproblems. We solve 52 instances of a weekly problem within a loop over the weeks, where the loop runs over an index that indicates the sub-period, and we set up a set, within the loop, related to the hours of that period. We want to save in the 52 GDX files only the variables and equations related to the corresponding week. The issue we are encountering is that each GDX file also contains the variables from the previous sub-periods. We tried using the solveOpt=2 option, but with this option, it seems that the fixed-value variables are being ignored.

We are wondering how the solveOpt option actually works.
Otherwise, we wanted to ask if it is possible, when defining the variables and equations to save in a GDX file using the put utility and execute unload functions, to save the variables and equations only for a subset of the sets on which they are defined.

The use case you describe is exactly for solveopt=clear or replace. Here is a little example script that demonstrates your use case:

set t / t1*t8760 /
    w / w1*w53   /
    d / d1*d7    /
    h / h1*h24  /
    map(t,w,d,h) / #t:(#w.#d.#h) /;
    
parameter dem(t);
variable x(t), z;
equation demand(t), defz;

set tt(t) "weekly hours";

demand(tt).. dem(tt) =e= x(tt);
defz.. z =e= sum(tt, x(tt));

model m / demand, defz /;

option limrow=0, limcol=0, solveopt=clear, solvelink=5;
loop(w,
  tt(t) = sum(map(t,w,d,h),1);
  option clear=dem; dem(tt) = uniform(0,1);
  solve m min z us lp;
  put_utility 'gdxout' / w.tl:0 '.gdx';
  execute_unload x;
);

This script generates GDX file w1.gdx, w2.gdx, …, w53.gdx with the x variable in it. If you look at w1.gdx you see x(‘t1’) to x(‘t168’). w2.gdx has x(‘t169’) to x(‘t336’)_and so on. If you change solveOpt to its default merge. w2.gdx has x(‘t1’) to x(‘t336’).

Hope this helps,
-Michael

Thank you very much for the help. In the end, we realized that the solveOpt = clear was clearing the variable bounds at each iteration, and they had to be reset at every cycle. Thanks again and have a great day.
Francesca.

Yes, this is sometimes a nasty side effect. The bounds are part of the variable records that get purged for when only a subset of variables is used and solveOpt is set to clear or replace.

-Michael