Logical Expressions
Logical expressions
Logical expressions are expressions that evaluate to a logical value-0.0 for false and 1.0 for true. AIMMS supports several types of logical expressions.
logical-expression:
Numerical expressions as logical
As AIMMS permits numerical expressions as logical expressions it is
important to discuss how numerical expressions are interpreted
logically, and how logical expressions are interpreted numerically.
Numerical expressions that evaluate to zero (0.0) are false, while all
others (including ZERO
, NA
and UNDF
) are true. A false
logical expression evaluates to zero (0.0), while a true logical
expression evaluates to one (1.0). If one or more of the operands of a
logical operator is UNDF
or NA
, the numerical value is also
UNDF
or NA
. Note that AIMMS will not accept expressions that
evaluate to UNDF
or NA
in 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).
Example
this table illustrates the different
interpretation of a number of numerical and logical expressions as
either a numerical or a logical expression. See also
this table for the results associated with the
AND
operator.
Expression |
Numerical value |
Logical value |
---|---|---|
|
3.0 |
true |
|
0.0 |
false |
|
2.0 |
true |
|
1.0 |
true |
|
0.0 |
false |
|
1.0 |
true |
|
|
true |
|
|
true |
Logical Operator Expressions
Unary and binary logical operators
AIMMS supports the unary logical operator NOT
and the binary logical operators AND
, OR
, and
XOR
. this table gives the logical results of
these operators for zero and nonzero operands.
Operands |
Result |
||||
---|---|---|---|---|---|
|
|
|
|
|
|
0 |
0 |
0 |
0 |
0 |
1 |
0 |
nonzero |
0 |
1 |
1 |
1 |
nonzero |
0 |
0 |
1 |
1 |
0 |
nonzero |
nonzero |
1 |
1 |
0 |
0 |
Precedence order
The precedence order of these operators from highest to lowest is given
by NOT
, AND
, OR
, and XOR
respectively. Whenever the
precedence order is not immediately clear, it is advisable to use
parentheses. Besides preventing unwanted mistakes, it also make your
model easier to understand and maintain.
Example
The expression
NOT a AND b XOR c OR d
is parsed by AIMMS as if it were written
((NOT a) AND b) XOR (c OR d).
Execution order
Due to the sparse execution system underlying AIMMS it is not guaranteed
that logical expressions containing binary logical operators are
executed in a strict left-to-right order. If you are a C
/C++
programmer (where logical conditions are executed in a strict
left-to-right order), you should take extra care to ensure that your
logical conditions do not depend on this assumption.
Numerical Comparison
Numerical comparison
Numerical relationships compare two numerical expressions, using one of
the relational operators =
, <>
, >
, >=
, <
, or <=
.
Numerical inclusions are equivalent to two numerical relationships, and
indicate whether a given expression lies within two bounds.
Syntax
expression-relationship:
expression-inclusion:
Numerical tolerances
For two real numbers \(x\) and \(y\) the result of the comparison \(x \gtrless y\), where \(\gtrless\) denotes any relational operator, depends on two tolerances
Equality_Absolute_Tolerance
(denoted as \(\varepsilon_a\)), andEquality_Relative_Tolerance
(denoted as \(\varepsilon_r\)).
You can set these tolerances through the options dialog box. Their default values are \(0\) and \(10^{-13}\), respectively. If the number \(\varepsilon_{x,y}\) is given by the formula
then the relational operators evaluate as shown in the this table.
AIMMS expression |
Evaluates as |
---|---|
\(x \mathtt{=} y\) |
\(|x-y| \leq \varepsilon_{x,y}\) |
\(x \mathtt{<>} y\) |
\(|x-y| > \varepsilon_{x,y}\) |
\(x \mathtt{<=} y\) |
\(x-y \leq \varepsilon_{x,y}\) |
\(x \mathtt{<} y\) |
\(x-y < -\varepsilon_{x,y}\) |
Comparison for extended arithmetic
For any combination of an ordinary real number with one of the special
symbols ZERO
, INF
, and -INF
, the relational operators behave
as expected. If any of the operands is either NA
or UNDF
,
relationships other than =
and <>
also evaluate to NA
or
UNDF
and hence, as a logical expression, to true. In addition, the
logical expressions INF = INF
and -INF = -INF
evaluate to true.
Testing for zero value
One can formulate numerous logical expressions to test for a zero value, and one should be clear on the desired result. The following example makes the point.
p_inv(i) := 1 / p(i);
p_inv(i | p(i)) := 1 / p(i);
p_inv(i | p(i) <> 0) := 1 / p(i);
The first assignment will produce a runtime error when p(i)
assumes
a value of 0 or ZERO
. The second assignment will filter out the 0’s,
but not the ZERO
values because ZERO
evaluates to the logical
value “true”. The last assignment will never produce runtime errors,
because of the numerical comparison to 0.
Set and Element Comparison
Set relationships
AIMMS features very powerful logical set comparison operators. Not only
can sets and their elements be compared using relational operators, but
you can also check for set membership with the IN
operator.
Syntax
set-relationship:
Element relationship and inclusion
Set elements that lie in the same set can be compared according to their
relative position inside that set. You can also compare the positions of
arbitrary set element expressions, as long as AIMMS is able to determine
a unique domain set in which the comparison has to take place. The
allowed relational operators are =
, <>
, <
, <=
, >
,
and >=
. As with numerical expression, AIMMS also allows you to
specify an inclusion relationship as a form of repeated comparison to
verify whether an element lies within two boundary elements.
Element comparison
The relational operators for element relationships are conveniently
defined in terms of the Ord
function. Let S
be a simple set,
i
and j
indices or element parameters in S
, \(\pm\) any
of the lag or lead operators +
, ++
, -
or -
, \(m\)
and \(n\) integer expressions, and \(\gtrless\) one of the
operators =
, <>
, <
, <=
, >
, or >=
. The relational
operators \(\gtrless\) have the following definition for set
elements, provided that the set elements on both sides of the relational
operator exist.
Note that this type of relational expression evaluates to “false” if one or both of the operands do not refer to existing set elements.
Compare within the same set
Only elements that lie in the same set are comparable using the <
,
<=
, >
, and >=
operators. The =
and <>
operators can
also be used when the operands merely share the same root set.
Example
The following set assignments demonstrate the correct use of element comparisons.
FuturePeriods := { t in Periods | CurrentPeriod <= t <= PlanningHorizon } ;
BandMatrix := { (i,j) | i - BandWidth <= j <= i + BandWidth } ;
Set membership
Set membership can be tested using the IN
operator. This operator
checks whether a set element or an element tuple on the left-hand side
is a member of the set expression on the right-hand side. Both operands
must have the same root set.
Example
Assume that all one-dimensional sets in the following two assignments
share the same root set Cities
. Then these statements illustrate the
correct use of the logical IN
operator.
NeighborhoodRoutes := { (i,j) in Routes | j in NeighborhoodCities(i) } ;
ExcludedCities := { i in ( SmallCities + ForeignCities ) } ;
Set comparisons
Sets can be logically compared using any of the relational operators
=
, <>
, <
, <=
, >
and >=
. The inequality operators
denote the usual subset relationships. They replace the standard
“contained in” operators \(\subsetneq\), \(\subseteq\),
\(\supsetneq\) and \(\supseteq\) which are not available on many
keyboards.
Example
The following statement illustrates a logical set comparison operator.
IF ( RoutesWithTransport <= NeighborhoodRoutes ) THEN
DialogMessage( "Solution only contains neighborhood transports" );
ENDIF;
String Comparison
String comparison
Besides their use for comparison of numerical, element- and set-valued
expressions, the relational operators =
, <>
, <
, <=
,
>
, and >=
can also be used for string comparison. When used for
string comparison, AIMMS employs the usual lexicographical ordering.
String comparison in AIMMS is case sensitive by default, i.e. strings
that only differ in case are considered to be unequal. You can modify
this behavior through the option Case_Sensitive_String_Comparison
.
Examples
All the following string comparisons evaluate to true.
"The city of Amsterdam" <> "the city of amsterdam" ! Note case
"The city of Amsterdam" <> "The city of Amsterdam " ! Note last space
"The city of Amsterdam" < "The city of Rotterdam"
Logical Iterative Expressions
Logical iterative operators
Logical iterative operators verify whether some or all elements in a domain satisfy a certain logical condition. this table lists all logical iterative operators supported by AIMMS. The second column in this table refers to the required number of expression arguments following the binding domain argument.
Name |
# Expr. |
Meaning |
---|---|---|
|
0 |
true if the domain is not empty |
|
1 |
true if the domain contains at least \(n\) elements |
|
1 |
true if the domain contains at most \(n\) elements |
|
1 |
true if the domain contains at exactly \(n\) elements |
|
1 |
true if the expression is true for all elements in the domain |
Example
The following statements illustrate the use of some of the logical iterative operators listed in this table.
MultipleSupplyCities := { i | Atleast( j | Transport(i,j), 2 ) } ;
IF ( ForAll( i, Exists( j | Transport(i,j) ) ) ) THEN
DialogMessage( "There are no cities without a transport" );
ENDIF ;