User Tools

Site Tools


gams:how_to_reduce_model_generation_time

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
gams:how_to_reduce_model_generation_time [2014/02/28 18:31]
support
gams:how_to_reduce_model_generation_time [2017/09/02 19:26] (current)
support
Line 1: Line 1:
 ===== How to reduce model generation time ===== ===== How to reduce model generation time =====
  
-If you want to reduce the model compilation ​timethen 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 subsets, as well as, the dollar control statement with an appropriate condition statement. Furthermore,​ McCarl Guide section ​[[http://​www.gams.com/​mccarl/​mccarlhtml/​finding_where_excessive_time_is_being_used.htm|Finding where excessive time is being used]] and, especially, option ​[[http://​www.gams.com/​mccarl/​mccarlhtml/​profile_2.htm|profile]], may be helpful when you are reducing the 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.
  
  
 <​code>​ <​code>​
-$Title Slow Compilation +$Title Slow Execution 
-set i /i1*i1000+* Setup 
-set k /k1*k20+* Use command line parameter ​ profile=1 ​ to see execution profile (exec times). 
-set l /l1*l20+Set i /i1*i500
-alias(k,kk)+    k /k1*k100
-alias(l,ll); +    l /l1*l100/; 
-parameter ​p1(k,l,i) test parameter 1; +Alias(i,​ii),​(k,kk),(l,ll); 
-parameter ​p2(i,k,l) test parameter 2; +Parameters ​p1(k,l,i) test parameter 1 
-parameter ​p3(k,i,l) test parameter 3;+           ​p2(i,k,l) test parameter 2 
 +           ​p3(k,i,l) test parameter 3 
 +           ​p4(i,​ii) ​ test parameter 4;
  
 * Test1 * Test1
 p1(k,​l,​i)$(ord(k)=ord(l))=1;​ p1(k,​l,​i)$(ord(k)=ord(l))=1;​
 +p4('​i1','​i2'​)=5;​
  
 * Test2 * Test2
Line 28: Line 31:
 ); );
  
-* Test4 +* Test4 and Test5 are coupled 
-Variable z+Variable z,y(k,l); 
-Equation e1; +Equation e1 Test5 
-e1(kk,ll)$(ord(kk)=ord(ll)).. +         e3 Test6 
-  z =G= sum((k,l,​i)$p3(k,​i,​l) ,​p3(k,​i,​l)*(ord(k)+ord(i)+ord(l)) ​+         e4 Test7 
-        / (ord(kk)+ord(ll));+         e5 Test8;
  
-model m /all/; +e1(k,​l)$(ord(k)=ord(l)).. 
-option lp=bdmlp; +  z =G= sum((kk,​ll,​ii)$p3(kk,​ii,​ll) ,​p3(kk,​ii,​ll)*(ord(kk)+ord(ii)+ord(ll)) ) 
-solve m us lp min z;+        / (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; Parameter Report;
 Report('​Generation_time'​)=m.resGen;​ Report('​Generation_time'​)=m.resGen;​
-display ​Report;+Display ​Report;
 </​code>​ </​code>​
  
-For the below model "​Fast ​Compilation" the model generation time is shorter.+For the below model "​Fast ​Execution" the model generation time is shorter.
  
 <​code>​ <​code>​
-$Title Fast Compilation +$Title Fast Execution 
-set i /i1*i1000+* Setup 
-set k /k1*k20+* Use command line parameter ​ profile=1 ​ to see execution profile (exec times). 
-set l /l1*l20+Set i /i1*i500
-alias(k,kk)+    k /k1*k100
-alias(l,ll); +    l /l1*l100/; 
-set kli(k,l,i) /#k:#l.#i/;+Alias(i,​ii),​(k,kk),(l,ll); 
 +Set kli(k,l,i) /#k:#l.#i/;
 * Extract subset kl from kli * Extract subset kl from kli
-set kl(k,l); +Set kl(k,l); 
-option ​kl<kli;+Option ​kl<kli;
  
 * Test1: Define levels instead of assigning them * Test1: Define levels instead of assigning them
-parameter ​p1(k,l,i) test parameter 1 /#k:#l.#i 1/ ; +Parameters ​p1(k,l,i) test parameter 1 /#k:#l.#i 1/ 
-parameter ​p2(i,k,l) test parameter 2; +           ​p2(i,k,l) test parameter 2 
-parameter ​p3(k,i,l) test parameter 3;+           ​p3(k,i,l) test parameter 3 
 +           ​p4(i,​ii) ​ test parameter 4 /i1.i2 5/ ;
  
 * Test2: Use assignments instead of loops * Test2: Use assignments instead of loops
Line 72: Line 92:
 ); );
  
 +* 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));
  
-* Test4 Avoid repeating a calculation 
-Variable z,x; 
-Equation e1,e2; 
 e1(kl(k,​l)).. z =G= x / (ord(k)+ord(l)) ; 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)) );+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/; 
-option lp=bdmlp; 
  
-* Keep listing file minimal +Model /all/;
-option limrow=0; +
-option limcol=0; +
-m.solprint=0;+
  
-solve m us lp min z;+Solve m us lp min z;
 Parameter Report; Parameter Report;
 Report('​Generation_time'​)=m.resGen;​ Report('​Generation_time'​)=m.resGen;​
-display ​Report;+Display ​Report;
 </​code>​ </​code>​
IMPRESSUM / LEGAL NOTICEPRIVACY POLICY gams/how_to_reduce_model_generation_time.1393608701.txt.gz · Last modified: 2014/02/28 18:31 by support