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