<!-- Ready to Review --> ---+<a name="top"></a> Logic in AMPL <ol> <li><a href="#relational">Relational Operators</a> <li><a href="#operators">Logical Operators</a> <li><a href="#logical">Logical Expressions</a> <li><a href="#conditional">Conditional Expressions</a> <li><a href="#structure">Conditional Structures</a> <li><a href="#binary">Binary Parameters</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="operators"></a> Logical Operators Logical operators are used to combine [[#logical][logical expressions]]. They are most commonly used in [[#conditional][conditional statements]], [[#structure][conditional structures]] and [[LoopingInAMPL#condition][conditional loops]]. The logical operators are =not=, =and= and =or=. They are used as shown in the table below: | *Expression* | *Result* | | =not <expression>= | True if =<expression>= is false, false if =<expression>= is true | | =<e1> and <e2>= | True if =<e1>= and =<e2>= are both true, otherwise false | | =<e1> or <e2>= | True if either =<e1>= or =<e2>= are true, otherwise false | [[#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 [[#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 =<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="conditional"></a> Conditional Expressions A conditional expression is very much like the =IF= function in Microsoft Excel: <pre> param ifvalue := if <some logical expression> then <a value> [else <another value>]; </pre> 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>=. [[#top][Return to top]] ---++<a name="structure"></a> 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++: <pre> if <logical expression> then <a statement>; [else <another statement>;] </pre> 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: <pre> if <logical expression> then { <some statements> } [else { <some other statements> }] </pre> [[#top][Return to top]] ---++<a name="binary"></a> Binary Parameters In AMPL we can create _binary_ parameters by using the =binary= keyword in the parameter declaration: <pre> param stillSearching binary; </pre> 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][conditional expressions]] to hold a true/false result from a [[#logical][logical expression]]: <pre> 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) </pre> so the syntax is <pre> let <binary parameter> := if <expression> then 1; </pre> You can also set binary parameters within [[#structure][conditional structures]] <pre> binary <binary parameter>; if <expression> then let <binary parameter> := 1; else let <binary parameter> := 0; </pre> Binary parameters may be used in [[#logical][logical expressions]] or as the condition in a [[#condition][conditional statement]] or [[#structure][conditional structure]]. They are very useful for building complex conditional statements or structures: <pre> Some example from depth first searching or column generation </pre> %RED%Coming soon!%ENDCOLOR% and controlling [[LoopingInAMPL#condition][conditional loops]]: <pre> Some example from depth first searching or column generation </pre> %RED%Coming soon!%ENDCOLOR% [[#top][Return to top]] -- Main.TWikiAdminGroup - 18 Mar 2008
This topic: OpsRes
>
WebHome
>
AMPLGuide
>
AMPLSyntax
>
LogicInAMPL
Topic revision: r6 - 2008-04-06 - MichaelOSullivan
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