This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
gams:how_to_reduce_model_generation_time [2014/03/03 11:59] tlastusilta |
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 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 [[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 |
* Setup | * Setup | ||
* Use command line parameter profile=1 to see execution profile (exec times). | * Use command line parameter profile=1 to see execution profile (exec times). | ||
- | Set i /i1*i1000/ | + | Set i /i1*i500/ |
- | k /k1*k20/ | + | k /k1*k100/ |
- | l /l1*l20/; | + | l /l1*l100/; |
Alias(i,ii),(k,kk),(l,ll); | Alias(i,ii),(k,kk),(l,ll); | ||
Parameters p1(k,l,i) test parameter 1 | Parameters p1(k,l,i) test parameter 1 | ||
Line 31: | Line 31: | ||
); | ); | ||
- | * Test4 and Test5 | + | * Test4 and Test5 are coupled |
- | Variable z; | + | Variable z,y(k,l); |
- | Equation e1,e3; | + | Equation e1 Test5 |
- | z.lo=smax((k,l)$(ord(k)=ord(l)),ord(k)+ord(l)); | + | e3 Test6 |
- | e1(kk,ll)$(ord(kk)=ord(ll)).. | + | e4 Test7 |
- | z =G= sum((k,l,i)$p3(k,i,l) ,p3(k,i,l)*(ord(k)+ord(i)+ord(l)) ) | + | e5 Test8; |
- | / (ord(kk)+ord(ll)); | + | |
+ | 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 | * Test6 | ||
e3(i,ii).. z =G= p4(i,ii); | 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/; | Model m /all/; | ||
Line 50: | Line 61: | ||
</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 |
* Setup | * Setup | ||
* Use command line parameter profile=1 to see execution profile (exec times). | * Use command line parameter profile=1 to see execution profile (exec times). | ||
- | Set i /i1*i1000/ | + | Set i /i1*i500/ |
- | k /k1*k20/ | + | k /k1*k100/ |
- | l /l1*l20/; | + | l /l1*l100/; |
Alias(i,ii),(k,kk),(l,ll); | Alias(i,ii),(k,kk),(l,ll); | ||
Set kli(k,l,i) /#k:#l.#i/; | Set kli(k,l,i) /#k:#l.#i/; | ||
Line 81: | Line 92: | ||
); | ); | ||
- | * Test4 Use subset kl to avoid the comparison $(ord(kk)=ord(ll)) | + | * Test4 Use subset kl in e1 to avoid the comparison $(ord(k)=ord(l)) |
- | * to be performed several times | + | * to be performed several times |
- | Variable z,x; | + | * Test5 Avoid repeating a calculation, hence, e2 defines x that is used in e1. |
- | Equation e1,e2,e3; | + | Variable z, y(k,l), x; |
- | z.lo=smax(kl(k,l),ord(k)+ord(l)); | + | 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)) ; | 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)) ); | ||
- | * Test5 Avoid repeating a calculation | + | * Test6 Use dollar exception-handling to avoid the generation of unnecessary constraints |
- | e2.. x =E= sum((k,l,i)$p3(k,i,l) ,p3(k,i,l)*(ord(k)+ord(i)+ord(l)) ); | + | |
- | + | ||
- | * Test6 Use dollar exception-handling to avoid | + | |
- | * the generation of unnecessary constraints | + | |
e3(i,ii)$p4(i,ii).. z =G= p4(i,ii); | 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/; | Model m /all/; |