User Tools

Site Tools


gams:how_to_reduce_model_generation_time

How to reduce model generation time

If you want to reduce the model generation time then the below example might be helpful. The first example “Slow Execution” is poorly formulated. The second example “Fast Execution” gives suggestions on how to improve the model, when we consider the execution speed of assignment statements and 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 subsets, as well as, 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 Execution
* Setup
* Use command line parameter  profile=1  to see execution profile (exec times).
Set i /i1*i500/
    k /k1*k100/
    l /l1*l100/;
Alias(i,ii),(k,kk),(l,ll);
Parameters p1(k,l,i) test parameter 1
           p2(i,k,l) test parameter 2
           p3(k,i,l) test parameter 3
           p4(i,ii)  test parameter 4;

* Test1
p1(k,l,i)$(ord(k)=ord(l))=1;
p4('i1','i2')=5;

* 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 and Test5 are coupled
Variable z,y(k,l);
Equation e1 Test5
         e3 Test6
         e4 Test7
         e5 Test8;

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

* Test6
e3(i,ii)..  z =G= p4(i,ii);

* Test 7 A constraint with driving indices lk in  sum(lk, kl)
e4..  z =G= sum((l,k)$(ord(k)=ord(l)),y(k,l));

* Test8
e5(k,l)$(ord(k)=ord(l))..
      y(k,l) =G= (ord(k)+ord(l)) / (card(k)*card(l));


Model m /all/;

Solve m us lp min z;
Parameter Report;
Report('Generation_time')=m.resGen;
Display Report;

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

$Title Fast Execution
* Setup
* Use command line parameter  profile=1  to see execution profile (exec times).
Set i /i1*i500/
    k /k1*k100/
    l /l1*l100/;
Alias(i,ii),(k,kk),(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
Parameters p1(k,l,i) test parameter 1 /#k:#l.#i 1/
           p2(i,k,l) test parameter 2
           p3(k,i,l) test parameter 3
           p4(i,ii)  test parameter 4 /i1.i2 5/ ;

* 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 Use subset kl in e1 to avoid the comparison $(ord(k)=ord(l))
* to be performed several times
* Test5 Avoid repeating a calculation, hence, e2 defines x that is used in e1.
Variable z, y(k,l), x;
Equation e1 Test5 Avoid repeating a calculation (1 of 2)
         e2 Test5 Avoid repeating a calculation (2 of 2)
         e3 Test6 Use dollar exception-handling to avoid the generation of unnecessary constraints
         e4 Logical ordering of driving indices kl (sum(kl. k l));

e1(kl(k,l)).. z =G= x / (ord(k)+ord(l)) ;
e2..  x =E= sum((kk,ll,ii)$p3(kk,ii,ll) ,p3(kk,ii,ll)*(ord(kk)+ord(ii)+ord(ll)) );

* Test6 Use dollar exception-handling to avoid the generation of unnecessary constraints
e3(i,ii)$p4(i,ii)..  z =G= p4(i,ii);

* Test7 Logical ordering of driving indices kl , i.e. sum(kl, kl)
e4..  z =G= sum(kl(k,l),y(k,l));

* Test8 Set bounds instead of generating constraints
Scalar klsum  sum of card(k)*card(l) / [card(k)*card(l)] /;
y.lo(kl(k,l)) = (ord(k)+ord(l)) / klsum;


Model m /all/;

Solve m us lp min z;
Parameter Report;
Report('Generation_time')=m.resGen;
Display Report;
gams/how_to_reduce_model_generation_time.txt · Last modified: 2017/09/02 17:26 by support