source: coopr.pyomo/stable/2.3/coopr/pyomo/tests/unit/test_util.py @ 2433

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

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

........

r2399 | wehart | 2010-02-24 11:36:56 -0700 (Wed, 24 Feb 2010) | 2 lines


Adding a work-around when pstats cannot be imported.

........

r2400 | wehart | 2010-02-24 14:51:08 -0700 (Wed, 24 Feb 2010) | 2 lines


Misc documentation updates.

........

r2408 | wehart | 2010-02-28 05:41:04 -0700 (Sun, 28 Feb 2010) | 3 lines


Allow an earlier version of Python. I'm still not sure this is a good idea,
but it's necessary for Jython.

........

r2409 | wehart | 2010-02-28 05:42:33 -0700 (Sun, 28 Feb 2010) | 7 lines


Rework of profiling imports.


Refinement of Pyomo command-line parsing, which is more
specific now.


Adding automatic import of pyomo to package.

........

r2415 | jwatson | 2010-03-08 20:53:13 -0700 (Mon, 08 Mar 2010) | 3 lines


Significantly improved performance of PyomoModel? _clear_attribute method, mainly by eliminate unnecessary calls to it through _setattr_exec_.

........

r2416 | jwatson | 2010-03-09 16:45:22 -0700 (Tue, 09 Mar 2010) | 5 lines


Modified the AMPL dat file parser to instantiate the lexer and yaccer objects only once, for the lifetime of the module.


They were being inadvertently created at each invocation, which pyomo users wouldn't notice. But PySP users, who can be creating 1K or more instances, do!

........

r2422 | wehart | 2010-03-11 16:01:09 -0700 (Thu, 11 Mar 2010) | 3 lines


Rework of unit tests to (a) import pyutilib.th as 'unittest' and
(b) employ test skipping.

........

r2430 | wehart | 2010-03-11 23:38:22 -0700 (Thu, 11 Mar 2010) | 2 lines


Reworking class decorators.

........

File size: 4.7 KB
Line 
1#
2# Unit Tests for Utility Functions
3#
4
5import os
6import sys
7from os.path import abspath, dirname
8sys.path.insert(0, dirname(dirname(abspath(__file__)))+"/../..")
9currdir = dirname(abspath(__file__))+os.sep
10
11import pyutilib.th as unittest
12from coopr.pyomo import *
13from nose.tools import nottest
14import pickle
15
16
17class TestNumericValue(unittest.TestCase):
18
19    def test_expr1(self):
20        model = Model()
21        model.A = Set(initialize=[1,2,3])
22        model.B = Param(model.A,initialize={1:100,2:200,3:300})
23        model.x = Var(model.A)
24        model.y = Var(model.A)
25        instance=model.create()
26        expr = dot_product(instance.x,instance.B,instance.y)
27        expr.simplify(instance)
28        OUTPUT=open(currdir+"test_expr1.out","w")
29        expr.pprint(ostream=OUTPUT)
30        OUTPUT.close()
31        self.failUnlessFileEqualsBaseline(currdir+"test_expr1.out",currdir+"test_expr1.txt")
32
33    def test_expr2(self):
34        model = Model()
35        model.A = Set(initialize=[1,2,3])
36        model.B = Param(model.A,initialize={1:100,2:200,3:300})
37        model.x = Var(model.A)
38        model.y = Var(model.A)
39        instance=model.create()
40        expr = dot_product(instance.x,instance.B,instance.y, index=[1,3])
41        expr.simplify(instance)
42        OUTPUT=open(currdir+"test_expr2.out","w")
43        expr.pprint(ostream=OUTPUT)
44        OUTPUT.close()
45        self.failUnlessFileEqualsBaseline(currdir+"test_expr2.out",currdir+"test_expr2.txt")
46
47    def test_expr3(self):
48        model = Model()
49        model.A = Set(initialize=[1,2,3])
50        model.B = Param(model.A,initialize={1:100,2:200,3:300})
51        model.x = Var(model.A)
52        model.y = Var(model.A)
53        instance=model.create()
54        expr = dot_product(instance.x,instance.B,denom=instance.y, index=[1,3])
55        OUTPUT=open(currdir+"test_expr3.out","w")
56        expr.pprint(ostream=OUTPUT)
57        expr.simplify(instance)
58        expr.pprint(ostream=OUTPUT)
59        OUTPUT.close()
60        self.failUnlessFileEqualsBaseline(currdir+"test_expr3.out",currdir+"test_expr3.txt")
61
62    def test_expr4(self):
63        model = Model()
64        model.A = Set(initialize=[1,2,3])
65        model.B = Param(model.A,initialize={1:100,2:200,3:300})
66        model.x = Var(model.A)
67        model.y = Var(model.A)
68        instance=model.create()
69        expr = dot_product(denom=[instance.y,instance.x])
70        expr.simplify(instance)
71        OUTPUT=open(currdir+"test_expr4.out","w")
72        expr.pprint(ostream=OUTPUT)
73        OUTPUT.close()
74        self.failUnlessFileEqualsBaseline(currdir+"test_expr4.out",currdir+"test_expr4.txt")
75
76class TestPickle(pyutilib.th.TestCase):
77
78    def test_p1(self):
79        model = Model()
80        model.A = Set(initialize=[1,2,3])
81        model.B = Param(model.A,initialize={1:100,2:200,3:300})
82        model.x = Var(model.A)
83        model.y = Var(model.A)
84        str = pickle.dumps(model)
85        tmodel = pickle.loads(str)
86        instance=tmodel.create()
87        expr = dot_product(instance.x,instance.B,instance.y)
88        expr.simplify(instance)
89        OUTPUT=open(currdir+"test_expr1.out","w")
90        expr.pprint(ostream=OUTPUT)
91        OUTPUT.close()
92        self.failUnlessFileEqualsBaseline(currdir+"test_expr1.out",currdir+"test_expr1.txt")
93
94    def test_p2(self):
95        model = Model()
96        model.A = Set(initialize=[1,2,3])
97        model.B = Param(model.A,initialize={1:100,2:200,3:300})
98        model.x = Var(model.A)
99        model.y = Var(model.A)
100        tmp=model.create()
101        str = pickle.dumps(tmp)
102        instance = pickle.loads(str)
103        expr = dot_product(instance.x,instance.B,instance.y)
104        expr.simplify(instance)
105        OUTPUT=open(currdir+"test_expr1.out","w")
106        expr.pprint(ostream=OUTPUT)
107        OUTPUT.close()
108        self.failUnlessFileEqualsBaseline(currdir+"test_expr1.out",currdir+"test_expr1.txt")
109
110    def test_p3(self):
111        def rule1(model):
112            return (1,model.x+model.y[1],2)
113        def rule2(i,model):
114            return (1,model.x+model.y[1]+i,2)
115
116        model = Model()
117        model.a = Set(initialize=[1,2,3])
118        model.A = Param(initialize=1)
119        model.B = Param(model.a)
120        model.x = Var(initialize=1,within=Reals)
121        model.y = Var(model.a, initialize=1,within=Reals)
122        model.obj = Objective(rule=lambda model: model.x+model.y[1])
123        model.obj2 = Objective(model.a,rule=lambda i,model: i+model.x+model.y[1])
124        model.con = Constraint(rule=rule1)
125        model.con2 = Constraint(model.a, rule=rule2)
126        instance = model.create()
127        try:
128            str = pickle.dumps(instance)
129            self.fail("Expected pickling error due to the use of lambda expressions")
130        except pickle.PicklingError:
131            pass
132
133
134
135if __name__ == "__main__":
136   unittest.main()
Note: See TracBrowser for help on using the repository browser.