source: coopr.opt/trunk/coopr/opt/tests/base/test_soln.py @ 1918

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

Rework of the SolverResults? object, to provide for a more explicit
definition of solver results, and to support loading of
solver results from YAML-formated data.

File size: 6.5 KB
Line 
1#
2# Unit Tests for coopr.opt.base.solution
3#
4#
5
6import os
7import sys
8from os.path import abspath, dirname
9sys.path.insert(0, dirname(dirname(abspath(__file__)))+"/../..")
10cooprdir = dirname(abspath(__file__))+os.sep+".."+os.sep+".."+os.sep
11currdir = dirname(abspath(__file__))+os.sep
12
13import unittest
14from nose.tools import nottest
15import coopr.opt
16import coopr
17import pyutilib.th
18import pyutilib.misc
19import pyutilib.services
20import xml
21
22class Test(pyutilib.th.TestCase):
23
24    def setUp(self):
25        pyutilib.services.TempfileManager.tempdir = currdir
26        self.results = coopr.opt.SolverResults()
27        self.soln = self.results.solution.add()
28        self.soln.variable[0]=0.0
29        self.soln.variable[1]=0.0
30        self.soln.variable[3]=0.0
31        pyutilib.services.TempfileManager.clear_tempfiles()
32
33    def tearDown(self):
34        pass
35
36    def test_write_solution1(self):
37        """ Write a SolverResults Object with solutions """
38        self.results.write(filename=currdir+"write_solution1.txt")
39        if not os.path.exists(currdir+"write_solution1.txt"):
40           self.fail("test_write_solution - failed to write write_solution1.txt")
41        self.failUnlessFileEqualsBaseline(currdir+"write_solution1.txt", currdir+"test1_soln.txt")
42
43    def test_write_solution2(self):
44        """ Write a SolverResults Object without solutions """
45        self.results.write(num=None,filename=currdir+"write_solution2.txt")
46        if not os.path.exists(currdir+"write_solution2.txt"):
47           self.fail("test_write_solution - failed to write write_solution2.txt")
48        self.failUnlessFileEqualsBaseline(currdir+"write_solution2.txt", currdir+"test2_soln.txt")
49
50    def test_read_solution1(self):
51        """ Read a SolverResults Object"""
52        self.results = coopr.opt.SolverResults()
53        self.results.read(filename=currdir+"test4_sol.txt")
54        self.results.write(filename=currdir+"read_solution1.out")
55        if not os.path.exists(currdir+"read_solution1.out"):
56           self.fail("test_read_solution1 - failed to write read_solution1.out")
57        self.failUnlessFileEqualsBaseline(currdir+"read_solution1.out", currdir+"test4_sol.txt")
58
59    #
60    # deleting is not supported right now
61    #
62    def Xtest_delete_solution(self):
63        """ Delete a solution from a SolverResults object """
64        self.results.solution.delete(0)
65        self.results.write(filename=currdir+"delete_solution.txt")
66        if not os.path.exists(currdir+"delete_solution.txt"):
67           self.fail("test_write_solution - failed to write delete_solution.txt")
68        self.failUnlessFileEqualsBaseline(currdir+"delete_solution.txt", currdir+"test4_soln.txt")
69
70    def test_get_solution(self):
71        """ Get a solution from a SolverResults object """
72        tmp = self.results.solution[0]
73        self.failUnlessEqual(tmp,self.soln)
74
75    def test_get_solution_attr_error(self):
76        """ Create an error with a solution suffix """
77        try:
78           tmp = self.soln.bad
79           self.fail("Expected attribute error failure for 'bad'")
80        except AttributeError:
81           pass
82
83    #
84    # This is currently allowed, although soln.variable = True is equivalent to
85    #   soln.variable.value = True
86    #
87    def Xtest_set_solution_attr_error(self):
88        """ Create an error with a solution suffix """
89        try:
90           self.soln.variable = True
91           self.fail("Expected attribute error failure for 'variable'")
92        except AttributeError:
93           pass
94
95    def test_soln_pprint1(self):
96        """ Write a solution with only zero values, using the results 'write()' method """
97        self.soln.variable[0]=0.0
98        self.soln.variable[1]=0.0
99        self.soln.variable[3]=0.0
100        self.results.write(filename=currdir+"soln_pprint.txt")
101        if not os.path.exists(currdir+"soln_pprint.txt"):
102           self.fail("test_write_solution - failed to write soln_pprint.txt")
103        self.failUnlessFileEqualsBaseline(currdir+"soln_pprint.txt", currdir+"test3_soln.txt")
104
105    def test_soln_pprint2(self):
106        """ Write a solution with only zero values, using the Solution.pprint() method """
107        self.soln.variable[0]=0.0
108        self.soln.variable[1]=0.0
109        self.soln.variable[3]=0.0
110        pyutilib.misc.setup_redirect(currdir+"soln_pprint2.out")
111        print self.soln
112        print self.soln.variable
113        pyutilib.misc.reset_redirect()
114        self.failUnlessFileEqualsBaseline(currdir+"soln_pprint2.out", currdir+"soln_pprint2.txt")
115
116    def test_soln_suffix_iter(self):
117        """ Test a suffix iterator """
118        self.soln.variable[0]=0.0
119        self.soln.variable[1]=0.1
120        self.soln.variable[3]=0.3
121        i=0
122        for key in self.soln.variable:
123          i=i+1
124          self.failUnlessEqual(self.soln.variable[key].value, self.soln.variable[key].id/10.0)
125        self.failUnlessEqual(i,len(self.soln.variable))
126
127    def test_soln_suffix_getiter(self):
128        self.soln.variable["x0"]=0.0
129        self.soln.variable[1]=0.1
130        self.soln.variable["x3"]=0.3
131        self.failUnlessEqual(self.soln.variable["x3"].value,0.3)
132        #print "HERE",self.soln.variable._names
133        self.failUnlessEqual(self.soln.variable[1].value,0.1)
134
135    def test_soln_suffix_setattr(self):
136        self.soln.variable["x(0)"] = 0.0
137        self.soln.variable["x3"]=0.3
138        self.soln.variable["x3"].slack=0.4
139        self.soln.variable["y[1,ab]"]=0.5
140        #self.soln.variable.z[0,'a']=2.0
141        #print "HERE",self.soln.variable._names
142        #print "HERE",self.soln.variable.__dict__.keys()
143        #print "HERE",self.soln.variable.keys()
144        #print "HERE",self.soln.variable.__getitem__('y[1,ab]')
145        #print "HERE",repr(self.soln.variable.x)
146        #print "HERE",repr(self.soln.variable.x[0])
147        #print "HERE",repr(self.soln.variable.y)
148        #print "HERE",repr(self.soln.variable.y[1,'ab'])
149        #print "HERE",repr(self.soln.variable.z)
150        #print "HERE",repr(self.soln.variable.z[0,'a'])
151        #print "HERE",repr(self.soln.variable.foo('x(0)'))
152        #self.soln.pprint()
153        #print "HERE", self.soln.variable._index.keys()
154        self.failUnlessEqual(self.soln.variable.x.keys(),[0])
155        self.failUnlessEqual(self.soln.variable.x[0].value,0.0)
156        self.failUnlessEqual(self.soln.variable.x3.value,0.3)
157        self.failUnlessEqual(self.soln.variable.x3.slack,0.4)
158        self.failUnlessEqual(self.soln.variable.y.keys(),[(1,'ab')])
159        self.failUnlessEqual(self.soln.variable.y[1,'ab'].value,0.5)
160
161
162if __name__ == "__main__":
163    import pyutilib.misc
164    #sys.settrace(pyutilib.misc.traceit)
165    unittest.main()
Note: See TracBrowser for help on using the repository browser.