Tags:
create new tag
view all tags
<!-- Ready to Review --> ---+<a name="top"></a> Looping in AMPL <ol> <li>[[#for][<tt>for</tt> Loops]] <li>[[#let][<tt>let</tt> Loops]] <li>[[#condition][Conditional Loops]] </ol> ---++<a name="for"></a><tt>for</tt> Loops In AMPL you can create basic _for_ loops by [[SetsInAMPL][creating a set automatically]] and looping over elements in the set. To loop over a set you use the =for= keyword and the =in= keyword. <pre> for {<element> in <SET>} <a statement>; </pre> Now =<a statement>= is repeated for each element in =<SET>=. If you need to do more than one statement you can use ={= and =}=: <pre> for {<element> in <SET>} { <some statements> } </pre> %RED%Examples coming soon!%ENDCOLOR% [[#top][Return to top]] ---++<a name="let"></a><tt>let</tt> Loops In AMPL you can [[PrintingInAMPL][display and/or print over specified subsets]] and you can also "loop" over a =let= statement. To assign values for an entire set you use the =let= keyword, the set and the <tt>:=</tt> operator: <pre> let {e in SET} <parameter, usually involving e> := <expression, often involving e>; </pre> Consider the following possibility for the American Steel problems ([[AmericanSteelTransshipmentProblem][the American Steel transshipment problem]] and [[AmericanSteelPlanningProblem][the American Steel planning problem]] respectively). American Steel can get a 5\% discount with their transportation provider out of Chicago as long as they commit to at least 1000 tonnes along each route. This change can be easily incorporated using two looping =let= statements: <pre> let {n in NODES : ('Chicago', n) in ARCS} Cost['Chicago', n] := 0.95 * Cost['Chicago', n]; let {n in NODES : ('Chicago', n) in ARCS} Min['Chicago', n] := max(1000, Min['Chicago', n]); </pre> [[#top][Return to top]] ---++<a name="condition"></a> Conditional Loops AMPL also provides syntax for repeating a loop depending on a [[LogicInAMPL][condition]] (often known as _while_ loops). These loops use a condition, the =repeat= keyword, the =while= or =until= keyword and the ={= and =}= operators: <pre> repeat while <expression> { <loop statements;> }; repeat until <expression> { <loop statements;> }; </pre> These statements check the expression *before* executing the loop statements. The first one continues while the expression is true, the second terminates when the expression is true. AMPL provides loops that check the expression after the loop completes: <pre> repeat { <loop statements;> } while <expression>; repeat { <loop statements;> } until <expression>; </pre> As before, the first loop continues while the expression is true, the second terminates when the expression is true. %RED%Examples coming soon!%ENDCOLOR% [[#top][Return to top]] -- Main.TWikiAdminGroup - 18 Mar 2008
E
dit
|
A
ttach
|
Watch
|
P
rint version
|
H
istory
: r5
<
r4
<
r3
<
r2
<
r1
|
B
acklinks
|
V
iew topic
|
Ra
w
edit
|
M
ore topic actions
Topic revision: r5 - 2008-03-18
-
TWikiAdminUser
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