---+<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"></a> Logical Expressions 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: <pre> Lower[r] <= sum {i in INGREDIENTS} Contributes[r, i] * Amount[i] </pre> <pre> sum {s in SURFBOARDS} Recipe[m, s] * Production[s] <= Supply[m]; </pre> However, there are some special logical expressions in AMPL for use with [[SetsInAMPL][sets]]: 1 =<e> in <SET>= is _true_ if =<e>= is a member of =<SET>=; 1 {\tt <e> not in <SET>} is _false_ if =<e>= is a member of =<SET>=; 1 =exists {<e> in <SET>} <expression>= is _true_ if some =<e>= in =<SET>= has =<expression>= being true; 1 =forall {<e> in <SET>} <expression>= is _true_ if all =<e>= in =<SET>= have =<expression>= being true; 1 =<SUBSET> within <SET>= is _true_ if all the elements in =<SUBSET>= are in =<SET>=; 1 =<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][binary parameters]] and [[#operators][logical operators]]. [[#top][Return to top]] ---++<a name="binary"></a> Binary Parameters 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: r3 - 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