source: trunk/test/plugins/mip/test_glpk.py @ 1794

Last change on this file since 1794 was 1794, checked in by wehart, 11 years ago

Misc bug fixes that were introduced by the introduction of variable_map
data, which is now called symbol_map.

Note: some tests still fail, due to the fact that pico_convert does not
generate symbol mapping information. This is being resolved.

File size: 6.7 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
24try:
25    glpk = coopr.plugins.mip.GLPK(keepFiles=True)
26    glpk_available= (not glpk.executable() is None)
27except IOError:
28    glpk_available=False
29    pass
30except pyutilib.common.ApplicationError:
31    glpk_available=False
32    pass
33
34
35class mock_all(pyutilib.th.TestCase):
36
37    def setUp(self):
38        self.do_setup(False)
39        pyutilib.services.TempfileManager.tempdir = currdir
40
41    def do_setup(self,flag):
42        if flag:
43            self.glpk = coopr.plugins.mip.GLPK(keepFiles=True)
44        else:
45            self.glpk = coopr.plugins.mip.MockGLPK(keepFiles=True)
46
47    def tearDown(self):
48        if os.path.exists(currdir+"glpk.soln"):
49           os.remove(currdir+"glpk.soln")
50        pyutilib.services.TempfileManager.clear_tempfiles()
51
52    def test_path(self):
53        """ Verify that the GLPK path is what is expected """
54        if type(self.glpk) is 'GLPK':
55           self.failUnlessEqual(self.glpk.executable.split(os.sep)[-1],"GLPK"+coopr.util.executable_extension)
56
57    def test_solve1(self):
58        """ Test GLPK - test1.mps """
59        results = self.glpk.solve(currdir+"test1.mps",logfile=currdir+"test_solve1.log", solnfile=currdir+"test1.soln")
60        results.write(currdir+"test_solve1.txt", times=False)
61        self.failUnlessFileEqualsBaseline(currdir+"test_solve1.txt", currdir+"test1_glpk.txt")
62        os.remove(currdir+"test_solve1.log")
63        os.remove(currdir+"test1.soln")
64
65    def test_solve2a(self):
66        """ Test GLPK - test1.mps """
67        results = self.glpk.solve(currdir+"test1.mps", rformat=ResultsFormat.log, logfile=currdir+"test_solve2a.log", solnfile=currdir+"test1.soln")
68        results.write(currdir+"test_solve2a.txt", times=False)
69        self.failUnlessFileEqualsBaseline(currdir+"test_solve2a.txt", currdir+"test1_glpk.txt")
70        os.remove(currdir+"test_solve2a.log")
71        os.remove(currdir+"test1.soln")
72
73    def test_solve2b(self):
74        """ Test GLPK - test1.mps """
75        results = self.glpk.solve(currdir+"test1.mps", pformat=ProblemFormat.mps, rformat=ResultsFormat.log, logfile=currdir+"test_solve2b.log", solnfile=currdir+"test1.soln")
76        results.write(currdir+"test_solve2b.txt", times=False)
77        self.failUnlessFileEqualsBaseline(currdir+"test_solve2b.txt", currdir+"test1_glpk.txt")
78        os.remove(currdir+"test_solve2b.log")
79        os.remove(currdir+"test1.soln")
80
81    def test_solve3(self):
82        """ Test GLPK - test2.lp """
83        results = self.glpk.solve(currdir+"test2.lp", logfile=currdir+"test_solve3.log", solnfile=currdir+"test2.soln")
84        results.write(currdir+"test_solve3.txt", times=False)
85        self.failUnlessFileEqualsBaseline(currdir+"test_solve3.txt", currdir+"test2_glpk.txt")
86        os.remove(currdir+"test_solve3.log")
87        os.remove(currdir+"test2.soln")
88       
89    def test_solve4(self):
90        """ Test GLPK - test4.nl """
91        results = self.glpk.solve(currdir+"test4.nl", rformat=ResultsFormat.log, logfile=currdir+"test_solve4.log", solnfile=currdir+"test4.soln")
92        results.write(currdir+"test_solve4.txt", times=False)
93        self.failUnlessFileEqualsBaseline(currdir+"test_solve4.txt", currdir+"test4_glpk.txt")
94        os.remove(currdir+"test_solve4.log")
95        #if os.path.exists(currdir+"ipconvert.lp"):
96           #os.remove(currdir+"ipconvert.lp")
97        #os.remove(currdir+"test4.soln")
98       
99    def Xtest_solve5(self):
100        """ Test GLPK - test5.mps """
101        results = self.glpk.solve(currdir+"test5.mps", rformat=ResultsFormat.log, logfile=currdir+"test_solve5.log", solnfile=currdir+"test5.soln",timelimit=300)
102        self.failUnlessEqual(results.solution(0).status, coopr.opt.SolutionStatus.bestSoFar)
103        os.remove(currdir+"test_solve5.log")
104        os.remove(currdir+"test5.soln")
105       
106    def test_diet(self):
107        """ Test GLPK - diet.mod """
108        results = self.glpk.solve(currdir+"diet.mod", logfile=currdir+"test_diet.log", solnfile=currdir+"diet.soln")
109        results.write(currdir+"test_diet.txt", times=False)
110        self.failUnlessFileEqualsBaseline(currdir+"test_diet.txt", currdir+"diet_glpk.txt")
111        os.remove(currdir+"test_diet.log")
112        if os.path.exists(currdir+"ipconvert.lp"):
113           os.remove(currdir+"ipconvert.lp")
114        os.remove(currdir+"diet.soln")
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 GLPK options behavior """
122        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")
123        results.write(currdir+"test_options.txt",times=False)
124        self.failUnlessFileEqualsBaseline(currdir+"test_options.txt", currdir+  "test4_glpk.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.glpk.solve(currdir+"test1.mps", rformat=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.glpk.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.glpk.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        """
157        Run the same tests, but only if GLPK is available
158        """
159        global glpk_available
160        if not glpk_available:
161           return
162        mock_all.run(self,result)
163
164    def setUp(self):
165        self.do_setup(True)
166
167    def test_solve1a(self):
168        """ Test GLPK - test1.mps """
169        results = self.glpk.solve(currdir+"test1.mps")
170        results.write(currdir+"test_solve1.txt", times=False)
171        self.failUnlessFileEqualsBaseline(currdir+"test_solve1.txt", currdir+"test1_glpk.txt")
172        if os.path.exists(currdir+"glpk.log"):
173            os.remove(currdir+"glpk.log")
174        if os.path.exists(currdir+"glpk.soln"):
175            os.remove(currdir+"glpk.soln")
176
177
178if __name__ == "__main__":
179    unittest.main()
180
Note: See TracBrowser for help on using the repository browser.