# Numerical Expressions

Constant numerical expressions

Like any expression in AIMMS, a numerical expression can either be a constant or a symbolic expression. Constant expressions are those that contain references to explicit set elements and values, but do not contain references to other identifiers. Constant expressions are mostly intended for the initialization of sets, parameters and variables. Such an initialization must conform to one of the following formats:

• a scalar value,

• a list expression,

• a table expression, or

• a composite table.

Table expressions and composite tables are mostly used for data initialization from external files. They are discussed in Format of Text Data Files.

Symbolic numerical expressions

Symbolic expressions are those expressions that contain references to other AIMMS identifiers. They can be used in the Definition attributes of sets, parameters and variables, or as the right-hand side of assignment statements. AIMMS provides a powerful notation for expressions, and complicated numerical manipulations can be expressed in a clear and concise manner.

Syntax

numerical-expression:

## Real Values and Arithmetic Extensions

Extension of the real line

Traditional arithmetic is defined on the real line, $$\mathbb{R}=(-\infty,\infty)$$, which does not contain either $$+\infty$$ or $$-\infty$$. AIMMS’ arithmetic is defined on the set $$\mathbb{R}\cup\{{}$$-INF, INF, NA, UNDF, ZERO$${}\}$$ and summarized in this table. The symbols INF and -INF are mostly used to model unbounded variables. The symbols NA and UNDF stand for not available and undefined data values respectively. The symbol ZERO denotes the numerical value zero, but has the logical value true (not zero).

Table 7 Extended values of the AIMMS language

Symbol

Description

Logical value

MapVal value

number

any valid real number

0

UNDF

undefined (result of an arithmetic error)

1

4

NA

not available

1

5

INF

$$+\infty$$

1

6

-INF

$$-\infty$$

1

7

ZERO

numerically indistinguishable from zero, but has the logical value of one.

1

8

Numerical behavior

AIMMS treats these special symbols as ordinary real numbers, and the results of the available arithmetic operations and functions on these symbols are defined. The values INF, -INF and ZERO are accessible by the user and are dealt with as expected: $$1+{\texttt{INF}}$$ evaluates to INF, $$1/{\texttt{INF}}$$ to 0, $$1+{\texttt{ZERO}}$$ to 1, etc. However, the values of INF and -INF are undetermined and therefore, it makes no sense to consider $${\texttt{INF}}/{\texttt{INF}}$$, $${\texttt{-INF}}+{\texttt{INF}}$$, etc. These expressions are therefore evaluated to UNDF. A runtime error will occur if the value UNDF is assigned to an identifier.

The symbol ZERO

The symbol ZERO behaves like zero numerically, but its logical value is one. Using this symbol, you can make a distinction between the default value of 0 and an assigned ZERO. As an illustration, consider a distance matrix with distances between selected factory-depot combinations. A missing distance value evaluates to 0, and could mean that the particular factory-depot combination should not be considered. A ZERO value in that case could be used to indicate that the combination should be considered even though the corresponding distance is zero because the depot and factory happen to be one facility.

Expressions with 0 and ZERO

Whenever the values 0 and ZERO appear in the same expression with equal priority, the value of ZERO prevails. For example, the expressions $$0+{\texttt{ZERO}}$$ or max(0,ZERO) will both result in a numerical value of ZERO. In this way, the logically distinctive effect of ZERO is retained as long as possible. You should note, however, that AIMMS will evaluate the multiplication of 0 with any special number to 0.

The symbol NA

The symbol NA can be used for missing data. The interpretation is “this number is not yet known”. Any operation that uses NA and does not use the symbol UNDF will also produce the result NA. AIMMS can reason with this value as it propagates the value NA through its computations and assignments. The only exception is the condition in control flow statements where it must be known whether the result of that condition is equal to 0.0 or not, see also Flow Control Statements.

The symbol UNDF

The symbol UNDF cannot be input directly by a user, but is, besides an error message, the result of an undefined or illegal arithmetic operation. For example, 1/ZERO, 0/0, (-2)^0.1 all result in UNDF. Any operation containing the UNDF symbol evaluates to UNDF.

## List Expressions

Element-value pairs

A list is a collection of element-value pairs. In a list a single element or range of elements is combined with a numerical, element-, or string-valued expression, separated by a colon. List expressions are the numerical extension of enumerated set expressions. The elements to which a value is assigned inside a list, are specified in exactly the same manner as in an enumerated set expression as explained in Enumerated Sets.

Syntax

enumerated-list:

Constant versus symbolic

