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