source: coopr.pyomo/stable/2.3/coopr/pyomo/tests/unit/test_model.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: 9.1 KB
Line 
1#
2# Unit Tests for Elements of a Model
3#
4# Test             Class to test the Model class
5#
6
7import os
8import sys
9from os.path import abspath, dirname
10sys.path.insert(0, dirname(dirname(abspath(__file__)))+os.sep+".."+os.sep+"..")
11currdir = dirname(abspath(__file__))+os.sep
12
13from coopr.pyomo.base import IntegerSet
14from coopr.pyomo import *
15from coopr.opt import *
16from coopr.pyomo.base.var import _VarElement
17import pyutilib.th as unittest
18import pyutilib.services
19
20class PyomoModel(unittest.TestCase):
21
22    def setUp(self):
23        self.model = Model()
24
25    def construct(self,filename):
26        self.instance = self.model.create(filename)
27
28
29class Test(PyomoModel):
30
31    def setUp(self):
32        #
33        # Create Model
34        #
35        PyomoModel.setUp(self)
36
37    def tearDown(self):
38        if os.path.exists("unknown.lp"):
39           os.unlink("unknown.lp")
40        pyutilib.services.TempfileManager.clear_tempfiles()
41
42    def test_clear_attribute(self):
43        """ Coverage of the _clear_attribute method """
44        self.model.A = Set()
45        self.failUnlessEqual(self.model.A.name,"A")
46        self.model.A = Var()
47        self.failUnlessEqual(self.model.A.name,"A")
48        self.model.A = Param()
49        self.failUnlessEqual(self.model.A.name,"A")
50        self.model.A = Objective()
51        self.failUnlessEqual(self.model.A.name,"A")
52        self.model.A = Constraint()
53        self.failUnlessEqual(self.model.A.name,"A")
54        self.model.A = Set()
55        self.failUnlessEqual(self.model.A.name,"A")
56
57    def test_set_attr(self):
58        self.model.x = Param()
59        self.model.x = None
60
61    def test_write(self):
62        self.model.A = RangeSet(1,4)
63        self.model.x = Var(self.model.A, bounds=(-1,1))
64        def obj_rule(model):
65            return summation(model.x)
66        self.model.obj = Objective(rule=obj_rule)
67        self.instance = self.model.create()
68        self.instance.write()
69
70    def test_write2(self):
71        self.model.A = RangeSet(1,4)
72        self.model.x = Var(self.model.A, bounds=(-1,1))
73        def obj_rule(model):
74            return summation(model.x)
75        self.model.obj = Objective(rule=obj_rule)
76        def c_rule(model):
77            return (1, model.x[1]+model.x[2], 2)
78        self.model.c = Constraint(rule=c_rule)
79        self.instance = self.model.create()
80        self.instance.write()
81
82    def test_write3(self):
83        """Test that the summation works correctly, even though param 'w' has a default value"""
84        self.model.J = RangeSet(1,4)
85        self.model.w=Param(self.model.J, default=4)
86        self.model.x=Var(self.model.J)
87        def obj_rule(instance):
88            return summation(instance.x, instance.w)
89        self.model.obj = Objective(rule=obj_rule)
90        self.instance = self.model.create()
91        self.failUnlessEqual(self.instance.obj[None].expr._nargs, 4)
92
93    def test_write_error(self):
94        self.instance = self.model.create()
95        try:
96            self.instance.write(format=ProblemFormat.ospl)
97            self.fail("Expected error because ospl writer is not available")
98        except ValueError:
99            pass
100
101    def test_solve1(self):
102        if not pyutilib.services.registered_executable("glpsol"):
103            return
104        self.model.A = RangeSet(1,4)
105        self.model.x = Var(self.model.A, bounds=(-1,1))
106        def obj_rule(model):
107            return summation(model.x)
108        self.model.obj = Objective(rule=obj_rule)
109        def c_rule(model):
110            expr = 0
111            for i in model.A:
112                expr += i*model.x[i]
113            return expr == 0
114        self.model.c = Constraint(rule=c_rule)
115        self.instance = self.model.create()
116        #self.instance.pprint()
117        opt = SolverFactory('glpk')
118        solutions = opt.solve(self.instance, keepFiles=True)
119        self.instance.load(solutions)
120        self.instance.display(currdir+"solve1.out")
121        self.failUnlessFileEqualsBaseline(currdir+"solve1.out",currdir+"solve1.txt")
122        #
123        def d_rule(model):
124            return model.x[1] > 0
125        self.model.d = Constraint(rule=d_rule)
126        self.model.d.deactivate()
127        self.instance = self.model.create()
128        solutions = opt.solve(self.instance, keepFiles=True)
129        self.instance.load(solutions)
130        self.instance.display(currdir+"solve1.out")
131        self.failUnlessFileEqualsBaseline(currdir+"solve1.out",currdir+"solve1.txt")
132        #
133        self.model.d.activate()
134        self.instance = self.model.create()
135        solutions = opt.solve(self.instance, keepFiles=True)
136        self.instance.load(solutions)
137        self.instance.display(currdir+"solve1.out")
138        self.failUnlessFileEqualsBaseline(currdir+"solve1.out",currdir+"solve1a.txt")
139        #
140        self.model.d.deactivate()
141        def e_rule(i, model):
142            return model.x[i] > 0
143        self.model.e = Constraint(self.model.A, rule=e_rule)
144        self.instance = self.model.create()
145        for i in self.instance.A:
146            self.instance.e[i].deactivate()
147        solutions = opt.solve(self.instance, keepFiles=True)
148        self.instance.load(solutions)
149        self.instance.display(currdir+"solve1.out")
150        self.failUnlessFileEqualsBaseline(currdir+"solve1.out",currdir+"solve1b.txt")
151
152    def test_load1(self):
153        """Testing loading of vector solutions"""
154        self.model.A = RangeSet(1,4)
155        self.model.x = Var(self.model.A, bounds=(-1,1))
156        def obj_rule(model):
157            return summation(model.x)
158        self.model.obj = Objective(rule=obj_rule)
159        def c_rule(model):
160            expr = 0
161            for i in model.A:
162                expr += i*model.x[i]
163            return expr == 0
164        self.model.c = Constraint(rule=c_rule)
165        self.instance = self.model.create()
166        ans = [0.75]*4
167        self.instance.load(ans)
168        self.instance.display(currdir+"solve1.out")
169        self.failUnlessFileEqualsBaseline(currdir+"solve1.out",currdir+"solve1c.txt")
170
171    def Xtest_solve2(self):
172        """
173        WEH - this is disabled because glpk appears to work fine
174        on this example.  I'm not quite sure what has changed that has
175        impacted this test...
176        """
177        if not pyutilib.services.registered_executable("glpsol"):
178            return
179        self.model.A = RangeSet(1,4)
180        self.model.x = Var(self.model.A, bounds=(-1,1))
181        def obj_rule(model):
182          expr = 0
183          for i in model.A:
184            expr += model.x[i]
185          return expr
186        self.model.obj = Objective(rule=obj_rule)
187        self.instance = self.model.create()
188        #self.instance.pprint()
189        opt = solvers.GLPK(keepFiles=True)
190        solutions = opt.solve(self.instance)
191        solutions.write()
192        sys.exit(1)
193        try:
194            self.instance.load(solutions)
195            self.fail("Cannot load a solution with a bad solver status")
196        except ValueError:
197            pass
198
199    def test_solve3(self):
200        self.model.A = RangeSet(1,4)
201        self.model.x = Var(self.model.A, bounds=(-1,1))
202        def obj_rule(model):
203          expr = 0
204          for i in model.A:
205            expr += model.x[i]
206          return expr
207        self.model.obj = Objective(rule=obj_rule)
208        self.instance = self.model.create()
209        self.instance.display(currdir+"solve3.out")
210        self.failUnlessFileEqualsBaseline(currdir+"solve3.out",currdir+"solve3.txt")
211
212    def test_solve4(self):
213        if not pyutilib.services.registered_executable("glpsol"):
214            return
215        self.model.A = RangeSet(1,4)
216        self.model.x = Var(self.model.A, bounds=(-1,1))
217        def obj_rule(model):
218            return summation(model.x)
219        self.model.obj = Objective(rule=obj_rule)
220        def c_rule(model):
221          expr = 0
222          for i in model.A:
223            expr += i*model.x[i]
224          return expr == 0
225        self.model.c = Constraint(rule=c_rule)
226        self.instance = self.model.create()
227        #self.instance.pprint()
228        opt = SolverFactory('glpk', keepFiles=True)
229        solutions = opt.solve(self.instance)
230        self.instance.load(solutions.solution(0))
231        self.instance.display(currdir+"solve1.out")
232        self.failUnlessFileEqualsBaseline(currdir+"solve1.out",currdir+"solve1.txt")
233
234    def Xtest_solve5(self):
235        """ A draft test for the option to select an objective """
236        if not pyutilib.services.registered_executable("glpsol"):
237            return
238        self.model.A = RangeSet(1,4)
239        self.model.x = Var(self.model.A, bounds=(-1,1))
240        def obj1_rule(model):
241          expr = 0
242          for i in model.A:
243            expr += model.x[i]
244          return expr
245        self.model.obj1 = Objective(rule=obj1_rule)
246        def obj2_rule(model):
247          expr = 0
248          tmp=-1
249          for i in model.A:
250            expr += tmp*i*model.x[i]
251            tmp *= -1
252          return expr
253        self.model.obj2 = Objective(rule=obj2_rule)
254        self.instance = self.model.create()
255        opt = SolverFactory('glpk', keepfiles=True)
256        solutions = opt.solve(self.instance, objective='obj2')
257        self.instance.load(solutions.solution())
258        self.instance.display(currdir+"solve5.out")
259        self.failUnlessFileEqualsBaseline(currdir+"solve5.out",currdir+"solve5a.txt")
260
261if __name__ == "__main__":
262   unittest.main()
263
Note: See TracBrowser for help on using the repository browser.