By preceding the list expression with the keyword DATA, it becomes a constant list expression, in a similar fashion as with constant set expressions (see Enumerated Sets). In a constant list expression, set elements need not be quoted and the assigned values must be constants. All other list expressions are symbolic, in which both the elements and the assigned values are the result of expression evaluation.

Example

The following assignments illustrate the use of list expressions.

• The following constant list expression assigns distances to tuples of cities.

Distance(i,j) := DATA {
(Amsterdam, Rotterdam  ) : 85 [km] ,
(Amsterdam, 'The Hague') : 65 [km] ,
(Rotterdam, 'The Hague') : 25 [km]
} ;

• The following symbolic list expression assigns a certain status to every node in a number of dynamically computed ranges.

NodeUsage(i) := {
FirstNode            .. FirstNode + Batch - 1   : 'InUse'   ,
FirstNode + Batch    .. FirstNode + 2*Batch - 1 : 'StandBy' ,
FirstNode + 2*Batch  .. LastNode                : 'Reserve'
} ;


## Arithmetic Functions

Standard functions

AIMMS provides the commonly used standard arithmetic functions such as the trigonometric functions, logarithms, and exponentiations. this table lists the available arithmetic functions with their arguments and result, where $$x$$ is an extended range arithmetic expressions, $$m$$, $$n$$ are integer expressions, $$i$$ is an index, $$l$$ is a set element, $$I$$ is a set identifier, and $$e$$ is a scalar reference.

Table 8 Intrinsic numerical functions of AIMMS

Function

Meaning

Abs$$(x)$$

absolute value $$|x|$$

Exp$$(x)$$

$$e^x$$

Log$$(x)$$

$$\log_e(x)$$ for $$x>0$$,UNDF otherwise

Log10$$(x)$$

$$\log_{10}(x)$$ for $$x>0$$, UNDF otherwise

Max$$(x_1,\dots,x_n)$$

$$\max(x_1,\dots,x_n)\quad (n>1)$$

Min$$(x_1,\dots,x_n)$$

$$\min(x_1,\dots,x_n)\quad (n>1)$$

Mod$$(x_1,x_2)$$

$$x_1 \bmod {x_2} \in [0,x_2)$$ for $$x_2 > 0$$ or $$\in(x_2,0]$$ for $$x_2<0$$

Div$$(x_1,x_2)$$

$$x_1\;\mathrm{div}\;{x_2}$$

Sign$$(x)$$

$$\mathrm{sign}(x)=+1$$ if $$x>0$$, $$-1$$ if $$x<0$$ and $$0$$ if $$x=0$$

Sqr$$(x)$$

$$x^2$$

Sqrt$$(x)$$

$$\sqrt x$$ for $$x\geq0$$, UNDF otherwise

Power$$(x_1,x_2)$$

$$x_1^{x_2}$$, alternative for x (see Numerical Operators)

ErrorF$$(x)$$

$${\frac{1}{\sqrt{2\pi}}}\int_{-\infty}^x e^{-{\frac{t^2}{2}}}\, dt$$

Cos$$(x)$$

$$\cos(x)$$; $$x$$ in radians

Sin$$(x)$$

$$\sin(x)$$; $$x$$ in radians

Tan$$(x)$$

$$\tan(x)$$; $$x$$ in radians

ArcCos$$(x)$$

$$\mathrm{arccos}(x)$$; result in radians

ArcSin$$(x)$$

$$\mathrm{arcsin}(x)$$; result in radians

ArcTan$$(x)$$

$$\mathrm{arctan}(x)$$; result in radians

Degrees$$(x)$$

converts $$x$$ from radians to degrees

Radians$$(x)$$

converts $$x$$ from degrees to radians

Cosh$$(x)$$

$$\cosh(x)$$

Sinh$$(x)$$

$$\sinh(x)$$

Tanh$$(x)$$

$$\tanh(x)$$

ArcCosh$$(x)$$

$$\mathrm{arccosh}(x)$$

ArcSinh$$(x)$$

$$\mathrm{arcsinh}(x)$$

ArcTanh$$(x)$$

$$\mathrm{arctanh}(x)$$

Card$$(I[,\mathit{suffix}])$$

cardinality of (suffix of) set, parameter or variable $$I$$

Ord$$(i)$$

ordinal number of index $$i$$ in set $$I$$ (see also this table)

Ord$$(l[,I])$$

ordinal number of element $$l$$ in set $$I$$

Ceil$$(x)$$

$$\lceil x \rceil = \text{smallest integer} \geq x$$

Floor$$(x)$$

$$\lfloor x \rfloor = \text{largest integer} \leq x$$

