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

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

Removing coopr.opt-depricated, and
renaming coopr.core to coopr.opt.

Unfortunately, plugins are still not being properly loaded, so
some of the tests are failing.

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