This is an old revision of the document!
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 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 Compilation * Setup * profile: Show execution profile (times for each statement). * It is better to use command line parameter profile=2 Option profile=2; Set i /i1*i1000/ k /k1*k20/ l /l1*l20/; 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 Variable z; Equation e1,e3; 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)); * Test5 e3(i,ii).. z =G= p4(i,ii); Model m /all/; 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 * Setup * profile: Show execution profile (times for each statement). * It is better to use command line parameter profile=2 Option profile=2; Set i /i1*i1000/ k /k1*k20/ l /l1*l20/; 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 Avoid repeating a calculation Variable z,x; Equation e1,e2,e3; 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)) ); * Test5 Use dollar conditions to avoid the generation of unnecessary constraints * (You may also use subsets to avoid the generation of unnecessary constraints) e3(i,ii)$p4(i,ii).. z =G= p4(i,ii); Model m /all/; * 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;