Precision$$(x,n)$$

$$x$$ rounded to $$n$$ significant digits

Round$$(x)$$

$$x$$ rounded to nearest integer

Round$$(x,n)$$

$$x$$ rounded to $$n$$ decimal places left ($$n<0$$) or right ($$n>0$$) of the decimal point

Trunc$$(x)$$

truncated value of $$x$$: Sign$$(x)*$$Floor$$($$Abs$$(x))$$

$$\mathtt{NonDefault}(e)$$

$$1$$ if $$e$$ is not at its default value, $$0$$ otherwise

MapVal$$(x)$$

MapVal value of $$x$$ according to this table

Functions and extended arithmetic

Special caution is required when one or more of the arguments in the functions are special symbols of AIMMS’ extended range arithmetic. If the value of any of the arguments is UNDF or NA, then the result will also be UNDF or NA. If the value of any of the arguments is ZERO and the numerical value of the result is zero, the function will return ZERO.

## Numerical Operators

Using unary or binary numerical operators you can construct numerical expressions that consist of multiple terms and/or factors. The syntax follows.

Syntax

operator-expression:

Standard numerical operators

The order of precedence of the standard numerical operators in AIMMS is given in this table. Parentheses may be used to override the precedence order. Expression evaluation is from left to right.

Table 9 Numerical operators

Operator

Meaning

Precedence

Unary

+

positive

n/a

-

negative

n/a

Binary

^

exponentiation

3 (high)

*

multiplication

2

/

division

2

+

1

-

subtraction

1 (low)

Example

The expression

p1 + p2 * p3 / p4^p5


is parsed by AIMMS as if it had been written

p1 + [(p2 * p3) / (p4^p5)]


In general, it is better to use parentheses than to rely on the precedence and associativity of the operators. Not only because it prevents you from making unwanted mistakes, but also because it makes your intentions clearer.

Exponential operator

Special restrictions apply to the exponential operator ^. AIMMS accepts the following combinations of left-hand side operand (called the base), and right-hand side operand (called the exponent):

• a positive base with a real exponent,

• a negative base with an integer exponent,

• a zero base with a positive exponent, and

• a zero base with a zero exponent results in one (as controlled by the option power_0_0).

## Numerical Iterative Operators

Arithmetic iterative operators

Iterative operators are used to express repeated arithmetic operations, such as summation, in a concise manner. The arithmetic iterative operators supported by AIMMS are listed in this table. The second column in this table refers to the required number of expression arguments following the binding domain argument, while the last column refers to the result of the operator in case of an empty domain.

Table 10 Arithmetic iterative operators

Name

# Expr.

Computes over all elements in the domain

Default

Sum

1

the sum of the expression

0

Prod

1

the product of the expression

1

Count

0

the total number of elements in the domain

0

Min

1

the minimum value of the expression

INF

Max

1

the maximum value of the expression

-INF

Compared expressions

The Min and Max operators return the minimum or maximum value of an expression. The allowed expressions are:

• numerical expressions, in which case AIMMS returns the lowest or highest numerical values,

• string expressions, in which case AIMMS returns the strings which are first or last with respect to the normal alphabetic ordering, and

• element expressions, in which case AIMMS returns the elements with the lowest or highest ordinal numbers (see also Intrinsic Functions for Sets and Set Elements).

Example

The following assignments are valid examples of the use of the arithmetic iterative operators.

NumberOfRoutes      := Count( (i,j) | Distance(i,j) ) ;
NettoTransport(i)   := Sum( j, Transport(i,j) - Transport(j,i) ) ;
MaximumTransport(i) := Max( j, Transport(i,j) ) ;


## Statistical Functions and Operators

Distributions

AIMMS provides the most commonly used distributions. They are listed in this table, together with the required type of arguments and a description of the result. You can find a more detailed description of these distributions in Discrete Distributions and Continuous Distributions. When called as functions inside your model, they behave as random number generators.

Table 11 Distributions available in AIMMS

Distribution

Meaning

Binomial$$(p,n)$$

Binomial distribution with probability $$p$$ and number of trials $$n$$

NegativeBinomial$$(p,r)$$

Negative Binomial distribution with probability $$p$$ and number of successes $$r$$

Poisson$$(\lambda)$$

Poisson distribution with rate $$\lambda$$

Geometric$$(p)$$

Geometric distribution with probability $$p$$

HyperGeometric$$(p,n,N)$$

Hypergeometric distribution with initial probability of success $$p$$, number of trials $$n$$ and population size $$N$$

