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