source: trunk/test/plugins/mip/test_pico.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.8 KB
Line 
1#
2# Unit Tests for coopr.plugins.mip.PICO
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
22
23try:
24    pico_convert =  pyutilib.registered_executable("pico_convert")
25    pico_convert_available= (not pico_convert is None)
26except pyutilib.ApplicationError:
27    pico_convert_available=False
28
29try:
30    pico = coopr.plugins.mip.PICO(keepFiles=True)
31    pico_available= (not pico.executable() is None)
32except pyutilib.ApplicationError:
33    pico_available=False
34    pass
35
36
37class mock_all(pyutilib_th.TestCase):
38
39    def setUp(self):
40        self.do_setup(False)
41
42    def do_setup(self,flag):
43        global tmpdir
44        tmpdir = os.getcwd()
45        os.chdir(currdir)
46        pyutilib.TempfileManager.tempdir = currdir
47        if flag:
48            self.pico = coopr.plugins.mip.PICO(keepFiles=True)
49        else:
50            self.pico = coopr.plugins.mip.MockPICO(keepFiles=True)
51
52    def tearDown(self):
53        global tmpdir
54        pyutilib.TempfileManager.clear_tempfiles()
55        os.chdir(tmpdir)
56
57    def test_path(self):
58        """ Verify that the PICO path is what is expected """
59        if type(self.pico) is 'PICO':
60           self.failUnlessEqual(self.pico.executable.split(os.sep)[-1],"PICO"+coopr.util.executable_extension)
61
62    def test_solve1(self):
63        """ Test PICO - test1.mps """
64        results = self.pico.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_pico.txt")
67        os.remove(currdir+"test_solve1.log")
68
69    def test_solve2a(self):
70        """ Test PICO - test1.mps """
71        results = self.pico.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_pico.txt")
74        os.remove(currdir+"test_solve2a.log")
75
76    def test_solve2b(self):
77        """ Test PICO - test1.mps """
78        results = self.pico.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_pico.txt")
81        os.remove(currdir+"test_solve2b.log")
82
83    def test_solve3(self):
84        """ Test PICO - test2.lp """
85        results = self.pico.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_pico.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 PICO - test4.nl """
94        if pico_convert_available:
95            results = self.pico.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_pico.txt")
98            os.remove(currdir+"test4.sol")
99            os.remove(currdir+"test_solve4.log")
100        else:
101            try:
102                results = self.pico.solve(currdir+"test4.nl", logfile=currdir+"test_solve4.log")
103            except ConverterError:
104                pass
105       
106    def Xtest_mock5(self):
107        """ Mock Test PICO - test5.mps """
108        if pico_available:
109           pass
110        results = self.pico.solve(currdir+"test4.nl", logfile=currdir+"test_solve5.log")
111        results.write(currdir+"test_mock5.txt",times=False)
112        self.failUnlessFileEqualsBaseline(currdir+"test_mock5.txt", currdir+"test4_pico.txt")
113        os.remove(currdir+"test4.sol.txt")
114        os.remove(currdir+"test_solve5.log")
115       
116    #
117    # This test is disabled, but it's useful for interactively exercising
118    # the option specifications of a solver
119    #
120    def Xtest_options(self):
121        """ Test PICO options behavior """
122        results = self.pico.solve(currdir+"bell3a.mps", logfile=currdir+"test_options.log", options="maxCPUMinutes=0.1 foo=1 bar='a=b c=d' zz=yy")
123        results.write(currdir+"test_options.txt",times=False)
124        self.failUnlessFileEqualsBaseline(currdir+"test_options.txt", currdir+"test4_pico.txt")
125        #os.remove(currdir+"test4.sol")
126        os.remove(currdir+"test_solve4.log")
127       
128    def test_error1(self):
129        """ Bad results format """
130        try:
131          results = self.pico.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.pico.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.pico.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 pico_available
157        if not pico_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.