source: trunk/test/plugins/mip/test_cbc.py @ 1657

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

Removing coopr.opt-depricated, and
renaming coopr.core to coopr.opt.

Unfortunately, plugins are still not being properly loaded, so
some of the tests are failing.

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