source: coopr.pyomo/stable/2.2/coopr/pyomo/tests/expr/test_canonical.py @ 2230

Last change on this file since 2230 was 2230, checked in by wehart, 11 years ago

Merged revisions 2208-2229 via svnmerge from
https://software.sandia.gov/svn/public/coopr/coopr.pyomo/trunk

........

r2209 | jwatson | 2010-01-30 17:01:12 -0700 (Sat, 30 Jan 2010) | 3 lines


Fix to new LP writer to output variable label, instead of name. The name has embedded quotes, which hoses all kinds of things. The label doesn't.

........

r2210 | jwatson | 2010-01-30 20:11:21 -0700 (Sat, 30 Jan 2010) | 3 lines


Additional minor fixings to make new LP writer work with PH - which it now does.

........

r2211 | jwatson | 2010-01-30 20:17:19 -0700 (Sat, 30 Jan 2010) | 3 lines


Suppressing warning for "constant constraint" when writing LP files - this happens all the time in PH and other contexts, where variable fixing is active.

........

r2213 | wehart | 2010-01-30 23:03:26 -0700 (Sat, 30 Jan 2010) | 2 lines


Adding the ability to process multiple *.dat files on the Pyomo command line

........

r2214 | wehart | 2010-01-31 14:44:12 -0700 (Sun, 31 Jan 2010) | 3 lines


Reworking the canonical expression definition to include a
dictionary from variable ID -> _VarValue object.

........

r2217 | wehart | 2010-01-31 21:48:34 -0700 (Sun, 31 Jan 2010) | 2 lines


Update to NL file writer, after recent changes in the canonical representation.

........

r2218 | wehart | 2010-01-31 22:11:59 -0700 (Sun, 31 Jan 2010) | 7 lines


Reworking the symbol_map generated for the NL file.


Fixing the use of this within PyomoModel? when loading data.


Adding logic to specify the 'solver' option when an optimization interface
is specified (e.g. asl:cplexamp).

........

r2226 | wehart | 2010-01-31 22:27:19 -0700 (Sun, 31 Jan 2010) | 2 lines


Removing some debugging IO.

........

r2227 | wehart | 2010-02-01 14:40:08 -0700 (Mon, 01 Feb 2010) | 7 lines


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.

........

r2228 | jwatson | 2010-02-01 14:49:40 -0700 (Mon, 01 Feb 2010) | 3 lines


Final fix to LP writer to (optionally) account for the models in which a variable originates - the PySP extensive form writer now passes all tests.

........

