Relational operators are used to compare two expressions. They are most commonly used in constraints, but not exclusively. The relational operators are:
Expression | Meaning |
---|---|
<> | Not equal to |
<= | Less than or equal to |
< | Less than |
>= | Greater than or equal to |
> | Greater than |
>= | Equal to |
Logical expressions are expressions that will evaluate to either true or false. Logical expressions are usually defined in terms of the relational operators:
Lower[r] <= sum {i in INGREDIENTS} Contributes[r, i] * Amount[i]
sum {s in SURFBOARDS} Recipe[m, s] * Production[s] <= Supply[m];
However, there are some special logical expressions in AMPL for use with sets:
<e> in <SET>
is true if <e>
is a member of <SET>
;
<e>
is a member of <SET>
;
exists {<e> in <SET>} <expression>
is true if some <e>
in <SET>
has <expression>
being true;
forall {<e> in <SET>} <expression>
is true if all <e>
in <SET>
have <expression>
being true;
<SUBSET> within <SET>
is true if all the elements in <SUBSET>
are in <SET>
;
<SUBSET> not within <SET>
is true if some element in <SUBSET>
is not in <SET>
.
Logical expressions can be built up from other logical expressions, binary parameters and logical operators.
In AMPL we can create binary parameters by using the {\tt binary} keyword in the parameter declaration:
\begin{verbatim} param stillSearching binary; \end{verbatim}
Binary parameters are used in a similar way to boolean variables (in Matlab, C, etc) and logical variables (Fortran). If a binary parameter has the value 0 this is equivalent to false, and 1 is equivalent to true. Binary parameters can be used with conditional expressions to hold a true/false result from a logical expression:
\begin{verbatim} param isGreater binary;
let isGreater := if 4 > 5 then 1 else 0; # isGreater = 0 (false) let isGreater := if 6 > 5 then 1; # else 0 is the default, isGreater = 1 (true) \end{verbatim}
so the syntax is
\begin{verbatim} \end{verbatim} or as the condition in a conditional statement or conditional structure. They are very useful for building complex conditional statements or structures:
\begin{verbatim} Some example from depth first searching or column generation \end{verbatim}
and controlling loops.
<\tt not} {\tt and} {\tt or}
A conditional expression is very much like the IF function in Excel:
param ifvalue := if
If the logical expression is true then ifvalue will be set to , otherwise it is set to 0 (by default) or, if the else part of the expression is present,
A conditional structure is the same as the classical if-then-else statement in programming languages like MATLAB, Fortran, Visual Basic and C++:
if
-- TWikiAdminGroup - 18 Mar 2008