1 | # |
---|
2 | # Imports |
---|
3 | # |
---|
4 | import sys |
---|
5 | sys.path.append("../../..") |
---|
6 | from coopr.pyomo import * |
---|
7 | ##pyomo.set_debugging() |
---|
8 | |
---|
9 | ## |
---|
10 | ## Creating a model |
---|
11 | ## |
---|
12 | model = 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 | # |
---|
21 | model.A = Set() |
---|
22 | # |
---|
23 | # An index set of sets can also be specified by providing sets as options |
---|
24 | # to the Set() object: |
---|
25 | # |
---|
26 | model.B = Set() |
---|
27 | model.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 | # |
---|
32 | model.D = model.A | model.B |
---|
33 | model.E = model.B & model.A |
---|
34 | model.F = model.A - model.B |
---|
35 | model.G = model.A ^ model.B |
---|
36 | # |
---|
37 | # Also, set cross-products can be specified as A*B |
---|
38 | # |
---|
39 | model.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 | # |
---|
44 | model.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 | # |
---|
54 | def 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 |
---|
60 | model.I = model.A*model.B |
---|
61 | model.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 | # |
---|
69 | model.J = Set() |
---|
70 | model.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 | # |
---|
76 | model.K = Set(initialize=[1,4,9]) |
---|
77 | model.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 | # |
---|
82 | model.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 | # |
---|
87 | def M_validate(value,model): |
---|
88 | return value in model.A |
---|
89 | model.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 | # |
---|
94 | model.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 | # |
---|
101 | def O_validate(value,model): |
---|
102 | return value[0] in model.A and value[1] in model.B |
---|
103 | model.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 | # |
---|
113 | def P_init(i, j, model): |
---|
114 | return range(0,i*j) |
---|
115 | model.P = Set(model.B,model.B) |
---|
116 | model.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 | # |
---|
142 | R_init={} |
---|
143 | R_init[2] = [1,3,5] |
---|
144 | R_init[3] = [2,4,6] |
---|
145 | R_init[4] = [3,5,7] |
---|
146 | model.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 | # |
---|
151 | model.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 | # |
---|
156 | def T_validate(value,model): |
---|
157 | return value in model.A |
---|
158 | model.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 | # |
---|
173 | def 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 |
---|
180 | model.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 | # |
---|
188 | def 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 |
---|
197 | model.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 | ## |
---|
203 | instance = model.create("set.dat") |
---|
204 | instance.pprint() |
---|