source: trunk/test/core/base/test_soln.py @ 1768

Last change on this file since 1768 was 1768, checked in by wehart, 10 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: 5.1 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 OptSolnDebug(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.create()
28
29    def tmp(self):
30        self.soln.variable[0]=0.0
31        self.soln.variable[1]=0.0
32        self.soln.variable[3]=0.0
33        pyutilib.services.TempfileManager.clear_tempfiles()
34
35    def tearDown(self):
36        pass
37
38    def test_write_solution1(self):
39        """ Write a SolverResults Object with solutions """
40        self.results.write(filename=currdir+"write_solution1.txt")
41        if not os.path.exists(currdir+"write_solution1.txt"):
42           self.fail("test_write_solution - failed to write write_solution1.txt")
43        self.failUnlessFileEqualsBaseline(currdir+"write_solution1.txt", currdir+"test1_soln.txt")
44
45    def test_write_solution2(self):
46        """ Write a SolverResults Object without solutions """
47        self.results.write(num=None,filename=currdir+"write_solution2.txt")
48        if not os.path.exists(currdir+"write_solution2.txt"):
49           self.fail("test_write_solution - failed to write write_solution2.txt")
50        self.failUnlessFileEqualsBaseline(currdir+"write_solution2.txt", currdir+"test2_soln.txt")
51
52    def test_delete_solution(self):
53        """ Delete a solution from a SolverResults object """
54        self.results.solution.delete(0)
55        self.results.write(filename=currdir+"delete_solution.txt")
56        if not os.path.exists(currdir+"delete_solution.txt"):
57           self.fail("test_write_solution - failed to write delete_solution.txt")
58        self.failUnlessFileEqualsBaseline(currdir+"delete_solution.txt", currdir+"test4_soln.txt")
59
60    def test_get_solution(self):
61        """ Get a solution from a SolverResults object """
62        tmp = self.results.solution[0]
63        self.failUnlessEqual(tmp,self.soln)
64
65    def test_get_solution_attr_error(self):
66        """ Create an error with a solution suffix """
67        try:
68           tmp = self.soln.bad
69           self.fail("Expected attribute error failure for 'bad'")
70        except AttributeError:
71           pass
72
73    def test_set_solution_attr_error(self):
74        """ Create an error with a solution suffix """
75        try:
76           self.soln.variable = True
77           self.fail("Expected attribute error failure for 'var'")
78        except AttributeError:
79           pass
80
81    def test_soln_pprint1(self):
82        """ Write a solution with only zero values, using the results 'write()' method """
83        self.soln.variable.add(0,0.0)
84        self.soln.variable.add(1,0.0)
85        self.soln.variable.add(3,0.0)
86        self.results.write(filename=currdir+"soln_pprint.txt")
87        if not os.path.exists(currdir+"soln_pprint.txt"):
88           self.fail("test_write_solution - failed to write soln_pprint.txt")
89        self.failUnlessFileEqualsBaseline(currdir+"soln_pprint.txt", currdir+"test3_soln.txt")
90
91    def test_soln_pprint2(self):
92        """ Write a solution with only zero values, using the Solution.pprint() method """
93        self.soln.variable.add(0,0.0)
94        self.soln.variable.add(1,0.0)
95        self.soln.variable.add(3,0.0)
96        pyutilib.misc.setup_redirect(currdir+"soln_pprint2.out")
97        self.soln.pprint()
98        self.soln._variable.pprint()
99        pyutilib.misc.reset_redirect()
100        self.failUnlessFileEqualsBaseline(currdir+"soln_pprint2.out", currdir+"soln_pprint2.txt")
101
102    def test_soln_suffix_iter(self):
103        """ Test a suffix iterator """
104        self.soln.variable.add(0,0.0)
105        self.soln.variable.add(1,0.1)
106        self.soln.variable.add(3,0.3)
107        i=0
108        for val in self.soln.variable:
109          i=i+1
110          self.failUnlessEqual(val[1], val[0]/10.0)
111        self.failUnlessEqual(i,len(self.soln.variable))
112
113    def test_soln_suffix_getiter(self):
114        self.soln.variable.add("x0",0.0)
115        self.soln.variable.add(1,0.1)
116        self.soln.variable.add("x3",0.3)
117        #print "HERE", self.soln.variable._index.keys()
118        self.failUnlessEqual(self.soln.variable["x3"],0.3)
119        self.failUnlessEqual(self.soln.variable[1],0.1)
120
121    def test_soln_suffix_setattr(self):
122        self.soln.variable.add("x(0)",0.0)
123        self.soln.variable.add("x3",0.3)
124        self.soln.variable.add("y[1,ab]",0.5)
125        #self.soln.pprint()
126        #print "HERE", self.soln.variable._index.keys()
127        self.failUnlessEqual(self.soln.variable.x.keys(),[0])
128        self.failUnlessEqual(self.soln.variable.x[0],0.0)
129        self.failUnlessEqual(self.soln.variable.x3,0.3)
130        self.failUnlessEqual(self.soln.variable.y.keys(),[(1,'ab')])
131        self.failUnlessEqual(self.soln.variable.y[1,'ab'],0.5)
132
133
134
135if __name__ == "__main__":
136   unittest.main()
Note: See TracBrowser for help on using the repository browser.