source: coopr.pyomo/trunk/examples/pyomo/tutorials/table.py @ 2227

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

This is a major rework of the ModelData? logic. I've extracted the
actual operations for data loading into different plugins. These
plugins share a common code base for processing the data into a form that
Pyomo can use, but that logic is now separate from the ModelData? class itself.
This will provide a path-forward for cleaning up this code, as well as
modularizing the parsing of AMPL *.dat files.

File size: 2.6 KB
Line 
1#
2# Imports
3#
4import sys
5sys.path.append("../../..")
6from 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#
18model = Model()
19
20##
21## Declaring Sets
22##
23#
24# An unordered set of arbitrary objects
25#
26model.A = Set()
27#
28# An unordered set of numeric values
29#
30model.B = Set()
31#
32# A simple cross-product
33#
34model.C = model.A * model.B
35#
36# A simple cross-product loaded with a tabular data format
37#
38model.D = Set(within=model.A * model.B)
39#
40# A multiple cross-product
41#
42model.E = Set(within=model.A * model.B * model.A)
43
44#
45# An indexed set
46#
47model.F = Set(model.A)
48#
49# An indexed set
50#
51model.G = Set(model.A,model.B)
52#
53# A simple set
54#
55model.H = Set()
56#
57# A simple set
58#
59model.I = Set()
60#
61# A two-dimensional set
62#
63model.J = Set(dimen=2)
64
65##
66## Declaring Params
67##
68#
69#
70# A simple parameter
71#
72model.Z = Param()
73#
74# A single-dimension parameter
75#
76model.Y = Param(model.A)
77#
78# An example of initializing two single-dimension parameters together
79#
80model.X = Param(model.A)
81model.W = Param(model.A)
82#
83# Initializing a parameter with two indices
84#
85model.U = Param(model.I,model.A)
86model.T = Param(model.A,model.I)
87#
88# Initializing a parameter with missing data
89#
90model.S = Param(model.A)
91#
92# An example of initializing two single-dimension parameters together with
93# an index set
94#
95model.R = Param(model.H, within=Reals)
96model.Q = Param(model.H, within=Reals)
97#
98# An example of initializing parameters with a two-dimensional index set
99#
100model.P = Param(model.J, within=Reals)
101model.PP = Param(model.J, within=Reals)
102model.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
110data = ModelData(model=model)
111data.add("tab/A.tab")
112data.add("tab/B.tab")
113data.add("tab/C.tab", set="C")
114data.add("tab/D.tab", set="D", format="set_array")
115data.add("tab/E.tab", set="E")
116data.add("tab/I.tab")
117data.add("tab/Z.tab", param="Z")
118data.add("tab/Y.tab")
119data.add("tab/XW.tab")
120data.add("tab/T.tab", param="T", format="transposed_array")
121data.add("tab/U.tab", param="U", format="array")
122data.add("tab/S.tab")
123data.add("tab/RQ.tab", index="H")
124data.add("tab/PO.tab", index="J", param=("P","O"))
125data.add("tab/PP.tab", param="PP")
126data.read()
127
128instance = model.create(data)
129instance.pprint()
130
Note: See TracBrowser for help on using the repository browser.