source: coopr.plugins/trunk/coopr/plugins/tests/mip/test_pico.py @ 2308

Last change on this file since 2308 was 2308, checked in by wehart, 10 years ago

Generalizing the suffix management in the MIP solvers so
they match regular expressions for suffixes. This allows
the user to specify the '.*' suffix, which matches everything.

Reworked the MIP tests, which all pass now.

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