source: coopr.plugins/trunk/coopr/plugins/tests/mip/test_glpk.py @ 2334

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

Updating GLPK test to work when pico_convert is not available.

File size: 7.2 KB
Line 
1#
2# Unit Tests for coopr.plugins.mip.GLPK
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 pyutilib.th
14import pyutilib.common
15import pyutilib.services
16import unittest
17from nose.tools import nottest
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    glpk = coopr.plugins.mip.GLPK(keepFiles=True)
27    glpk_available= (not glpk.executable() is None)
28except IOError:
29    glpk_available=False
30    pass
31except pyutilib.common.ApplicationError:
32    glpk_available=False
33    pass
34
35try:
36    pico_convert =  pyutilib.services.registered_executable("pico_convert")
37    pico_convert_available= (not pico_convert is None)
38except pyutilib.common.ApplicationError:
39    pico_convert_available=False
40
41
42
43class mock_all(pyutilib.th.TestCase):
44
45    def setUp(self):
46        self.do_setup(False)
47        pyutilib.services.TempfileManager.tempdir = currdir
48
49    def do_setup(self,flag):
50        if flag:
51            self.glpk = coopr.plugins.mip.GLPK(keepFiles=True)
52        else:
53            self.glpk = coopr.plugins.mip.MockGLPK(keepFiles=True)
54
55    def tearDown(self):
56        if os.path.exists(currdir+"glpk.soln"):
57           os.remove(currdir+"glpk.soln")
58        pyutilib.services.TempfileManager.clear_tempfiles()
59
60    def test_path(self):
61        """ Verify that the GLPK path is what is expected """
62        if type(self.glpk) is 'GLPK':
63           self.failUnlessEqual(self.glpk.executable.split(os.sep)[-1],"GLPK"+coopr.util.executable_extension)
64
65    def test_solve1(self):
66        """ Test GLPK - test1.mps """
67        results = self.glpk.solve(currdir+"test1.mps",logfile=currdir+"test_solve1.log", solnfile=currdir+"test1.soln")
68        results.write(filename=currdir+"test_solve1.txt", times=False)
69        self.failUnlessFileEqualsBaseline(currdir+"test_solve1.txt", currdir+"test1_glpk.txt")
70        os.remove(currdir+"test_solve1.log")
71        os.remove(currdir+"test1.soln")
72
73    def test_solve2a(self):
74        """ Test GLPK - test1.mps """
75        results = self.glpk.solve(currdir+"test1.mps", rformat=ResultsFormat.soln, logfile=currdir+"test_solve2a.log", solnfile=currdir+"test1.soln")
76        results.write(filename=currdir+"test_solve2a.txt", times=False)
77        self.failUnlessFileEqualsBaseline(currdir+"test_solve2a.txt", currdir+"test1_glpk.txt")
78        os.remove(currdir+"test_solve2a.log")
79        os.remove(currdir+"test1.soln")
80
81    def test_solve2b(self):
82        """ Test GLPK - test1.mps """
83        results = self.glpk.solve(currdir+"test1.mps", pformat=ProblemFormat.mps, rformat=ResultsFormat.soln, logfile=currdir+"test_solve2b.log", solnfile=currdir+"test1.soln")
84        results.write(filename=currdir+"test_solve2b.txt", times=False)
85        self.failUnlessFileEqualsBaseline(currdir+"test_solve2b.txt", currdir+"test1_glpk.txt")
86        os.remove(currdir+"test_solve2b.log")
87        os.remove(currdir+"test1.soln")
88
89    def test_solve3(self):
90        """ Test GLPK - test2.lp """
91        results = self.glpk.solve(currdir+"test2.lp", logfile=currdir+"test_solve3.log", solnfile=currdir+"test2.soln")
92        results.write(filename=currdir+"test_solve3.txt", times=False)
93        self.failUnlessFileEqualsBaseline(currdir+"test_solve3.txt", currdir+"test2_glpk.txt")
94        os.remove(currdir+"test_solve3.log")
95        os.remove(currdir+"test2.soln")
96       
97    def test_solve4(self):
98        """ Test GLPK - test4.nl """
99        if pico_convert_available:
100            results = self.glpk.solve(currdir+"test4.nl", rformat=ResultsFormat.soln, logfile=currdir+"test_solve4.  log")
101            results.write(filename=currdir+"test_solve4.txt",times=False)
102            self.failUnlessFileEqualsBaseline(currdir+"test_solve4.txt", currdir+"test4_glpk.txt")
103            os.remove(currdir+"test4.soln")
104            os.remove(currdir+"test_solve4.log")
105        else:
106            try:
107                results = self.glpk.solve(currdir+"test4.nl", rformat=ResultsFormat.soln, logfile=currdir+"test_solve4.log")
108            except coopr.opt.ConverterError:
109                pass
110
111       
112    def Xtest_solve5(self):
113        """ Test GLPK - test5.mps """
114        results = self.glpk.solve(currdir+"test5.mps", rformat=ResultsFormat.soln, logfile=currdir+"test_solve5.log", solnfile=currdir+"test5.soln",timelimit=300)
115        self.failUnlessEqual(results.solution(0).status, coopr.opt.SolutionStatus.bestSoFar)
116        os.remove(currdir+"test_solve5.log")
117        os.remove(currdir+"test5.soln")
118       
119    def test_diet(self):
120        """ Test GLPK - diet.mod """
121        results = self.glpk.solve(currdir+"diet.mod", logfile=currdir+"test_diet.log", solnfile=currdir+"diet.soln")
122        results.write(filename=currdir+"test_diet.txt", times=False)
123        self.failUnlessFileEqualsBaseline(currdir+"test_diet.txt", currdir+"diet_glpk.txt")
124        os.remove(currdir+"test_diet.log")
125        if os.path.exists(currdir+"ipconvert.lp"):
126           os.remove(currdir+"ipconvert.lp")
127        os.remove(currdir+"diet.soln")
128       
129    #
130    # This test is disabled, but it's useful for interactively exercising
131    # the option specifications of a solver
132    #
133    def Xtest_options(self):
134        """ Test GLPK options behavior """
135        results = self.glpk.solve(currdir+"bell3a.mps", logfile=currdir+"test_options.log", options="maxCPUMinutes=0.1 foo=1 bar='a=b c=d' zz=yy")
136        results.write(filename=currdir+"test_options.txt",times=False)
137        self.failUnlessFileEqualsBaseline(currdir+"test_options.txt", currdir+  "test4_glpk.txt")
138        #os.remove(currdir+"test4.sol")
139        #os.remove(currdir+"test_solve4.log")
140
141    def Xtest_error1(self):
142        """ Bad results format """
143        try:
144          results = self.glpk.solve(currdir+"test1.mps", rformat=ResultsFormat.sol)
145          self.fail("test_error1")
146        except ValueError:
147          pass
148
149    def test_error2(self):
150        """ Bad solve option """
151        try:
152          results = self.glpk.solve(currdir+"test1.mps", foo="bar")
153          self.fail("test_error2")
154        except ValueError:
155          pass
156
157    def test_error3(self):
158        """ Bad solve option """
159        try:
160          results = self.glpk.solve(currdir+"bad.mps", foo="bar")
161          self.fail("test_error3")
162        except ValueError:
163          pass
164
165
166class mip_all(mock_all):
167
168    def run(self, result=None):
169        """
170        Run the same tests, but only if GLPK is available
171        """
172        global glpk_available
173        if not glpk_available:
174           return
175        mock_all.run(self,result)
176
177    def setUp(self):
178        self.do_setup(True)
179
180    def test_solve1a(self):
181        """ Test GLPK - test1.mps """
182        results = self.glpk.solve(currdir+"test1.mps")
183        results.write(filename=currdir+"test_solve1.txt", times=False)
184        self.failUnlessFileEqualsBaseline(currdir+"test_solve1.txt", currdir+"test1_glpk.txt")
185        if os.path.exists(currdir+"glpk.log"):
186            os.remove(currdir+"glpk.log")
187        if os.path.exists(currdir+"glpk.soln"):
188            os.remove(currdir+"glpk.soln")
189
190
191if __name__ == "__main__":
192    unittest.main()
193
Note: See TracBrowser for help on using the repository browser.