Difference: SetsInAMPL (6 vs. 7)

Revision 72008-03-11 - MichaelOSullivan

Line: 1 to 1
 
META TOPICPARENT name="AMPLSyntax"
<-- Ready to Review - done - Lauren-->

Sets in AMPL

Line: 23 to 23
 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:
set ARCS within NODES cross NODES; # Elements of ARCS must have both elements in NODES 
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:
set ROUTES dimen 2; 
There are some other set attributes, but we will not use them.

Set literals can be defined as a list of elements:

Changed:
<
<
{'HOST', 'DEVICE', 'SWITCH', 'HUB', 'LINK', 'SUPERLINK'} 
or a sequence of numbers:
param start; param end > start; param step;  set NUMBERS := start .. end by step; 
If the by step is missing, the step is assumed to be 1
set NUMBERS := 1..5; # NUMBERS = {1, 2, 3, 4, 5} 
Note Automatic set generation can only be done in the [[AMPLProcess#model][model environment] should this be a link? - Lauren , in the data environment you must define the set explicitly:
set NUMBERS := 1 2 3 4 5; # NUMBERS = {1, 2, 3, 4 5} 
>
>
{'HOST', 'DEVICE', 'SWITCH', 'HUB', 'LINK', 'SUPERLINK'} 
or a sequence of numbers:
param start; param end > start; param step;  set NUMBERS := start .. end by step; 
If the by step is missing, the step is assumed to be 1
set NUMBERS := 1..5; # NUMBERS = {1, 2, 3, 4, 5} 
Note Automatic set generation can only be done in the model environment, in the data environment you must define the set explicitly:
set NUMBERS := 1 2 3 4 5; # NUMBERS = {1, 2, 3, 4 5} 
  Return to top
Line: 32 to 32
 
  1. Define the elements that make up the set, e.g., s in SUPPLY_NODES, before the colon : operator;
  2. 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:
<
<
Generic Set Expression Fix underlining - Lauren
{  in , [ in ,  in , ...] :   } Set expressions may also involve one or more _set operators_: 
  1. =A union B= gives the set of elements in either =A= or =B=;
  2. =A inter B= gives the set of elements in both =A= and =B=;
  3. =A diff B= gives the set of elements in =A= that are not in =B=;
  4. =A symdiff B= gives the set of elements in either =A= or =B= but not both;
  5. =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]]

>
>
Generic Set Expression
{ <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:

  1. A union B gives the set of elements in either A or B;
  2. A inter B gives the set of elements in both A and B;
  3. A diff B gives the set of elements in A that are not in B;
  4. A symdiff B gives the set of elements in either A or B but not both;
  5. 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.

Return to top

 

Defining a Set

Changed:
<
<

Sets are usually defined in a data file:

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:
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:
set SEARCH within VERTICES;  let SEARCH := {v in VERTICES: (v, w) in EDGES}; 

>
>
Sets are usually defined in a data file:

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:

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:

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

Defining 2-Dimensional Sets

Changed:
<
<
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.
    model; set ARCS within NODES cros NODES; data; set ARCS := (Youngstown, Albany), (Youngstown, Cincinnati), ... ; 
  2. 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 - - - - - + + ; 
  3. 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

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

    model;
    set ARCS within NODES cros NODES;
    
    data;
    
    set ARCS := (Youngstown, Albany), (Youngstown, Cincinnati), ... ;
    
  2. 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            -          -         -      -       -      +    + ;
    
  3. 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

 

Ordered Sets

Changed:
<
<

You can create sets where the elements are ordered using the ordered keyword during definition.

set MONTHS ordered; 
AMPL will puts the elements in this set in the order they appear in the data file (or {\tt let} statement). AMPL also understands the following operations for ordered sets:
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

>
>
You can create sets where the elements are ordered using the ordered keyword during definition.

Up to here - Mike

set MONTHS ordered; 
AMPL will puts the elements in this set in the order they appear in the data file (or {\tt let} statement). AMPL also understands the following operations for ordered sets:
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

 

Set Example

Consider the following AMPL statement from the The American Steel Planning Problem. We use ord in the creation of TIME_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 arcs used for transportation     ( (m = n) and (ord(t, MONTHS) + 1 = ord(u, MONTHS)) )}; # The arcs used for storage 
There are many concepts within this one statement, let's look at them one at a time.

Set Operations

 
This site is powered by the TWiki collaboration platform Powered by PerlCopyright © 2008-2021 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback