---+<a name="top"></a> Logic in AMPL <ol> <li><a href="#relational">Relational Operators</a> <li><a href="#logical">Logical Expressions</a> <li><a href="#binary">Binary Parameters</a> <li><a href="#operators">Logical Operators</a> <li><a href="#conditional">Conditional Expressions</a> <li><a href="#structure">Conditional Structures</a> </ol> ---++<a name="relational"></a> Relational Operators Relational operators are used to compare two expressions. They are most commonly used in [[ConstraintsInAMPL][constraints]], but not exclusively. The relational operators are: | *Expression* | *Meaning* | | <tt><</tt> | Less than | | <tt><</tt> | Less than or equal to | | <tt>></tt> | Greater than | | <tt>>=</tt> | Greater than or equal to | | <tt>>=</tt> | Equal to | | <tt><></tt> | Not equal to | [[#top][Return to top]] <a name="logical"><h3>Logical Expressions</h3></a> Logical expressions are expressions that will evaluate to either true or false. Logical expressions are usually defined in terms of the <a href="#relational">relational operators: <p>\begin{verbatim} sum {i in INGREDIENTS} FatPercent[i] * Percentage[i] >= MinFat \end{verbatim}<p> <p>\begin{verbatim} sum {i in INGREDIENTS} FibrePercent[i] * Percentage[i] <= MaxFibre; \end{verbatim}<p> However, there are some special logical expressions in AMPL for use with <a href="Sets in AMPL">sets</a>: <ol> <li> {\tt <e> in <SET>} is _true_ if {\tt <e>} is a member of {\tt <SET>}; <li> {\tt <e> not in <SET>} is _false_ if {\tt <e>} is a member of {\tt <SET>}; <li> exists {<e> in <SET>} <expression> is _true_ if some <e> in <SET> has <expression> being true; <li> forall {<e> in <SET>} <expression> is _true_ if all <e> in <SET> have <expression> being true; <li><SUBSET> within <SET> is _true_ if all the elements in <SUBSET> are in <SET>; <li><SUBSET> not within <SET> is _true_ if some element in <SUBSET> is _not_ in <SET>. </ol> Logical expressions can be built up from other logical expressions, <a href="#binary">binary parameters</a> and <a href="#operators">logical operators</a>. <h1>Under Construction</h1> <a name="binary"><h3>Binary Parameters</h3></a> In AMPL we can create <i>binary</i> parameters by using the {\tt binary} keyword in the parameter declaration: <p>\begin{verbatim} param stillSearching binary; \end{verbatim}<p> 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 <a href="#conditional">conditional expressions</a> to hold a true/false result from a <a href="#logical">logical expression</a>: <p>\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}<p> so the syntax is <p>\begin{verbatim} \end{verbatim} or as the condition in a <a href="#condition">conditional statement</a> or <a href="#structure">conditional structure</a>. They are very useful for building complex conditional statements or structures: <p>\begin{verbatim} Some example from depth first searching or column generation \end{verbatim}<p> and controlling <a href="Loops in AMPL">loops</a>. <a name="operators"><h3>Logical Operators</h3></a> <\tt not} {\tt and} {\tt or} <a name="conditional"><h3>Conditional Expressions</h3></a> A conditional expression is very much like the IF function in Excel: param ifvalue := if <some logical expression> then <a value> [else <another value>]; If the logical expression is true then ifvalue will be set to <a value>, otherwise it is set to 0 (by default) or, if the else part of the expression is present, <another value>. Note that if the else keyword is present, then no ; needs to be included after <a value>. <a name="structure"><h3>Conditional Structures</h3></a> A conditional structure is the same as the classical if-then-else statement in programming languages like MATLAB, Fortran, Visual Basic and C++: if <logical expression> then <a statement>; [else <another statement>;] Note here that even if the else keyword is present you need to end <a statement> with ;. If you want to include more than one statement within the conditional structures you can use { and } to enclose your statements. if <logical expression> then { <some statements> } [else { <some other statements> }] -- Main.TWikiAdminGroup - 18 Mar 2008
This topic: OpsRes
>
WebHome
>
AMPLGuide
>
AMPLSyntax
>
LogicInAMPL
Topic revision: r2 - 2008-03-18 - TWikiAdminUser
Copyright © 2008-2025 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki?
Send feedback