Uniform$$({min,max})$$

Uniform distribution with lower bound min and upper bound max

Triangular$$(\beta,{min},{max})$$

Triangular distribution with shape $$\beta$$, lower bound min, and upper bound max, where $$\beta=(x_{\text{peak}}-{min})/({max}-{min})$$

Beta$$(\alpha,\beta,{min},{max})$$

Beta distribution with shapes $$\alpha$$, $$\beta$$, lower bound min, and upper bound max

LogNormal$$(\beta,{min},s)$$

Lognormal distribution with shape $$\beta$$, lower bound min, and scale $$s$$

Exponential$$({min},s)$$

Exponential distribution with lower bound min and scale $$s$$

Gamma$$(\beta,{min},s)$$

Gamma distribution with shape $$\beta$$, lower bound min, and scale $$s$$

Weibull$$(\beta,{min},s)$$

Weibull distribution with shape $$\beta$$, lower bound min, and scale $$s$$

Pareto$$(\beta,l,s)$$

Pareto distribution with shape $$\beta$$, location $$l$$, and scale $$s$$ ($$\text{lower bound} = l + s$$)

Normal$$(\mu,\sigma)$$

Normal distribution with mean $$\mu$$ and standard deviation $$\sigma$$

Logistic$$(\mu,s)$$

Logistic distribution with mean $$\mu$$ and scale $$s$$

ExtremeValue$$(l,s)$$

Extreme Value distribution with location $$l$$ and scale $$s$$

Setting the seed

You can set the seed of the random number generators for all distributions using the execution option seed. By setting the seed explicitly you can guarantee that your model results are reproducible.

Cumulative distributions and their derivatives

Each distribution in this table can be used as an argument for four operators: DistributionCumulative and DistributionInverseCumulative, and their derivatives DistributionDensity and DistributionInverseDensity. In the explanation below it is assumed that $$\alpha \in [0,1]$$, $$x \in (-\infty,\infty)$$, and $$X$$ a random variable distributed according to the given distribution distr.

Use in constraints

For the continuous distributions in this table, AIMMS can compute the derivatives of the cumulative and inverse cumulative distribution functions. As a consequence, you may use these functions in the constraints of a nonlinear model when the second argument is a variable.

Example

The following statements demonstrate how the distributions can be used to perform statistical tasks.

1. Draw a random number from a distribution.

Draw := Normal(0,1);
Draw := Uniform(LowestValue, HighestValue);

2. Compute the probability of at most 10 successes out of 50 trials, with a 0.25 probability of success.

Probability := DistributionCumulative( Binomial(0.25,50), 10 );

3. Compute a two-sided 90% confidence interval of a Normal(0,1) distribution.

LeftBound  := DistributionInverseCumulative( Normal(0,1), 0.05);
RightBound := DistributionInverseCumulative( Normal(0,1), 0.95);


Statistical operators

The distributions, listed in this table, make it possible for you to execute a stochastic experiment based on your model representation. In order to analyze the subsequent results, AIMMS provides a number of statistical iterative operators which are listed in this table. The second column in this table refers to the required number of expression arguments following the binding domain argument. For the most common sample operators, AIMMS provides distribution operators to calculate the corresponding expected values, assuming the sample is drawn from a given distribution. These distribution operators are listed in this table. A more detailed description of these operators is provided in Distributions, Statistical Operators and Histogram Functions.

Table 12 Statistical sample operators

Name

# Expr.

Computes over all elements in the domain

Mean

1

the (arithmetic) mean

GeometricMean

1

the geometric mean

HarmonicMean

1

the harmonic mean

RootMeanSquare

1

the root mean square

Median

1

the median

SampleDeviation

1

the standard deviation of a sample

PopulationDeviation

1

the standard deviation of a population

Skewness

1

the coefficient of skewness

Kurtosis

1

the coefficient of kurtosis

Correlation

2

the correlation coefficient

RankCorrelation

2

the rank correlation coefficient

Table 13 Statistical distribution operators

Name

Computes for a given distribution

DistributionMean

the (arithmetic) mean

DistributionDeviation

the (standard) deviation

DistributionVariance

the variance (the square of the deviation)

DistributionSkewness

the coefficient of skewness

DistributionKurtosis

the coefficient of kurtosis

Example

Assume that p is an index into a set that has been used to index a number of experiments resulting in observables x(p) and y(p). Then the following assignments demonstrate the use of the statistical operators in AIMMS.

MeanX         := Mean(p, x(p));
MeanX         := Mean(p | x(p), x(p));
DeviationX    := SampleDeviation(p, x(p));
CorrelationXY := Correlation(p, x(p), y(p));


