1 | # |
---|
2 | # Imports |
---|
3 | # |
---|
4 | import sys |
---|
5 | sys.path.append("../../..") |
---|
6 | from coopr.pyomo import * |
---|
7 | #pyomo.set_debugging() |
---|
8 | |
---|
9 | ## |
---|
10 | ## Using a Model |
---|
11 | ## |
---|
12 | # |
---|
13 | # Pyomo makes a fundamental distinction between an abstract model and a |
---|
14 | # problem instance. The Pyomo Model() class is used to manage the |
---|
15 | # declaration of model components (e.g. sets and variables), and to |
---|
16 | # generate a problem instance. |
---|
17 | # |
---|
18 | model = Model() |
---|
19 | |
---|
20 | ## |
---|
21 | ## Declaring Sets |
---|
22 | ## |
---|
23 | # |
---|
24 | # An unordered set of arbitrary objects |
---|
25 | # |
---|
26 | model.A = Set() |
---|
27 | # |
---|
28 | # An unordered set of numeric values |
---|
29 | # |
---|
30 | model.B = Set() |
---|
31 | # |
---|
32 | # A simple cross-product |
---|
33 | # |
---|
34 | model.C = model.A * model.B |
---|
35 | # |
---|
36 | # A simple cross-product loaded with a tabular data format |
---|
37 | # |
---|
38 | model.D = Set(within=model.A * model.B) |
---|
39 | # |
---|
40 | # A multiple cross-product |
---|
41 | # |
---|
42 | model.E = Set(within=model.A * model.B * model.A) |
---|
43 | |
---|
44 | # |
---|
45 | # An indexed set |
---|
46 | # |
---|
47 | model.F = Set(model.A) |
---|
48 | # |
---|
49 | # An indexed set |
---|
50 | # |
---|
51 | model.G = Set(model.A,model.B) |
---|
52 | # |
---|
53 | # A simple set |
---|
54 | # |
---|
55 | model.H = Set() |
---|
56 | # |
---|
57 | # A simple set |
---|
58 | # |
---|
59 | model.I = Set() |
---|
60 | # |
---|
61 | # A two-dimensional set |
---|
62 | # |
---|
63 | model.J = Set(dimen=2) |
---|
64 | |
---|
65 | ## |
---|
66 | ## Declaring Params |
---|
67 | ## |
---|
68 | # |
---|
69 | # |
---|
70 | # A simple parameter |
---|
71 | # |
---|
72 | model.Z = Param() |
---|
73 | # |
---|
74 | # A single-dimension parameter |
---|
75 | # |
---|
76 | model.Y = Param(model.A) |
---|
77 | # |
---|
78 | # An example of initializing two single-dimension parameters together |
---|
79 | # |
---|
80 | model.X = Param(model.A) |
---|
81 | model.W = Param(model.A) |
---|
82 | # |
---|
83 | # Initializing a parameter with two indices |
---|
84 | # |
---|
85 | model.U = Param(model.I,model.A) |
---|
86 | model.T = Param(model.A,model.I) |
---|
87 | # |
---|
88 | # Initializing a parameter with missing data |
---|
89 | # |
---|
90 | model.S = Param(model.A) |
---|
91 | # |
---|
92 | # An example of initializing two single-dimension parameters together with |
---|
93 | # an index set |
---|
94 | # |
---|
95 | model.R = Param(model.H, within=Reals) |
---|
96 | model.Q = Param(model.H, within=Reals) |
---|
97 | # |
---|
98 | # An example of initializing parameters with a two-dimensional index set |
---|
99 | # |
---|
100 | model.P = Param(model.J, within=Reals) |
---|
101 | model.PP = Param(model.J, within=Reals) |
---|
102 | model.O = Param(model.J, within=Reals) |
---|
103 | |
---|
104 | ## |
---|
105 | ## Process an input file and confirm that we get appropriate |
---|
106 | ## set instances. |
---|
107 | ## |
---|
108 | #model.pprint() |
---|
109 | |
---|
110 | data = ModelData(model=model) |
---|
111 | data.add("tab/A.tab") |
---|
112 | data.add("tab/B.tab") |
---|
113 | data.add("tab/C.tab", set="C") |
---|
114 | data.add("tab/D.tab", set="D", format="set_array") |
---|
115 | data.add("tab/E.tab", set="E") |
---|
116 | data.add("tab/I.tab") |
---|
117 | data.add("tab/Z.tab", param="Z") |
---|
118 | data.add("tab/Y.tab") |
---|
119 | data.add("tab/XW.tab") |
---|
120 | data.add("tab/T.tab", param="T", format="transposed_array") |
---|
121 | data.add("tab/U.tab", param="U", format="array") |
---|
122 | data.add("tab/S.tab") |
---|
123 | data.add("tab/RQ.tab", index="H") |
---|
124 | data.add("tab/PO.tab", index="J", param=("P","O")) |
---|
125 | data.add("tab/PP.tab", param="PP") |
---|
126 | data.read() |
---|
127 | |
---|
128 | instance = model.create(data) |
---|
129 | instance.pprint() |
---|
130 | |
---|