source: trunk/test/plugins/mip/test_cplex.py @ 1663

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

Misc changes in test that reflect (a) the move of coopr.core to
coopr.opt and (b) refinement of test failure management when PICO is
available but not pico_convert.

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