In case the $$x$$ values are drawn from a Binomial(0.6,8) distribution the expected value of MeanX is given by

ExpectedMeanX := DistributionMean(Binomial(0.6,8));


Units of measurement

For all distributions, the units of measurement (see also Units of Measurement) of parameters and result should be consistent. The unit relationships for each distribution are described in Distributions, Statistical Operators and Histogram Functions in full detail. In the presence of units of measurement within your model, AIMMS will perform a unit consistency check.

Histogram support

For easy visualization of statistical data, AIMMS offers support for creating histograms based on a large collection of observed values. Through a number of predefined procedures and functions, AIMMS allows you to flexibly create interval-based histogram data, which can easily be displayed, for instance, using the standard (graphical) AIMMS bar chart object. For further information about creating and displaying histograms, as well as an illustrative example, you are referred to Creating Histograms in the Appendix.

Combinatoric functions

In addition to the distribution and statistical operators listed above, AIMMS also offers support for the most common combinatoric calculations. this table contains the list of combinatoric functions that are available in AIMMS.

Table 14 Combinatoric functions

Function

Meaning

Factorial$$(n)$$

$$n!$$

Combination$$(n,m)$$

$$\binom{n}{m}$$

Permutation$$(n,m)$$

$$m!\cdot{\binom{n}{m}}$$

## Financial Functions

Financial functions

AIMMS provides an extensive library of financial functions for a variety of financial applications. The available functions can be classified as follows.

• Functions for the computation of the depreciation of assets using various methods such as fixed-declining balance method, double-declining balance method, etc.

• Functions for computing various quantities regarding investments that consist of a series of constant or variable periodic cash flows. The computed quantities include present value, net present value, future value, internal rate of return, interest and principal payments, etc.

• Functions for computing various security-related quantities of, for instance, discounted securities, securities that pay periodic interest and securities that pay interest at maturity. The computed quantities include yield, interest rate, redemption, price, accrued interest, etc.

Consult the online function reference

The precise description of all financial functions available in AIMMS is not included in this Language Reference. A complete list of the available financial functions can be found in Financial Functions of the AIMMS Function Reference. The Function Reference provides a description as well as the prototype of every financial function present in AIMMS.

## Conditional Expressions

Two conditional expressions

There are two ways to specify expressions that adopt different values depending on one or more logical conditions. The ONLYIF operator is the simpler and operates as it sounds. The IF-THEN-ELSE expression is more powerful in its ability to distinguish several cases.

Syntax

conditional-expression:

The ONLYIF operator

The simplest way of specifying a conditional expression is to use the ONLYIF operator. Its syntax is given by

onlyif-expression:

The ONLYIF expression evaluates to the arithmetic expression in the first argument if the logical condition of the second argument is true. Otherwise, it is zero. The $ symbol can be used as a synonym for the ONLYIF operator. Example A simple example of the use of the ONLYIF operator is given by the assignment AverageVelocity := (Distance / TravelTime) ONLYIF TravelTime ;  or equivalently, using the $ operator,

AverageVelocity := (Distance / TravelTime) $TravelTime ;  Both expressions evaluate to Distance / TravelTime if TravelTime assumes a nonzero value, or to zero otherwise. In Modifying the Sparsity you will see that this particular expression can be written even more concisely using the sparsity modifier $.

IF-THEN-ELSE expressions

A much more flexible way for specifying conditional expressions is given by the IF-THEN-ELSE operator. The syntax of the IF-THEN-ELSE expression is given below.

Syntax

if-then-else-expression:

Explanation

The IF-THEN-ELSE expression works like a switch statement-a series of ELSEIFs can be used to denote numerous special cases. The value of the IF-THEN-ELSE expression is the first numerical expression for which the corresponding logical condition is true. If none of the conditions are true, then the value will be the numerical expression after the ELSE keyword if present or zero otherwise.

Example

A simple illustration of the use of the IF-THEN-ELSE construction is given by the assignments

AverageVelocity := IF TravelTime THEN Distance / TravelTime ENDIF ;


which is equivalent to the ONLYIF expression above. A more elaborate example is given by the assignment

WeightedDistance(i) :=
IF     Distance(i) <= 100 THEN Distance(i)
ELSEIF Distance(i) <= 200 THEN (100 + Distance(i)) / 2
ELSEIF Distance(i) <= 300 THEN (250 + Distance(i)) / 3
ELSE   550 / 3
ENDIF ;


The expression takes the value associated with the first logical expression that is true.