source: trunk/test/plugins/mip/test_cbc.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: 6.2 KB
Line 
1#
2# Unit Tests for coopr.plugins.mip.CBC
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 pyutilib.th
14import pyutilib.common
15import pyutilib.services
16import unittest
17from nose.tools import nottest
18import coopr.plugins.mip
19import coopr.opt
20import coopr
21import xml
22from coopr.opt import ResultsFormat, ProblemFormat
23
24try:
25    cbc = coopr.plugins.mip.CBC(keepFiles=True)
26    cbc_available= (not cbc.executable() is None)
27except pyutilib.common.ApplicationError:
28    cbc_available=False
29    pass
30
31
32class mock_all(pyutilib.th.TestCase):
33
34    def setUp(self):
35        self.do_setup(False)
36
37    def do_setup(self,flag):
38        global tmpdir
39        tmpdir = os.getcwd()
40        os.chdir(currdir)
41        pyutilib.services.TempfileManager.tempdir = currdir
42        if flag:
43            self.cbc = coopr.plugins.mip.CBC(keepFiles=True)
44        else:
45            self.cbc = coopr.plugins.mip.MockCBC(keepFiles=True)
46
47    def tearDown(self):
48        global tmpdir
49        pyutilib.services.TempfileManager.clear_tempfiles()
50        os.chdir(tmpdir)
51
52    def test_path(self):
53        """ Verify that the CBC path is what is expected """
54        if type(self.cbc) is 'CBC':
55           self.failUnlessEqual(self.cbc.executable.split(os.sep)[-1],"CBC"+coopr.    util.executable_extension)
56
57    def test_solve1(self):
58        """ Test CBC - test1.mps """
59        results = self.cbc.solve(currdir+"test1.mps",logfile=currdir+"test_solve1.log", solnfile=currdir+"test1.soln")
60        results.write(currdir+"test_solve1.txt",times=False)
61        self.failUnlessFileEqualsBaseline(currdir+"test_solve1.txt", currdir+"test1_cbc.txt")
62        os.remove(currdir+"test_solve1.log")
63        os.remove(currdir+"test1.soln")
64
65    def test_solve2a(self):
66        """ Test CBC - test1.mps """
67        results = self.cbc.solve(currdir+"test1.mps", rformat=ResultsFormat.log, logfile=currdir+"test_solve2a.log", solnfile=currdir+"test1.soln")
68        results.write(currdir+"test_solve2a.txt",times=False)
69        self.failUnlessFileEqualsBaseline(currdir+"test_solve2a.txt", currdir+"test1_cbc.txt")
70        os.remove(currdir+"test_solve2a.log")
71        os.remove(currdir+"test1.soln")
72
73    def test_solve2b(self):
74        """ Test CBC - test1.mps """
75        results = self.cbc.solve(currdir+"test1.mps", pformat=ProblemFormat.mps, rformat=ResultsFormat.log, logfile=currdir+"test_solve2b.log", solnfile=currdir+"test1.soln")
76        results.write(currdir+"test_solve2b.txt",times=False)
77        self.failUnlessFileEqualsBaseline(currdir+"test_solve2b.txt", currdir+"test1_cbc.txt")
78        os.remove(currdir+"test_solve2b.log")
79        os.remove(currdir+"test1.soln")
80
81    def test_solve3(self):
82        """ Test CBC - test2.lp """
83        results = self.cbc.solve(currdir+"test2.lp", logfile=currdir+"test_solve3.log", solnfile=currdir+"test2.soln")
84        results.write(currdir+"test_solve3.txt",times=False)
85        self.failUnlessFileEqualsBaseline(currdir+"test_solve3.txt", currdir+"test2_cbc.txt")
86        #os.remove(currdir+"test_solve3.log")
87        #os.remove(currdir+"test2.soln")
88       
89    def test_solve4(self):
90        """ Test CBC - test4.nl """
91        results = self.cbc.solve(currdir+"test4.nl", rformat=ResultsFormat.log, logfile=currdir+"test_solve4.log", solnfile=currdir+"test4.soln")
92        results.write(currdir+"test_solve4.txt",times=False)
93        self.failUnlessFileEqualsBaseline(currdir+"test_solve4.txt", currdir+"test4_cbc.txt")
94        os.remove(currdir+"test_solve4.log")
95        if os.path.exists(currdir+"ipconvert.lp"):
96           os.remove(currdir+"ipconvert.lp")
97        #os.remove(currdir+"test4.soln")
98       
99    #
100    # This test is disabled, but it's useful for interactively exercising
101    # the option specifications of a solver
102    #
103    def Xtest_options(self):
104        """ Test CBC options behavior """
105        results = self.cbc.solve(currdir+"bell3a.mps", logfile=currdir+        "test_options.log", options="sec=0.1 foo=1 bar='a=b c=d' zz=yy")
106        results.write(currdir+"test_options.txt",times=False)
107        self.failUnlessFileEqualsBaseline(currdir+"test_options.txt", currdir+  "test4_cbc.txt")
108        #os.remove(currdir+"test4.sol")
109        #os.remove(currdir+"test_solve4.log")
110
111    def Xtest_prod(self):
112        """ Test CBC - prod.mod """
113        results = self.cbc.solve(currdir+"prod.mod", logfile=currdir+"test_prod.log", solnfile=currdir+"prod.soln")
114        results.write(currdir+"test_prod.txt",times=False)
115        self.failUnlessFileEqualsBaseline(currdir+"test_prod.txt", currdir+"prod_cbc.txt")
116        os.remove(currdir+"test_prod.log")
117        if os.path.exists(currdir+"ipconvert.lp"):
118           os.remove(currdir+"ipconvert.lp")
119        os.remove(currdir+"prod.soln")
120       
121    def test_error1(self):
122        """ Bad results format """
123        try:
124          results = self.cbc.solve(currdir+"test1.mps", rformat=ResultsFormat.sol)
125          self.fail("test_error1")
126        except ValueError:
127          pass
128
129    def test_error2(self):
130        """ Bad solve option """
131        try:
132          results = self.cbc.solve(currdir+"test1.mps", foo="bar")
133          self.fail("test_error2")
134        except ValueError:
135          pass
136
137    def test_error3(self):
138        """ Bad solve option """
139        try:
140          results = self.cbc.solve(currdir+"bad.mps", foo="bar")
141          self.fail("test_error3")
142        except ValueError:
143          pass
144
145
146class mip_all(mock_all):
147
148    def run(self, result=None):
149        """
150        Run the same tests, but only if CBC is available
151        """
152        global cbc_available
153        if not cbc_available:
154           return
155        mock_all.run(self,result)
156
157    def setUp(self):
158        self.do_setup(True)
159
160    def test_solve1a(self):
161        """ Test CBC - test1.mps """
162        results = self.cbc.solve(currdir+"test1.mps",logfile=currdir+"test_solve1a.log", solnfile=currdir+"test_solve1a.soln")
163        results.write(currdir+"test_solve1a.txt",times=False)
164        self.failUnlessFileEqualsBaseline(currdir+"test_solve1a.txt", currdir+"test1_cbc.txt")
165        os.remove(currdir+"test_solve1a.log")
166        os.remove(currdir+"test_solve1a.soln")
167
168
169if __name__ == "__main__":
170    unittest.main()
171
Note: See TracBrowser for help on using the repository browser.