|
Defined variables are auxilliary variables in the AMPL model that can depend
on the problem variables and on other defined variables. They are most often
used to simplify the notation. If a complex expression appears many times in
a model, it may be worthwhile to store it in a defined variable and use this
defined variable where the expression would appear.
In AMPL, defined variables are not stored in the same data structures as
other expressions involving only the problem variables. Moreover, if a
defined variable v depends on other defined variable u, even if linearly in
appearance, AMPL considers this dependance nonlinear. For instance in the
situation
var x {1..n};
var v = sum {i in 1..n} x[i]^2;
var u = v-x[n];
AMPL considers that v is part of the nonlinear part of
u. In other words, the linear part of a defined variable only
contains problem variables, not defined variables.
Defined variables are also referred to as as common expressions.
An element of code to parse common expressions is as follows.
In this code segment, all uppercase variables are defined in the main
AMPL headers. The input is the variable e, a pointer to a
general nonlinear expression. The linear part of the expression is
displayed in the last for loop. Its nonlinear part is
displayed by the hypothetical function display_expr().
k = (expr_v *)e - VAR_E;
if( k >= n_var ) {
// This is a common expression. Find pointer to its root.
j = k - n_var;
if( j < ncom0 )
com_expr = CEXPS;
else
com_expr = CEXPS1 - ncom0;
Printf( " Nonlinear part:\n" );
display_expr( (com_expr + j)->e, asl );
nlin = (com_expr + j)->nlin; // Number of linear terms
if( nlin > 0 ) {
Printf( "\n Linear terms:\n" );
L = (com_expr + j)->L;
for( i = 0; i < nlin; i++ ) {
vp = (expr_v *)((char *)L->v.rp - ((char *)&ev.v - (char *)&ev));
Printf( " %-g x[%-d]", L->fac, (int)(vp - VAR_E) );
L++;
}
}
}
|