# Logic in AMPL

## Relational Operators

Relational operators are used to compare two expressions. They are most commonly used in constraints, but not exclusively. The relational operators are:

Expression Meaning
< Less than
< Less than or equal to
> Greater than
>= Greater than or equal to
>= Equal to
<> Not equal to

### Logical Expressions

Logical expressions are expressions that will evaluate to either true or false. Logical expressions are usually defined in terms of the relational operators:

\begin{verbatim} sum {i in INGREDIENTS} FatPercent[i] * Percentage[i] >= MinFat \end{verbatim}

\begin{verbatim} sum {i in INGREDIENTS} FibrePercent[i] * Percentage[i] <= MaxFibre; \end{verbatim}

However, there are some special logical expressions in AMPL for use with sets:

1. {\tt <e> in <SET>} is true if {\tt <e>} is a member of {\tt <SET>};
2. {\tt <e> not in <SET>} is false if {\tt <e>} is a member of {\tt <SET>};
3. exists { in } is true if some in has being true;
4. forall { in } is true if all in have being true;
5. within is true if all the elements in are in ;
6. not within is true if some element in is not in .

Logical expressions can be built up from other logical expressions, binary parameters and logical operators.

# Under Construction

### Binary Parameters

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.

### Logical Operators

<\tt not} {\tt and} {\tt or}

### Conditional Expressions

A conditional expression is very much like the IF function in Excel: param ifvalue := if then [else ];

### Conditional Structures

A conditional structure is the same as the classical if-then-else statement in programming languages like MATLAB, Fortran, Visual Basic and C++: if then ; [else ;] Note here that even if the else keyword is present you need to end with ;. If you want to include more than one statement within the conditional structures you can use { and } to enclose your statements. if then { } [else { }]

-- TWikiAdminGroup - 18 Mar 2008

Edit | Attach | Watch | Print version |  | Backlinks | Raw View | Raw edit | More topic actions...
Topic revision: r2 - 2008-03-18 - TWikiAdminUser     Edit Attach Copyright © 2008-2021 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback