Tags:
create new tag
view all tags
<!-- Ready to Review - done - Lauren--> ---+ <a name=top"></a> Sets in AMPL 1 [[#describe][Description]] 1 [[#declare][Declaring a Set]] 1 [[#expression][Set Expressions]] 1 [[#define][Defining a Set]] 1 [[#ordered][Ordered Sets]] 1 [[#example][Set Example]] 1 [[#restricted][Restricted Sets]] 1 [[#multi][Multi-Dimensional Sets]] 1 [[#generate][Efficient Generation of Sets]] ---++ <a name="describe"></a> Description Sets are one of the most fundamental concepts in AMPL. They are used to index variables, constraints, parameters and sometimes other sets. Most expressions involve an operation being performed over a set. Sets allow for large mathematical programmes to be expressed concisely. [[#top][Return to top]] ---++ <a name="declare"></a> Declaring a Set Sets are declared using the =set= keyword followed by a label, possibly some attributes and either a set literal or set expression. The most common attribute is set by the =within= keyword. This specifies that the set will only contain elements from the following set definition: <pre>set ARCS within NODES cross NODES; # Elements of ARCS must have both elements in NODES </pre> If you need a multi-dimensional set, but don't have the 1-dimensional sets to construct it yet you can use the =dimen= keyword: <pre>set ROUTES dimen 2; </pre> There are some other set attributes, but we will not use them here. Set literals can be defined as a list of elements: <pre>{'HOST', 'DEVICE', 'SWITCH', 'HUB', 'LINK', 'SUPERLINK'} </pre> or a sequence of numbers: <pre>param start; param end > start; param step; set NUMBERS := start .. end by step; </pre> If the =by step= is missing, the step is assumed to be 1: <pre>set NUMBERS := 1..5; # NUMBERS = {1, 2, 3, 4, 5} </pre> *Note* Automatic set generation can only be done in the [[AMPLProcess#model][model environment]], in the [[AMPLProcess#data][data environment]] you must define the set explicitly: <pre>set NUMBERS := 1 2 3 4 5; # NUMBERS = {1, 2, 3, 4 5} </pre> [[#top][Return to top]] ---++ <a name="expression"></a> Set Expressions Set expressions take the following form (which can be extended to higher dimensional sets): 1 Define the elements that make up the set, e.g., =s in SUPPLY_NODES=, before the colon =:= operator; 1 Use a logical expression (after the =:=) to indicate if an element (or pair of elements, or “tuple” of elements) should be included in the set. *Generic Set Expression* <pre>{ <e> in <S>, [<f> in <T>, <g> in <U>, …] : <logical expression involving e [f, g, …]>} </pre> Set expressions may also involve one or more _set operators_: 1 =A union B= gives the set of elements in either =A= or =B=; 1 =A inter B= gives the set of elements in both =A= and =B=; 1 =A diff B= gives the set of elements in =A= that are not in =B=; 1 =A symdiff B= gives the set of elements in either =A= or =B= but not both; 1 =A cross B= gives the two-dimensional set of all pairs =a= %$\in$% =A=, =b= %$\in$% =B=. This can also be defined by ={a in A, b in B}=. You will see examples of set expressions throughout the rest of this page. [[#top][Return to top]] ---++ <a name="define"></a>Defining a Set Sets are usually defined in a data file: <pre> set NODES := Youngstown Pittsburgh Cincinnati 'Kansas City' Chicago Albany Houston Tempe Gary ; </pre> although they may be defined during declaration using either an explicit set literal or using a set expression: <pre> set KIND := {'HOST', 'DEVICE', 'SWITCH', 'HUB', 'LINK', 'SUPERLINK'}; set COMPONENT := {C in CLASSES : (kind[C] = 'HOST' ) or (kind[C] = 'DEVICE') or (kind[C] = 'HUB' ) or (kind[C] = 'SWITCH')}; set FABRIC := NODE union LINK; </pre> and sets may also be defined dynamically: <pre> set SEARCH within VERTICES; let SEARCH := {v in VERTICES: (v, w) in EDGES}; </pre> ---+++ Defining 2-Dimensional Sets There are three different ways to define 2-dimensional sets. The "best" way to use depends on the set. 1 *Using a List* You simply list the elements in the set. This is good for sparse sets.</p> <pre> model; set ARCS within NODES cross NODES; data; set ARCS := (Youngstown, Albany), (Youngstown, Cincinnati), ... ; </pre> 1 *Using a Table* You give a table using the first index set for the rows and the second index set for the columns, then you place a =+= where an element exists and a =-= where there is no element. This is good for dense sets. <pre> set ARCS: Cincinnati 'Kansas City' Chicago Albany Houston Tempe Gary := Youngstown + + + + - - - Pittsburgh + + + - - - + Cincinnati - - - + + - - 'Kansas City' - - - - + + - Chicago - - - - - + + ; </pre> 1 *Using an Array* You define a list of column indices for each row index. This is a good for sets with a few elements for each row. <pre> set ARCS := (Youngstown, *) Cincinnati ‘Kansas City’ Chicago Albany (Pittsburgh, *) Cincinnati ‘Kansas City’ Chicago Gary (Cincinnati, *) Albany Houston ... </pre> [[#top][Return to top]] ---++ <a name="ordered"></a> Ordered Sets You can create sets where the elements are ordered using the =ordered= keyword during definition. <pre>set MONTHS ordered; </pre> AMPL will put the elements in this set in the order they appear in the data file (or =let= statement). AMPL also understands the following operations for ordered sets: <pre> ord(e, ORD_SET) # The position of e in ORD_SET first(ORD_SET) # The first element in ORD_SET last(ORD_SET) # The last element in ORD_SET prev(e, ORD_SET) # The element before e in ORD_SET next(e, ORD_SET) # The element after e in ORD_SET member(i, ORDSET) # The element at position i in ORD_SET </pre> [[#top][Return to top]] ---++ <a name="example"></a> Set Example Consider the following AMPL statement from the [[AmericanSteelPlanningProblem][The American Steel Planning Problem]]. We use =ord= in the creation of =TIME_ARCS=: <pre> # The set of time-staged arcs set TIME_ARCS within TIME_NODES cross TIME_NODES := { (m, t) in TIME_NODES, (n, u) in TIME_NODES : ( ( (m, n) in ARCS) and (t = u) ) or # The transportation arcs ( (m = n) and (ord(t, MONTHS) + 1 = ord(u, MONTHS)) )}; # The storage arcs </pre> There are many concepts within this one statement, let's look at them one at a time. ---+++ Set Operations There are many operations we can perform on sets (see [[#expressions][Set Expressions]]). We have seen that =cross= creates all pairs of two sets, so =TIME_NODES cross TIME_NODES= creates a set of all pairs of =TIME_NODES=. Some set operations may be [[LoopingInAMPL][looped]] over indexing sets. For example, to generate all the transportation arcs in the time-staged network you could use the following statement: <pre> set TRANSPORT_ARCS := union {t in MONTHS} (union {(m, n) ARCS} {(m, t, n, t)}); </pre> or you could loop over =MONTHS= and =ARCS= simultaneously: <pre> set TRANSPORT_ARCS := union {t in MONTHS, (m, n) in ARCS} {(m, t, n, t)}; </pre> ---+++ Set Membership and Subsets We have seen how to loop over a set using the =in= keyword. This keyword also provides a [[LogicInAMPL][logical]] check if an element is in a set, e.g., =(m, n) in ARCS= is true if the pair =(m, n)= is in the set =ARCS= and false otherwise. We may restrict a set to be a subset of an existing set by using the keyword =within=, e. g., <pre>set ARCS within NODES cross NODES; </pre> means each arc is created between two nodes. ---+++ Ordered Set Operators The final condition on =TIME_ARCS= <pre> ( (m = n) and (ord(t, MONTHS) + 1 = ord(u, MONTHS)) )}; </pre> creates the "storage" arcs. We could use next(t, MONTHS) = u or t = prev(u, MONTHS) except for a problem when =t= is =June= or =u= is =April=, respectively. When you use =prev= or =next= you must be careful of the first and last members of the set respectively. However, you can use =first= or =last= to check if you are using these elements. [[#top][Return to top]] ---++ <a name="restricted"></a> Restricted Sets When [[PrintingInAMPL][using display or printf statements]] we saw that we could restrict the members of a set being printed, e.g., <pre>display {(m, n) in ARCS : Supply[m] > 0}; # Display all arcs from supply nodes </pre> We can do this when creating sets, e. g., =TIME_NODES=, or when using a set as an index for variables, parameters or constraints. For example, rather than setting the upper bound of =UnderProduction= to be 0 for all non-supply nodes (since non-supply nodes don't produce anything) we could only create this variable for the supply nodes (in fact this may be preferable since there will be less variables). <pre>var UnderProduction {(n, t) in TIME_NODES : Supply[n, t] > 0} >= 0, integer; </pre> We could make sure this variable is only added to the constraints for the supply nodes (e.g., =ConserveFlow= constraints) by using a conditional expression. <pre>subject to ConserveFlow {(n, t) in TIME_NODES}: sum {(m, s) in TIME_NODES: (m, s, n, t) in TIME_ARCS} Shipment[m, s, n, t] + Supply[n, t] - if Supply[n, t] > 0 then UnderProduction[n, t] = ... </pre> [[#top][Return to top]] ---++ <a name="multi"></a> Multi-dimensional Sets We have already seen different ways of [[#define][declaring 2-dimensional sets]]. We have now encountered higher dimensional sets, e.g., =TIME_ARCS= has 4 dimensions. We generated =TIME_ARCS= automatically, but we could have specified it using a data file. <pre>set TIME_ARCS within TIME_NODES cross TIME_NODES; </pre> *List* <pre>set TIME_ARCS := (Youngstown, April, Albany, April) (Youngstown, April, Youngstown, May) ... ; </pre> *Table* <pre>set TIME_ARCS : = (*, May, *, May) Cincinnati 'Kansas City' Albany ... := Youngstown + + + ... Pittsburgh + + - ... ... ; </pre> *Array* <pre>set TIME_ARCS := (*, May, *, May) (Youngstown, Cincinnati) ... ... ; </pre> or <pre>set TIME_ARCS := (Youngstown, May, *, May) Cincinnati 'Kansas City' ... ... ; </pre> [[#top][Return to top]] ---++ <a name="generate"></a> Efficient Generation of Sets When creating models, generating sets by looping over all possibilities and removing those that don't fit some conditions (e.g., like the =TIME_ARCS= statement) can take a long time if there are many possibilities. This is time that could be spent solving the model! If possible you should try to create sets by building them up from smaller building block, rather than by creating an enormous set and pruning it. For example, instead of using this statement <pre># The set of time-staged arcs set TIME_ARCS within TIME_NODES cross TIME_NODES := { (m, t) in TIME_NODES, (n, u) in TIME_NODES : ( ( (m, n) in ARCS) and (t = u) ) or # The transportation arcs ( (m = n) and (ord(t, MONTHS) + 1 = ord(u, MONTHS)) )}; # The storage arcs </pre> to create =TIME_ARCS= you could use these statements <pre>set TRANSPORT_ARCS := union {t in MONTHS, (m, n) in ARCS} {(m, t, n, t)}; set STORAGE_ARCS := union {t in MONTHS, (m, n) in ARCS : t <> last(MONTHS)} {(m, t, n, next(t, MONTHS)}; set TIME_ARCS within TIME_NODES cross TIME_NODES := TRANSPORT_ARCS union STORAGE_ARCS; </pre> Rather than looping over all possibilities and only keeping those that are appropriate, the new statement only loops over smaller sets that can be used to build up the =TIME_NODES= set efficiently. [[#top][Return to top]] -- Main.MichaelOSullivan - 27 Feb 2008
E
dit
|
A
ttach
|
Watch
|
P
rint version
|
H
istory
: r15
<
r14
<
r13
<
r12
<
r11
|
B
acklinks
|
V
iew topic
|
Ra
w
edit
|
M
ore topic actions
Topic revision: r15 - 2019-11-10
-
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