File size: 10.4 KB
Line 
1#
2# Test the canonical expressions
3#
4
5import unittest
6import os
7import sys
8from os.path import abspath, dirname
9sys.path.insert(0, dirname(dirname(abspath(__file__)))+os.sep+".."+os.sep+"..")
10currdir = dirname(abspath(__file__))+os.sep
11
12from coopr.pyomo import *
13from coopr.opt import *
14#from coopr.pyomo.base.var import _VarElement
15import pyutilib.th
16import pyutilib.services
17from coopr.pyomo.presolve.simple_presolver import SimplePresolver
18from pyutilib.component.core import PluginGlobals
19
20class frozendict(dict):
21    __slots__ = ('_hash',)
22    def __hash__(self):
23        rval = getattr(self, '_hash', None)
24        if rval is None:
25            rval = self._hash = hash(frozenset(self.iteritems()))
26        return rval
27
28
29class TestBase(pyutilib.th.TestCase):
30
31    def setUp(self):
32        self.model = Model()
33
34    def construct(self,filename):
35        self.instance = self.model.create(filename)
36
37
38class Test(TestBase):
39
40    def setUp(self):
41        #
42        # Create Model
43        #
44        TestBase.setUp(self)
45        #PluginGlobals.pprint()
46        self.plugin = SimplePresolver()
47        self.plugin.deactivate_action("compute_canonical_repn")
48
49    def tearDown(self):
50        if os.path.exists("unknown.lp"):
51           os.unlink("unknown.lp")
52        pyutilib.services.TempfileManager.clear_tempfiles()
53        self.plugin.activate_action("compute_canonical_repn")
54
55    def test_expr1(self):
56        self.model.A = RangeSet(1,4)
57        def p_init(i, model):
58            return 2*i
59        self.model.p = Param(self.model.A, initialize=p_init)
60        self.model.x = Var(self.model.A, bounds=(-1,1))
61        def obj_rule(model):
62            return summation(model.p, model.x)
63        self.model.obj = Objective(rule=obj_rule)
64        self.instance = self.model.create()
65        #self.instance.obj[None].expr.pprint()
66        mid=id(self.instance)
67        rep = generate_canonical_repn(self.instance.obj[None].expr)
68        del rep[-1]
69        self.failUnlessEqual(rep, {1: {frozendict({(mid,2): 1}): 6.0, frozendict({(mid,1): 1}): 4.0, frozendict({(mid,3): 1}): 8.0, frozendict({(mid,0): 1}): 2.0}})
70
71    def test_expr2(self):
72        self.model.A = RangeSet(1,4)
73        def p_init(i, model):
74            return 2*i
75        self.model.p = Param(self.model.A, initialize=p_init)
76        self.model.x = Var(self.model.A, bounds=(-1,1))
77        def obj_rule(model):
78            return summation(model.x, model.p)
79        self.model.obj = Objective(rule=obj_rule)
80        self.instance = self.model.create()
81        mid=id(self.instance)
82        #self.instance.obj[None].expr.pprint()
83        rep = generate_canonical_repn(self.instance.obj[None].expr)
84        del rep[-1]
85        self.failUnlessEqual(rep, {1: {frozendict({(mid,2): 1}): 6.0, frozendict({(mid,1): 1}): 4.0, frozendict({(mid,3): 1}): 8.0, frozendict({(mid,0): 1}): 2.0}})
86
87    def test_expr3(self):
88        self.model.A = RangeSet(1,4)
89        def p_init(i, model):
90            return 2*i
91        self.model.p = Param(self.model.A, initialize=p_init)
92        self.model.x = Var(self.model.A, bounds=(-1,1))
93        def obj_rule(model):
94            return 2*model.x[1] + 3*model.x[1] + 4*(model.x[1]+model.x[2])
95        self.model.obj = Objective(rule=obj_rule)
96        self.instance = self.model.create()
97        mid=id(self.instance)
98        #self.instance.obj[None].expr.pprint()
99        rep = generate_canonical_repn(self.instance.obj[None].expr)
100        del rep[-1]
101        self.failUnlessEqual(rep,
102                    {1: {frozendict({(mid,0): 1}):9.0, frozendict({(mid,1): 1}):4.0}})
103
104    def test_expr4(self):
105        self.model.A = RangeSet(1,4)
106        def p_init(i, model):
107            return 2*i
108        self.model.p = Param(self.model.A, initialize=p_init)
109        self.model.x = Var(self.model.A, bounds=(-1,1))
110        def obj_rule(model):
111            return 1.2 + 2*model.x[1] + 3*model.x[1]
112        self.model.obj = Objective(rule=obj_rule)
113        self.instance = self.model.create()
114        mid=id(self.instance)
115        #self.instance.obj[None].expr.pprint()
116        rep = generate_canonical_repn(self.instance.obj[None].expr)
117        del rep[-1]
118        self.failUnlessEqual(rep, {0:frozendict({None:1.2}), 1: {frozendict({(mid,0): 1}): 5.0}})
119
120    def test_expr5(self):
121        self.model.A = RangeSet(1,4)
122        def p_init(i, model):
123            return 2*i
124        self.model.p = Param(self.model.A, initialize=p_init)
125        self.model.x = Var(self.model.A, bounds=(-1,1))
126        def obj_rule(model):
127            return model.x[1]*(model.x[1]+model.x[2]) + model.x[2]*(model.x[1]+3.0*model.x[3]*model.x[3])
128        self.model.obj = Objective(rule=obj_rule)
129        self.instance = self.model.create()
130        mid=id(self.instance)
131        #self.instance.obj[None].expr.pprint()
132        rep = generate_canonical_repn(self.instance.obj[None].expr)
133        del rep[-1]
134        self.failUnlessEqual(rep,
135                {2: {frozendict({(mid,0):2}):1.0, frozendict({(mid,0):1, (mid,1):1}):2.0}, 3:{frozendict({(mid,1):1, (mid,2):2}):3.0}})
136
137    def test_expr6(self):
138        self.model.A = RangeSet(1,4)
139        def p_init(i, model):
140            return 2*i
141        self.model.p = Param(self.model.A, initialize=p_init)
142        self.model.x = Var(self.model.A, bounds=(-1,1))
143        def obj_rule(model):
144            return 2.0*(1.2 + 2*model.x[1] + 3*model.x[1]) + 3.0*(1.0+model.x[1])
145        self.model.obj = Objective(rule=obj_rule)
146        self.instance = self.model.create()
147        mid=id(self.instance)
148        #self.instance.obj[None].expr.pprint()
149        rep = generate_canonical_repn(self.instance.obj[None].expr)
150        del rep[-1]
151        self.failUnlessEqual(rep,
152            {0:frozendict({None:5.4}), 1: {frozendict({(mid,0): 1}): 13.0}})
153
154    def test_expr7(self):
155        self.model.A = RangeSet(1,4)
156        def p_init(i, model):
157            return 2*i
158        self.model.p = Param(self.model.A, initialize=p_init)
159        self.model.x = Var(self.model.A, bounds=(-1,1))
160        def obj_rule(model):
161            return summation(model.p, model.x)/2.0
162        self.model.obj = Objective(rule=obj_rule)
163        self.instance = self.model.create()
164        mid=id(self.instance)
165        #self.instance.obj[None].expr.pprint()
166        rep = generate_canonical_repn(self.instance.obj[None].expr)
167        del rep[-1]
168        self.failUnlessEqual(rep,
169            {1: {frozendict({(mid,2): 1}): 3.0, frozendict({(mid,1): 1}): 2.0, frozendict({(mid,3): 1}): 4.0, frozendict({(mid,0): 1}): 1.0}})
170
171    def test_expr8(self):
172        self.model.A = RangeSet(1,4)
173        def p_init(i, model):
174            return 2*i
175        self.model.p = Param(self.model.A, initialize=p_init)
176        self.model.x = Var(self.model.A, bounds=(-1,1))
177        self.model.y = Var(initialize=2.0)
178        def obj_rule(model):
179            return summation(model.p, model.x)/model.y
180        self.model.obj = Objective(rule=obj_rule)
181        self.instance = self.model.create()
182        self.instance.y.fixed = True
183        self.instance.y.reset()
184        mid=id(self.instance)
185        #self.instance.obj[None].expr.pprint()
186        rep = generate_canonical_repn(self.instance.obj[None].expr)
187        del rep[-1]
188        self.failUnlessEqual(rep,
189            {1: {frozendict({(mid,2): 1}): 3.0, frozendict({(mid,1): 1}): 2.0, frozendict({(mid,3): 1}): 4.0, frozendict({(mid,0): 1}): 1.0}})
190
191    def test_expr9(self):
192        self.model.A = RangeSet(1,4)
193        def p_init(i, model):
194            return 2*i
195        self.model.p = Param(self.model.A, initialize=p_init)
196        self.model.x = Var(self.model.A, bounds=(-1,1))
197        self.model.y = Var(initialize=1.0)
198        def obj_rule(model):
199            return summation(model.p, model.x)/(1+model.y)
200        self.model.obj = Objective(rule=obj_rule)
201        self.instance = self.model.create()
202        self.instance.y.fixed = True
203        self.instance.y.reset()
204        mid=id(self.instance)
205        #self.instance.obj[None].expr.pprint()
206        rep = generate_canonical_repn(self.instance.obj[None].expr)
207        del rep[-1]
208        self.failUnlessEqual(rep,
209            {1: {frozendict({(mid,2): 1}): 3.0, frozendict({(mid,1): 1}): 2.0, frozendict({(mid,3): 1}): 4.0, frozendict({(mid,0): 1}): 1.0}})
210
211    def test_expr10(self):
212        self.model.A = RangeSet(1,4)
213        def p_init(i, model):
214            return 2*i
215        self.model.p = Param(self.model.A, initialize=p_init)
216        self.model.x = Var(self.model.A, bounds=(-1,1))
217        self.model.y = Var(initialize=1.0)
218        def obj_rule(model):
219            return summation(model.p, model.x)/(1+model.y)
220        self.model.obj = Objective(rule=obj_rule)
221        self.instance = self.model.create()
222        #self.instance.obj[None].expr.pprint()
223        rep = generate_canonical_repn(self.instance.obj[None].expr)
224        self.failUnless(len(rep) == 1 and None in rep)
225
226    def test_expr11(self):
227        self.model.A = RangeSet(1,4)
228        def p_init(i, model):
229            return 2*i
230        self.model.p = Param(self.model.A, initialize=p_init)
231        self.model.x = Var(self.model.A, bounds=(-1,1))
232        self.model.y = Var(initialize=1.0)
233        def obj_rule(model):
234            return (1.0+model.x[1]+1/model.x[2]) + (2.0+model.x[2]+1/model.x[3])
235        self.model.obj = Objective(rule=obj_rule)
236        self.instance = self.model.create()
237        #self.instance.obj[None].expr.pprint()
238        rep = generate_canonical_repn(self.instance.obj[None].expr)
239        rep[None].pprint()
240        del rep[None]
241        del rep[-1]
242        mid=id(self.instance)
243        self.failUnlessEqual(rep, {0: {None: 3.0}, 1: {frozendict({(mid,1): 1}): 1.0, frozendict({(mid,0): 1}): 1.0}})
244
245    def test_expr12(self):
246        self.model.A = RangeSet(1,4)
247        def p_init(i, model):
248            return 2*i
249        self.model.p = Param(self.model.A, initialize=p_init)
250        self.model.x = Var(self.model.A, bounds=(-1,1))
251        self.model.y = Var(initialize=1.0)
252        def obj_rule(model):
253            return (1.0+model.x[1]+1/model.x[2]) * (2.0+model.x[2]+1/model.x[3])
254        self.model.obj = Objective(rule=obj_rule)
255        self.instance = self.model.create()
256        mid=id(self.instance)
257        #self.instance.obj[None].expr.pprint()
258        rep = generate_canonical_repn(self.instance.obj[None].expr)
259        rep[None].pprint()
260        del rep[None]
261        del rep[-1]
262        self.failUnlessEqual(rep, {0: {None: 2.0}, 1: {frozendict({(mid,1): 1}): 1.0, frozendict({(mid,0): 1}): 2.0}, 2: {frozendict({(mid,0): 1, (mid,1): 1}): 1.0}})
263
264if __name__ == "__main__":
265   unittest.main()
266
Note: See TracBrowser for help on using the repository browser.