Common subexpressions

Does AMPL internally do any common subexpression elimination (CSE) with the algebraic expressions? The expressions in my AMPL model can become quite large. However, the model will have many common subexpressions. If AMPL doesn’t do any CSE, then I might reformulate how I generate the model, but wanted to make sure AMPL isn’t already doing something like CSE in the backend.

As background, for my target application, when using IPOPT, most of the time is spent evaluating functions throughout the optimization, which is why I believe CSE might be useful.

AMPL identifies common subexpressions to a limited extent:

  • Within an objective expression defined by a minimize or maximize statement.
  • Within a constraint expression defined by a subject to statement.

By identifying these common subexpressions, AMPL can represent a problem instance more concisely in the representation that is sent to the solver interface. This does not make a significant difference to the work required for function and derivative evaluations, however. (Also, identical expressions that appear in different AMPL model statements are not recognized as common subexpressions.)

You can explicitly identify common subexpressions in your model, by use of defined variables, which are created by var statements that have an = followed by a defining expression. For example,

var x {i in S} = y[i] * sum {j in T} z[i,j];

Defined variables are often used just to make the model clearer, but if a defined variable appears in more than one place in a model — including in multiple statements — then it has the effect of a common subexpression. In that case, there may be a speedup in evaluations, though some benchmarking should be done to determine whether there is a significant speedup for your particular model. The most likely opportunities for speedups occur where defined variables have nonlinear defining expressions, or are used in nonlinear constraints.