source: coopr.pyomo/trunk/examples/pyomo/tutorials/set.py @ 2369

Last change on this file since 2369 was 2369, checked in by wehart, 10 years ago

Adding functionality for ordered sets.

File size: 5.8 KB
Line 
1#
2# Imports
3#
4import sys
5sys.path.append("../../..")
6from coopr.pyomo import *
7##pyomo.set_debugging()
8
9##
10## Creating a model
11##
12model = Model()
13
14##
15## Declaring Sets
16##
17#
18# An unordered set of arbitrary objects can be defined by creating a Set()
19# object:
20#
21model.A = Set()
22#
23# An index set of sets can also be specified by providing sets as options
24# to the Set() object:
25#
26model.B = Set()
27model.C = Set(model.A,model.B)
28#
29# Set declarations can also use standard set operations to declare
30# a set in a constructive fashion:
31#
32model.D = model.A | model.B
33model.E = model.B & model.A
34model.F = model.A - model.B
35model.G = model.A ^ model.B
36#
37# Also, set cross-products can be specified as A*B
38#
39model.H = model.A * model.B
40#
41# Note that this is different from the following, which specifies that Hsub
42# is a subset of this cross-product.
43#
44model.Hsub = Set(within=model.A * model.B)
45
46##
47## Data for Simple Sets
48##
49#
50# A set can be constructed with the _initialize_ option, which is a function
51# that accepts the set indices and model and returns the value of that set
52# element:
53#
54def I_init(model):
55    ans=[]
56    for a in model.A:
57      for b in model.B:
58        ans.append( (a,b) )
59    return ans
60model.I = model.A*model.B
61model.I.initialize = I_init
62#
63# Note that the set model.I is not created when this set object is
64# constructed.  Instead, I_init() is called during the construction of a
65# problem instance.
66#
67# A set can also be explicitly constructed by add set elements:
68#
69model.J = Set()
70model.J.add(1,4,9)
71#
72# The _initialize_ option can also be used to specify the values in
73# a set.  These default values may be overriden by later construction
74# steps, or by data in an input file:
75#
76model.K = Set(initialize=[1,4,9])
77model.K_2 = Set(initialize=[(1,4),(9,16)],dimen=2)
78#
79# Validation of set data is supported in two different ways.  First, a
80# superset can be specified with the _within_ option:
81#
82model.L = Set(within=model.A)
83#
84# Validation of set data can also be performed with the _validate_ option,
85# which is a function that returns True if a data belongs in this set:
86#
87def M_validate(value,model):
88    return value in model.A
89model.M = Set(validate=M_validate)
90#
91# Although the _within_ option is convenient, it can force the creation of
92# a temporary set.  For example, consider the declaration
93#
94model.N = Set(within=model.A*model.B)
95#
96# In this example, the cross-product of sets A and B is needed to validate
97# the members of set C.  Pyomo creates this set implicitly and uses
98# it for validation.  By contrast, a simple validation function could be used
99# in this example, though with a less intuitive syntax:
100#
101def O_validate(value,model):
102  return value[0] in model.A and value[1] in model.B
103model.O = Set(validate=O_validate)
104
105##
106## Data for Set Arrays
107##
108#
109# A set array can be constructed with the _initialize_ option, which is a
110# function that accepts the set indices and model and returns the set for that
111# array index:
112#
113def P_init(i, j, model):
114    return range(0,i*j)
115model.P = Set(model.B,model.B)
116model.P.initialize = P_init
117#
118# A set array CANNOT be explicitly constructed by adding set elements
119# to individual arrays.  For example, the following is invalid:
120#
121#   model.Q = Set(model.B)
122#   model.Q[2].add(4)
123#   model.Q[4].add(16)
124#
125# The reason is that the line
126#
127#   model.Q = Set(model.B)
128#
129# declares set Q with an abstract index set B.  However, B is not initialized
130# until the 'model.create()' call is executed at the end of this file.  We
131# could, however, execute
132#
133#   model.Q[2].add(4)
134#   model.Q[4].add(16)
135#
136# after the execution of 'model.create()'.
137#
138# The _initialize_ option can also be used to specify the values in
139# a set array.  These default values are defined in a dictionary, which
140# specifies how each array element is initialized:
141#
142R_init={}
143R_init[2] = [1,3,5]
144R_init[3] = [2,4,6]
145R_init[4] = [3,5,7]
146model.R = Set(model.B,initialize=R_init)
147#
148# Validation of a set array is supported with the _within_ option.  The
149# elements of all sets in the array must be in this set:
150#
151model.S = Set(model.B, within=model.A)
152#
153# Validation of set arrays can also be performed with the _validate_ option.
154# This is applied to all sets in the array:
155#
156def T_validate(value,model):
157    return value in model.A
158model.T = Set(model.B, validate=M_validate)
159
160##
161## Set options
162##
163#
164# By default, sets are unordered.  That is, the internal representation
165# may place the set elements in any order.  In some cases, we need to know
166# the order in which set elements are declared.  In such cases, we can declare
167# a set to be ordered with an additional constructor option. 
168#
169# An ordered set can take a initialization function with an additional option
170# that specifies the index into the ordered set.  In this case, the function is
171# called repeatedly to construct each element in the set:
172#
173def U_init(z, model):
174    if z==6:
175       return None
176    if z==1:
177       return 1
178    else:
179       return model.U[z-1]*z
180model.U = Set(ordered=True, initialize=U_init)
181#
182# This example can be generalized to array sets.  Note that in this case
183# we can use ordered sets to to index the array, thereby guaranteeing that
184# data has been filled.  The following example illustrates the use of the
185# RangeSet(a,b) object, which generates an ordered set from 'a' to 'b'
186# (inclusive).
187#
188def V_init(i, z, model):
189    if z==6:
190       return None
191    if i==1:
192       if z==1:
193          return 1
194       else:
195          return z
196    return model.V[i-1][z]+z-1
197model.V = Set(RangeSet(1,4), initialize=V_init, ordered=True)
198
199##
200## Process an input file and confirm that we get appropriate
201## set instances.
202##
203instance = model.create("set.dat")
204instance.pprint()
Note: See TracBrowser for help on using the repository browser.