User Tools

Site Tools


gams:how_to_reduce_model_generation_time

This is an old revision of the document!


How to reduce model generation time

If you want to reduce the model compilation time, then the below examples might be helpful. The first example “Slow Compilation” is poorly modeled. The second example “Fast Compilation” gives suggestions on how to improve the model, especially, regarding model generation time. Keep in mind that when you use indexed statements, like loop and sum, then you might be able to restrict the traversing over indices to a subset of all indices by using the dollar control statement with an appropriate condition statement. Furthermore, McCarl Guide section Finding where excessive time is being used and, especially, option profile, may be helpful when you are reducing the model generation time.

$Title Slow Compilation
set i /i1*i1000/
set k /k1*k20/
set l /l1*l20/
alias(k,kk);
alias(l,ll);
parameter p1(k,l,i) test parameter 1;
parameter p2(i,k,l) test parameter 2;
parameter p3(k,i,l) test parameter 3;

* Test1
p1(k,l,i)$(ord(k)=ord(l))=1;

* Test2
loop((i,k,l),p2(i,k,l)=2;)

* Test3
loop((i,k,l),
  if(mod(ord(i),2)=0,
     p3(k,i,l)=3;
  );
);

* Test4
Variable z;
Equation e1;
e1(kk,ll)$(ord(kk)=ord(ll))..
  z =G= sum((k,l,i)$p3(k,i,l) ,p3(k,i,l)*(ord(k)+ord(i)+ord(l)) )
        / (ord(kk)+ord(ll));

model m /all/;
option lp=bdmlp;
solve m us lp min z;
Parameter Report;
Report('Generation_time')=m.resGen;
display Report;

For the below model “Fast Compilation” the model generation time is shorter.

$Title Fast Compilation
set i /i1*i1000/
set k /k1*k20/
set l /l1*l20/
alias(k,kk);
alias(l,ll);
set kli(k,l,i) /#k:#l.#i/;
* Extract subset kl from kli
set kl(k,l);
option kl<kli;

* Test1: Define levels instead of assigning them
parameter p1(k,l,i) test parameter 1 /#k:#l.#i 1/ ;
parameter p2(i,k,l) test parameter 2;
parameter p3(k,i,l) test parameter 3;

* Test2: Use assignments instead of loops
p2(i,k,l)=2;

* Test3: Set loop index with most elements in last position
loop((k,l,i),
  if(mod(ord(i),2)=0,
     p3(k,i,l)=3;
  );
);


* Test4 Avoid repeating a calculation
Variable z,x;
Equation e1,e2;
e1(kl(k,l)).. z =G= x / (ord(k)+ord(l)) ;
e2..  x =E= sum((k,l,i)$p3(k,i,l) ,p3(k,i,l)*(ord(k)+ord(i)+ord(l)) );

model m /all/;
option lp=bdmlp;

* Keep listing file minimal
option limrow=0;
option limcol=0;
m.solprint=0;

solve m us lp min z;
Parameter Report;
Report('Generation_time')=m.resGen;
display Report;
IMPRESSUM / LEGAL NOTICEPRIVACY POLICY gams/how_to_reduce_model_generation_time.1393603710.txt.gz · Last modified: 2014/02/28 17:08 by support