Tags:
create new tag
view all tags
<!-- Ready to Review --> ---+ <a name="top"></a> Logic in AMPL 1 <a href="#relational">Relational Operators</a> 1 <a href="#operators">Logical Operators</a> 1 <a href="#logical">Logical Expressions</a> 1 <a href="#conditional">Conditional Expressions</a> 1 <a href="#structure">Conditional Structures</a> 1 <a href="#binary">Binary Parameters</a> ---++ <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* | | < | Less than | | <= | Less than or equal to | | > | Greater than | | >= | Greater than or equal to | | == | Equal to | | <> | 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 <span><expression></span> is false, false if <span><expression></span> is true | | =<e1> and <e2>= | True if <span><e1></span> and <span><e2></span> are both true, otherwise false | | =<e1> or <e2>= | True if either <span><e1></span> or <span><e2></span> 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 <span class="WYSIWYG_TT"><e> in <SET></span> is _true_ if <span class="WYSIWYG_TT"><e></span> is a member of <span class="WYSIWYG_TT"><SET></span>; 1 <span class="WYSIWYG_TT"><e> not in <SET></span> is _false_ if <span class="WYSIWYG_TT"><e></span> is a member of <span class="WYSIWYG_TT"><SET></span>; 1 <span class="WYSIWYG_TT">exists {<e> in <SET>} <expression></span> is _true_ if some <span class="WYSIWYG_TT"><e></span> in <span class="WYSIWYG_TT"><SET></span> has <span class="WYSIWYG_TT"><expression></span> being true; 1 <span class="WYSIWYG_TT">forall {<e> in <SET>} <expression></span> is _true_ if all <span class="WYSIWYG_TT"><e></span> in <span class="WYSIWYG_TT"><SET></span> have <span class="WYSIWYG_TT"><expression></span> being true; 1 <span class="WYSIWYG_TT"><SUBSET> within <SET></span> is _true_ if all the elements in <span class="WYSIWYG_TT"><SUBSET></span> are in <span class="WYSIWYG_TT"><SET></span>; 1 <span class="WYSIWYG_TT"><SUBSET> not within <SET></span> is _true_ if some element in <span class="WYSIWYG_TT"><SUBSET></span> is _not_ in <span class="WYSIWYG_TT"><SET></span>. 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 <span><a value></span>, otherwise it is set to 0 (by default) or, if the else part of the expression is present, <span><another value></span>. Note that if the =else= keyword is present, then no =;= needs to be included after <span><a value></span>. [[#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 <span><a statement></span> 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
E
dit
|
A
ttach
|
Watch
|
P
rint version
|
H
istory
: r7
<
r6
<
r5
<
r4
<
r3
|
B
acklinks
|
V
iew topic
|
Ra
w
edit
|
M
ore topic actions
Topic revision: r7 - 2009-09-18
-
MichaelOSullivan
Home
Site map
Forum web
Main web
NDSG web
ORUA web
OpsRes web
Sandbox web
TWiki web
OpsRes Web
Create New Topic
Index
Search
Changes
Notifications
RSS Feed
Statistics
Preferences
P
P
View
Raw View
Print version
Find backlinks
History
More topic actions
Edit
Raw edit
Attach file or image
Edit topic preference settings
Set new parent
More topic actions
Account
Log In
E
dit
A
ttach
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