Changeset 1961


Ignore:
Timestamp:
Dec 5, 2009 2:46:24 AM (11 years ago)
Author:
wehart
Message:

Documentation updates for sets.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • coopr.pyomo/trunk/doc/pyomo/ref-set.tex

    r1808 r1961  
    1919\subsection{Set Declarations}
    2020
    21 A simple instance of \code{Set} objects declares an unordered set of arbitrary objects:
     21A simple instance of the \code{Set} object declares an unordered set of arbitrary objects:
    2222\begin{lstlisting}
    2323model.A = Set()
    2424\end{lstlisting}
    25 A set array can also be specified by providing sets as options
    26 to the \code{Set} object.  Multi-dimensional set arrays can be declared by simply including a list of sets as options to the \code{Set} object:
     25A set array can also be specified by providing a set as an option
     26to the \code{Set} object, and  multi-dimensional set arrays can be declared by simply including a list of sets as options to the \code{Set} object:
    2727\begin{lstlisting}
    2828model.B = Set()
     
    4848\end{lstlisting}
    4949
     50
    5051\subsection{Set Initialization}
    5152
    52 By default, a set object refers to an abstract set in a model.  However,
    53 a set can be initialized with data by using the \code{initialize} option, which is a function
    54 that accepts the set indices and model and returns the value of that set
    55 element:
     53By default, a set object refers to an abstract set in a model. 
     54However, a set can be explicitly initialized by adding set elements:
     55\begin{lstlisting}
     56model.J = Set()
     57model.J.add(1,4,9)
     58\end{lstlisting}
     59The \code{initialize} option can also be used to specify the values in
     60a set:
     61\begin{lstlisting}
     62model.K = Set(initialize=[1,4,9])
     63model.K_2 = Set(initialize=[(1,4),(9,16)])
     64\end{lstlisting}
     65The value of the \code{initialize} option can be a Python iterator.
     66Thus, sophisticated list comprehensions and generator expressions can be used:
     67\begin{lstlisting}
     68model.L = Set(initialize=(i for i in model.K if i%2 == 0))
     69\end{lstlisting}
     70
     71Two types of functions can be used with the \code{initialize} option.  If the
     72function accepts a model object, then this function is expected to return
     73an iterator containing data for the set:
    5674\begin{lstlisting}
    5775def I_init(model):
     
    6179        ans.append( (a,b) )
    6280    return ans
    63 model.I = model.A*model.B
    64 model.I.initialize = I_init
    65 \end{lstlisting}
    66 Note that the set \code{model.I} is not created when this set object is
    67 constructed.  Instead, \code{I\_init()} is called during the construction of a
    68 problem instance.
    69 
    70 A set can also be explicitly constructed by add set elements:
    71 \begin{lstlisting}
    72 model.J = Set()
    73 model.J.add(1,4,9)
    74 \end{lstlisting}
    75 The \code{initialize} option can also be used to specify the values in
    76 a set.  These default values may be overriden by later construction
    77 steps, or by data in an input file:
    78 \begin{lstlisting}
    79 model.K = Set(initialize=[1,4,9])
    80 model.K_2 = Set(initialize=[(1,4),(9,16)],dimen=2)
    81 \end{lstlisting}
    82 
    83 A set array can be constructed with the \code{initialize} option, which is a
    84 function that accepts the set indices and model and returns the set for that
    85 array index:
    86 \begin{lstlisting}
    87 def P_init(i, j, model):
    88     return range(0,i*j)
    89 model.P = Set(model.B,model.B)
    90 model.P.initialize = P_init
    91 \end{lstlisting}
    92 The \code{initialize} option can also be used to specify the values in
    93 a set array.  These default values are defined in a dictionary, which
     81model.I = Set(initialize=I_init)
     82\end{lstlisting}
     83If the function accepts a index and model, then this function is expected to return the set value assocated with that index:
     84\begin{lstlisting}
     85def M_init(i, model):
     86    return 2*i+1
     87model.M = Set(initialize=M_init)
     88\end{lstlisting}
     89
     90The \code{initialize} option can be used to specify the values in
     91a set array.  These values are defined in a dictionary, which
    9492specifies how each array element is initialized:
    9593\begin{lstlisting}
     
    117115after the execution of \code{model.create()}.
    118116
     117The \code{initialize} option can also specify a function that is used to provide values for a set array.  The function accepts the set indices and model and returns the set for that array index:
     118\begin{lstlisting}
     119def P_init(i, j, model):
     120    return range(0,i*j)
     121model.P = Set(model.B,model.B)
     122model.P.initialize = P_init
     123\end{lstlisting}
     124
     125Note that set data is not created when this set object is constructed.
     126Instead, initialization functions like \code{I\_init()} are called during
     127the construction of a problem instance.
     128Also, note that these values may be overriden by later construction
     129steps in a Pyomo model.  For example, data specified in an input file
     130will override the data specified by the \code{initialize} options.
     131
    119132
    120133\subsection{Data Validation}
     
    159172model.T = Set(model.B, validate=M_validate)
    160173\end{lstlisting}
     174
    161175
    162176\subsection{Set Options}
     
    199213\end{lstlisting}
    200214
     215
    201216\subsection{Class Attributes}
    202217
Note: See TracChangeset for help on using the changeset viewer.