# GAMS Support Wiki

### Site Tools

gams:keeping_gams_memory_constant_for_multiple_solves_in_a_loop

# Keeping memory used by GAMS constant for multiple solves in a loop

When utilizing multiple solve statements (e.g. in a loop) the amount of memory used by GAMS may increase even though the indiviual model size (for the particular solve statement) remains the same. This is due to storage of parameters and variables grows, even though they are not necessarily used for each solve statement.

In some case there are ways to keep the amount of memory pretty constant. They include:

1. Use `option solveopt=replace` which replaces rather than merges all solution values of each GAMS solve.
2. Use `option clear=param` to clean temporary data which is not used afterwards.
3. Use put files to store solution values instead of storing in memory.
```          file fput /rep.txt/;
put fput (data);```
4. Batch several sub-models into fewer but bigger models.

The first two options are simple, the third one might be inconvenient (especially if you want to do post solution analysis) whereas the option to aggregate the LPs into fewer but bigger models can be quite difficult. This option generally is only useful if each sub-model to solve is not dependent on the solution of the previous model. Although there may be instances where the addition of a couple of more constraints can accomplish this, it is not always possible.

We shall give examples using the transportation model (trnsport.gms) where we have introduced a time series component.

``` Set  t      time      / t1*t10 /
tt(t)       dynamic version of t;
Parameter   c(i,j)   transport cost in thousands of dollars per case
at(i,t), bt(j,t), ct(i,j,t)  "supply, demand, cost by time";
c(i,j) = f * d(i,j) / 1000 ;
Variables
x(i,j,t)  shipment quantities in cases

Equations
cost          define objective function
supply(i,t)   observe supply limit at plant i
demand(j,t)   satisfy demand at market j ;

cost ..           z  =e=  sum((i,j,tt), ct(i,j,tt)*x(i,j,tt)) ;
supply(i,tt) ..   sum(j, x(i,j,tt))  =l=  at(i,tt) ;
demand(j,tt) ..   sum(i, x(i,j,tt))  =g=  bt(j,tt) ;

loop(t,
tt(t) = yes;

ct(i,j,tt(t)) = c(i,j) * ( 1 + 0.25**ord(t));
at(i,tt(t)) = a(i) * (1 + 0.15**ord(t));
bt(j,tt(t)) = b(j) * (1 + 0.15**ord(t));

Solve transport using lp minimizing z ;

tt(t) = no;
);```
1. For the solution replace option we add: `option solveopt=replace;` before the `loop(t,` statement.
2. For the clear option we can clear the parameters `ct`, `at`, and `bt`, since they are only used for the local solve. After `tt(t) = yes;`we specify `option clear=ct, clear=at, clear=bt;`
3. For the put file option to store solutions, we create a put file called `frep.put` before the loop by:` file frep`. After the solve statement, we write to the put file by:
```  put frep / t.tl 'modstat' transport.modelstat
/ t.tl 'slvstat' transport.solvestat
/ t.tl 'obj    ' z.l;```
4. In order to aggregate the models we need to change our objective function. The model becomes:
``` Equations
defobj
cost(t)       define objective function
supply(i,t)   observe supply limit at plant i
demand(j,t)   satisfy demand at market j ;

defobj ..         obj =e= sum(tt, z(tt));
cost(tt) ..       z(tt) =e=  sum((i,j), ct(i,j,tt)*x(i,j,tt)) ;
scalar idx /0/, bsize /5/;
repeat
tt(t) = no;

*  Batch 5 single LPs into one larger model:
loop(t\$(ord(t)>(idx*bsize) and ((idx+1)*bsize)>=ord(t)), tt(t) = yes);

ct(i,j,tt(t)) = c(i,j) * ( 1 + 0.25**ord(t));
at(i,tt(t)) = a(i) * (1 + 0.15**ord(t));
bt(j,tt(t)) = b(j) * (1 + 0.15**ord(t));

if (card(tt), Solve transport using lp minimizing obj);
idx = idx + 1;
until card(tt)=0;```

The implementation of all four options using the same equations as above is:

``` File frep;
Option solveopt=replace;
repeat
tt(t) = no;
loop(t\$(ord(t)>(idx*bsize) and ((idx+1)*bsize)>=ord(t)), tt(t) = yes);

option clear=ct, clear=at, clear=bt;
ct(i,j,tt(t)) = c(i,j) * ( 1 + 0.25**ord(t));
at(i,tt(t)) = a(i) * (1 + 0.15**ord(t));
bt(j,tt(t)) = b(j) * (1 + 0.15**ord(t));
if (card(tt), Solve transport using lp minimizing obj);

loop(tt(t),
put frep / t.tl 'modstat' transport.modelstat
/ t.tl 'slvstat' transport.solvestat
/ t.tl 'obj    ' z.l(t);
);
idx = idx + 1;
until card(tt)=0;```
{"ft_lang_label":"__label__en","ft_lang_prob":0.661857,"math_prob":0.996193,"size":3502,"snap":"2021-43-2021-49","text_gpt3_token_len":1066,"char_repetition_ratio":0.12521441,"word_repetition_ratio":0.22563177,"special_character_ratio":0.32524273,"punctuation_ratio":0.20092915,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.99961907,"pos_list":[0,1,2,3,4],"im_url_duplicate_count":[null,null,null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-10-18T00:39:50Z\",\"WARC-Record-ID\":\"<urn:uuid:a63bb9c6-975e-4f3a-b936-0b81df876467>\",\"Content-Length\":\"15767\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:ef691c19-5368-407a-bdf6-800d23472934>\",\"WARC-Concurrent-To\":\"<urn:uuid:e58f5458-56fe-4682-9f5e-49b95b384caa>\",\"WARC-IP-Address\":\"78.46.254.22\",\"WARC-Target-URI\":\"https://support.gams.com/gams:keeping_gams_memory_constant_for_multiple_solves_in_a_loop\",\"WARC-Payload-Digest\":\"sha1:WX566CIMDLJW27YD4XLEXGTFDTAKLLEU\",\"WARC-Block-Digest\":\"sha1:MSKETMMUMBDD7EDDAUKPPCUZO4YBSTPA\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-43/CC-MAIN-2021-43_segments_1634323585186.33_warc_CC-MAIN-20211018000838-20211018030838-00413.warc.gz\"}"}