1

The documentation suggests that sets should appear in the same order to increase performance. If most of our variables share a set, is it better to have the common set first or last?

I.e. which is more efficient?

  y[i,t] =E= a[t] * x[j,t];

or

  y[t,i] =E= a[t] * x[t,j];
Martin Bonde
  • 536
  • 3
  • 11

2 Answers2

2

The main point of this "same order" is, that the sets should be used in the order they are controlled. So

Equation1(t,i,j).. y[t,i] =E= a[t] * x[t,j];

should be better than

Equation2(i,j,t).. y[t,i] =E= a[t] * x[t,j];

Other than that, it is not so easy to give many general rules. If you have also full control over the controlling indices, often it is beneficial, if the largest set is last, so if t >> i, than x[i,t] should be better than x[t,i]. In general, the GAMS command line parameter profile (https://www.gams.com/latest/docs/UG_GamsCall.html#GAMSAOprofile) is very useful to check the influence of different formulations of your mode.

Lutz
  • 2,197
  • 1
  • 10
  • 12
  • Thanks! It makes sense that the controlling indices and size of sets are relevant. Does your answer imply that there is no clear rule of thump for which should be faster between: Equation1(t,i,j).. y[t,i] =E= a[t] * .. z[t] * x[t,j]; and Equation2(i,j,t).. y[i,t] =E= a[t] * .. z[t] * x[j,t]; Given that the sets have the same size? I.e. putting a set shared by most variables first or last (time index in a large dynamic model). The profiler has been very helpful, but in this case would need a substantial re-formulation to check. – Martin Bonde Oct 29 '19 at 14:28
  • 1
    Equation1 should be faster. But not sure, if you want to do a major reformulation for this difference. I made a trivial experiment (cannot post it completely here in the comment): `$set max 200 Set t / t1*t%max%/ i / i1*i%max%/ j / j1*j%max%/; ... Equation1(t,i,j).. y[t,i] =E= a[t] * a[t] *b[t] *c[t] *d[t] * z[t] * x[t,j]; Equation2(i,j,t).. yy[i,t] =E= a[t] * a[t] *b[t] *c[t] *d[t] * z[t] * xx[j,t];` Here is the profile on my machine: 14.812sec Equation1 (8000000); 16.265sec Equation2 – Lutz Nov 04 '19 at 08:53
  • Very interesting. That is significant enough that I will give the reformulation a try when I have time. – Martin Bonde Nov 05 '19 at 15:43
1

In case anyone stumbles upon this, the context is a large dynamic economic model.

We tried switching our time index from always being the last set to always being the first. The model is a square system of around 1m equations, 7m Jacobian elements, of which 2m are non-linear.

The solution time using CONOPT4 was significantly worse after the change. I.e. we have better performance with the time index last rather than first, despite the time index being among the largest sets in the model. The result is probably not transferable to other models, but confirms that there is no trivial answer.

Martin Bonde
  • 536
  • 3
  • 11