source: trunk/test/pyomo/unit/test_util.py @ 1768

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

Rework of Coopr to use the new PyUtilib? package decomposition.

NOTE: to use Coopr with this update, we need to work with a new version of coopr_install.

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