Line: 1 to 1 | ||||||||
---|---|---|---|---|---|---|---|---|

<-- Ready to Review - done - Lauren--> ## Sets in AMPL | ||||||||

Line: 36 to 36 | ||||||||

- 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.
| ||||||||

Changed: | ||||||||

< < | { | |||||||

> > | { <e> in <S>, [<f> in <T>, <g> in <U>, …] : <logical expression involving e [f, g, …]>} | |||||||

Set expressions may also involve one or more set operators: | ||||||||

Line: 54 to 54 | ||||||||

Sets are usually defined in a data file: | ||||||||

Changed: | ||||||||

< < | set NODES := Youngstown Pittsburgh Cincinnati 'Kansas City' Chicago Albany Houston Tempe Gary ; | |||||||

> > | set NODES := Youngstown Pittsburgh Cincinnati 'Kansas City' Chicago Albany Houston Tempe Gary ; | |||||||

although they may be defined during declaration using either an explicit set literal or using a set expression: | ||||||||

Changed: | ||||||||

< < | 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; | |||||||

> > | 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; | |||||||

and sets may also be defined dynamically: | ||||||||

Changed: | ||||||||

< < | set SEARCH within VERTICES; let SEARCH := {v in VERTICES: (v, w) in EDGES}; | |||||||

> > | set SEARCH within VERTICES; let SEARCH := {v in VERTICES: (v, w) in EDGES}; | |||||||

## Defining 2-Dimensional SetsThere are three different ways to define 2-dimensional sets. The "best" way to use depends on the set. | ||||||||

Changed: | ||||||||

< < | -
**Using a List**You simply list the elements in the set. This is good for sparse sets.model; set ARCS within NODES cross NODES; data; set ARCS := (Youngstown, Albany), (Youngstown, Cincinnati), ... ; -
**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.set ARCS: Cincinnati 'Kansas City' Chicago Albany Houston Tempe Gary := Youngstown + + + + - - - Pittsburgh + + + - - - + Cincinnati - - - + + - - 'Kansas City' - - - - + + - Chicago - - - - - + + ; -
**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.set ARCS := (Youngstown, *) Cincinnati ‘Kansas City’ Chicago Albany (Pittsburgh, *) Cincinnati ‘Kansas City’ Chicago Gary (Cincinnati, *) Albany Houston ...
| |||||||

> > | -
**Using a List**You simply list the elements in the set. This is good for sparse sets.model; set ARCS within NODES cross NODES; data; set ARCS := (Youngstown, Albany), (Youngstown, Cincinnati), ... ; -
**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.
set ARCS: Cincinnati 'Kansas City' Chicago Albany Houston Tempe Gary := Youngstown + + + + - - - Pittsburgh + + + - - - + Cincinnati - - - + + - - 'Kansas City' - - - - + + - Chicago - - - - - + + ; -
**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.
set ARCS := (Youngstown, *) Cincinnati ‘Kansas City’ Chicago Albany (Pittsburgh, *) Cincinnati ‘Kansas City’ Chicago Gary (Cincinnati, *) Albany Houston ... | |||||||

Return to top | ||||||||

Line: 82 to 117 | ||||||||

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: | ||||||||

Changed: | ||||||||

< < | 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 | |||||||

> > | 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 | |||||||

Return to top | ||||||||

Line: 90 to 132 | ||||||||

Consider the following AMPL statement from the The American Steel Planning Problem. We use `ord` in the creation of `TIME_ARCS` : | ||||||||

Changed: | ||||||||

< < | # 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 | |||||||

> > | # 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 | |||||||

There are many concepts within this one statement, let's look at them one at a time. | ||||||||

Line: 99 to 147 | ||||||||

There are many operations we can perform on sets (see 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 looped over indexing sets. For example, to generate all the transportation arcs in the time-staged network you could use the following statement: | ||||||||

Changed: | ||||||||

< < | set TRANSPORT_ARCS := union {t in MONTHS} (union {(m, n) ARCS} {(m, t, n, t)});or you could loop over `MONTHS` and `ARCS` simultaneously: set TRANSPORT_ARCS := union {t in MONTHS, (m, n) in ARCS} {(m, t, n, t)}; | |||||||

> > | set TRANSPORT_ARCS := union {t in MONTHS} (union {(m, n) ARCS} {(m, t, n, t)});or you could loop over `MONTHS` and `ARCS` simultaneously:
set TRANSPORT_ARCS := union {t in MONTHS, (m, n) in ARCS} {(m, t, n, t)}; | |||||||

## Set Membership and Subsets |

View topic | History: r15 < r14 < r13 < r12 | More topic actions...

Copyright © 2008-2023 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.

Ideas, requests, problems regarding TWiki? Send feedback

Ideas, requests, problems regarding TWiki? Send feedback