Nested loop solves with dynamic sets: can I clear solutions between loops?

Hello.
I have a discrete time problem for which I have some hourly variables and some daily variables that depend on dynamic sets which change in every iteration, depending on two sets: k and r. I made a code that has the following structure:

sets  
i days /1*100/
t hours /1*1000/
ii(i) dynamic set of i
tt(t) dynamic set of t
k independent iterations  /1*5/
r dependent iterations within k /1*10/;

scalars
d initial day;

variables;
*My variables depend on the dynamic sets
equations
*I have equations blocks that depend on my dynamic sets
eq1(tt)..;
eq2(ii)..;
etc..;

model cycle_days /all/;
options mip = cplex;

*Merge solutions:
cycle_days.solveopt = 1;

*Use file with starting solution:
cycle_days.optfile = 1;

loop(k,
d = ord(k)-1;

loop(r,

loop(t,
tt(t) = no;
tt(t) = yes$(ord(t) ge (ord(r)+d)*24-23 and ord(t) le 24*(ord(r)+d));
*for d=0, r=1, tt will be yes between 1 and 24
*for d=0, r=2, tt will be yes between 25 and 48
*for d=1, r=1, tt will be yes between 25 and 48
);

loop(i,
ii(i) = no;
ii(i) = yes$(ord(d) gt ord(r)+d and ord(d) le card(r)+d);
*for d=0, r=1, ii will be yes between 2 and 10
*for d=0, r=2, ii will be yes between 3 and 10
*for d=1, r=1, ii will be yes between 3 and 11
);

solve cycle_days using mip minimizing objective

*All variables that have tt=no or ii=no are set to zero by GAMS

*Now I create a feasible solution for the next r

);
*Put merged solution to excel file

*I would like here, to somehow erase the previous solution values (not set them to 0)
*so, cplex can find an initial feasible solution automatically, but I cannot use .optfile=0 in a loop
);

While I want the solver to read initial solutions while iterating over r, I want to erase everything when I iterate over k (or not read initial solution from the previous iteration). I was thinking of using option clear for every variable, but I have too many of them (plus I think it does not do what I want).

For example, before I created the loop of k, I manually changed my scalar d and pressed the run GAMS button on top, and it ran just fine. How can I make this process automated?

Thanks in advance for your help, and sorry for the long post.

Best,
Petros

Petros,

There is no single command that clears all the variables.

Since you solve a MIP you don’t have to worry at all. For MIPs the GAMS variable/equation levels/marginals are not provided to Cplex (because Cplex and other MIP solvers can’t take advantage of a good basis because the MIP presolve will destroy this in most cases) unless you set option mipStart<>0.

For other model types I like to make a separate “reference solve” and store this solution using the savePoint facility. Then in the loop I load this reference point via execute_loadpoint:

...
* Create mymodel_p.gdx
mymodel.savePoint = 1;
solve mymodel us lp min obj;
mymodel.savePoint = 0;
loop(i,
   ...
   execute_load 'mymodel_p.gdx';
   solve mymodel us lp min obj;
   ...
);
...

Hope this helps,
-Michael

Hi Michael,

Thanks a lot for your answer. Can I use the .savepoint facility inside a loop as well? Also, in case my original question was confusing:

Before i decided to use nested loop solves, i manually set a value of my scalar “d” and pressed run (for example: d /0/, press run, then d /1/, press run again, etc.). Now I just want to do this process in a loop, but the previous solutions interact with the new iteration. Will your “reference solve” approach work for this problem?

Thanks again,
Petros

Yes, savepoint can be used inside the loop, but I would noit understand the use of it in your case. And yes, the reference solution should work for the described case.

-Michael