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

Last change on this file since 1794 was 1794, checked in by wehart, 10 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: 10.0 KB
Line 
1#
2# Unit Tests for coopr.opt.base.convert
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 re
14import unittest
15from nose.tools import nottest
16import coopr.plugins
17from coopr.opt import ProblemFormat, ConverterError
18import coopr
19import pyutilib.th
20import pyutilib.services
21import pyutilib.common
22import pyutilib.plugin.core
23import pyutilib.plugin.config
24import xml
25import filecmp
26
27
28class MockArg(object):
29
30    def __init__(self):
31        pass
32
33    def valid_problem_types(self):
34        return [ProblemFormat.pyomo]
35
36    def write(self,filename="", format=None):
37        return (filename,None)
38
39class MockArg2(MockArg):
40
41    def valid_problem_types(self):
42        return [ProblemFormat.nl]
43
44    def write(self,filename="", format=None):
45        OUTPUT=open(filename,"w")
46        INPUT=open(currdir+"test4.nl")
47        for line in INPUT:
48          print >>OUTPUT, line,
49        OUTPUT.close()
50        INPUT.close()
51        return (filename,None)
52
53class MockArg3(MockArg):
54
55    def valid_problem_types(self):
56        return [ProblemFormat.mod]
57
58    def write(self,filename="", format=None):
59        return (filename,None)
60
61class MockArg4(MockArg):
62
63    def write(self,filename="", format=None):
64        OUTPUT=open(filename,"w")
65        INPUT=open(currdir+"test4.nl")
66        for line in INPUT:
67          print >>OUTPUT, line,
68        OUTPUT.close()
69        INPUT.close()
70        return (filename,None)
71
72
73class Test(pyutilib.th.TestCase):
74
75    def setUp(self):
76        pyutilib.services.TempfileManager.tempdir = currdir
77
78    def tearDown(self):
79        pyutilib.services.TempfileManager.clear_tempfiles()
80        #
81        # Reset all options
82        #
83        for ep in pyutilib.plugin.core.ExtensionPoint(pyutilib.plugin.config.IOption):
84            ep.reset()
85
86    def test_nl_nl1(self):
87        """ Convert from NL to NL """
88        ans = coopr.opt.convert_problem( ("test4.nl",), None, [ProblemFormat.nl])
89        self.failUnlessEqual(ans[0],("test4.nl",))
90
91    def test_nl_nl2(self):
92        """ Convert from NL to NL """
93        ans = coopr.opt.convert_problem( ("test4.nl","tmp.nl"), None, [ProblemFormat.nl])
94        self.failUnlessEqual(ans[0],("test4.nl","tmp.nl"))
95
96    def test_nl_lp1(self):
97        """ Convert from NL to LP """
98        try:
99           ans = coopr.opt.convert_problem( (currdir+"test4.nl",), None, [ProblemFormat.cpxlp])
100        except pyutilib.common.ApplicationError, err:
101           if not pyutilib.services.registered_executable("pico_convert") is None:
102              self.fail("Unexpected ApplicationError - pico_convert is enabled but not available: '%s'" % str(err))
103           return
104        except ConverterError, err:
105           if not pyutilib.services.registered_executable("pico_convert") is None:
106              self.fail("Unexpected ConverterError - pico_convert is enabled but not available: '%s'" % str(err))
107           return
108        self.failUnlessEqual(ans[0][0][-15:],"pico_convert.lp")
109        self.failUnlessFileEqualsBaseline(ans[0][0], currdir+"test1_convert.lp")
110
111    def test_mod_lp1(self):
112        """ Convert from MOD to LP """
113        try:
114           ans = coopr.opt.convert_problem( (currdir+"test3.mod",), None, [ProblemFormat.cpxlp])
115        except pyutilib.common.ApplicationError, err:
116           if not pyutilib.services.registered_executable("glpsol") is None:
117              self.fail("Unexpected ApplicationError - glpsol is enabled but not available: '%s'" % str(err))
118           return
119        except ConverterError, err:
120           if not pyutilib.services.registered_executable("glpsol") is None:
121              self.fail("Unexpected ConverterError - glpsol is enabled but not available: '%s'" % str(err))
122           return
123        self.failUnlessEqual(ans[0],("glpsol.lp",))
124        self.failUnlessFileEqualsBaseline(ans[0][0], currdir+"test2_convert.lp")
125
126    def test_mod_lp2(self):
127        """ Convert from MOD+DAT to LP """
128        try:
129           ans = coopr.opt.convert_problem( (currdir+"test5.mod",currdir+"test5.dat"), None, [ProblemFormat.cpxlp])
130        except pyutilib.common.ApplicationError, err:
131           if not pyutilib.services.registered_executable("glpsol") is None:
132              self.fail("Unexpected ApplicationError - glpsol is enabled but not available: '%s'" % str(err))
133           return
134        except ConverterError, err:
135           if not pyutilib.services.registered_executable("glpsol") is None:
136              self.fail("Unexpected ConverterError - glpsol is enabled but not available: '%s'" % str(err))
137           return
138        self.failUnlessEqual(ans[0],("glpsol.lp",))
139        self.failUnlessFileEqualsBaseline(ans[0][0], currdir+"test3_convert.lp")
140
141    def test_mock_lp1(self):
142        """ Convert from Pyomo to LP """
143        arg=MockArg()
144        ans = coopr.opt.convert_problem( (arg,ProblemFormat.cpxlp,arg), None, [ProblemFormat.cpxlp])
145        self.failIfEqual(re.match(".*tmp.*pyomo.lp$",ans[0][0]), None)
146
147    def test_mock_lp2(self):
148        """ Convert from NL to LP """
149        arg=MockArg2()
150        try:
151           ans = coopr.opt.convert_problem( (arg,), None, [ProblemFormat.cpxlp])
152        except ConverterError, err:
153           if pyutilib.services.registered_executable("pico_convert") is None:
154              return
155           else:
156              self.fail("Expected ApplicationError because pico_convert is not available: '%s'" % str(err))
157        self.failUnlessEqual(ans[0][0][-15:],"pico_convert.lp")
158        os.remove(ans[0][0])
159
160    def test_mock_mps1(self):
161        """ Convert from Pyomo to MPS """
162        arg=MockArg4()
163        try:
164           ans = coopr.opt.convert_problem( (arg,ProblemFormat.mps,arg), None, [ProblemFormat.mps])
165        except ConverterError, err:
166           if pyutilib.services.registered_executable("pico_convert") is None:
167                return
168           else:
169                self.fail("Expected ApplicationError because pico_convert is not available: '%s'" % str(err))
170        self.failUnlessEqual(ans[0][0][-16:],"pico_convert.mps")
171        os.remove(ans[0][0])
172
173    def test_mock_nl1(self):
174        """ Convert from Pyomo to NL """
175        arg=MockArg4()
176        ans = coopr.opt.convert_problem( (arg,ProblemFormat.nl,arg), None, [ProblemFormat.nl])
177        self.failIfEqual(re.match(".*tmp.*pyomo.nl$",ans[0][0]), None)
178        os.remove(ans[0][0])
179
180    def test_error1(self):
181        """ No valid problem types """
182        try:
183          coopr.opt.convert_problem( ("test4.nl","tmp.nl"), ProblemFormat.nl, [])
184          self.fail("Expected ConverterError exception")
185        except ConverterError, err:
186          pass
187
188    def test_error2(self):
189        """ Target problem type is not valid """
190        try:
191          coopr.opt.convert_problem( ("test4.nl","tmp.nl"), ProblemFormat.nl, [ProblemFormat.mps])
192          self.fail("Expected ConverterError exception")
193        except ConverterError:
194          pass
195
196    def test_error3(self):
197        """ Empty argument list """
198        try:
199          coopr.opt.convert_problem( (), None, [ProblemFormat.mps])
200          self.fail("Expected ConverterError exception")
201        except ConverterError:
202          pass
203
204    def test_error4(self):
205        """ Unknown source type """
206        try:
207          coopr.opt.convert_problem( ("prob.foo",), None, [ProblemFormat.mps])
208          self.fail("Expected ConverterError exception")
209        except ConverterError:
210          pass
211
212    def test_error5(self):
213        """ Unknown source type """
214        try:
215          coopr.opt.convert_problem( ("prob.lp",), ProblemFormat.nl, [ProblemFormat.nl])
216          self.fail("Expected ConverterError exception")
217        except ConverterError:
218          pass
219
220    def test_error6(self):
221        """ Cannot use pico_convert with more than one file """
222        try:
223          ans = coopr.opt.convert_problem( (currdir+"test4.nl","foo"), None, [ProblemFormat.cpxlp])
224          self.fail("Expected ConverterError exception")
225        except ConverterError:
226          pass
227
228    def test_error8(self):
229        """ Error when source file cannot be found """
230        try:
231          ans = coopr.opt.convert_problem( (currdir+"unknown.nl",), None, [ProblemFormat.cpxlp])
232          self.fail("Expected ConverterError exception")
233        except pyutilib.common.ApplicationError, err:
234           if pyutilib.services.registered_executable("pico_convert").enabled():
235              self.fail("Expected ApplicationError because pico_convert is not available: '%s'" % str(err))
236           return
237        except ConverterError:
238          pass
239
240    def test_error9(self):
241        """ The Opt configuration has not been initialized """
242        cmd = pyutilib.services.registered_executable("pico_convert")
243        if not cmd is None:
244            cmd.disable()
245        try:
246           ans = coopr.opt.convert_problem( (currdir+"test4.nl",), None, [ProblemFormat.cpxlp])
247           self.fail("This test didn't fail, but pico_convert should not be defined.")
248        except ConverterError:
249           pass
250        if not cmd is None:
251            cmd.enable()
252
253    def test_error10(self):
254        """ GLPSOL can only convert file data """
255        try:
256           arg = MockArg3()
257           ans = coopr.opt.convert_problem( (arg,ProblemFormat.cpxlp,arg), None, [ProblemFormat.cpxlp])
258           self.fail("This test didn't fail, but glpsol cannot handle objects.")
259        except ConverterError:
260           pass
261
262    def test_error11(self):
263        """ Cannot convert MOD that contains data """
264        try:
265          ans = coopr.opt.convert_problem( (currdir+"test3.mod",currdir+"test5.dat"), None, [ProblemFormat.cpxlp])
266          self.fail("Expected ConverterError exception because we provided a MOD file with a 'data;' declaration")
267        except pyutilib.common.ApplicationError, err:
268           if pyutilib.services.registered_executable("glpsol").enabled():
269              self.fail("Expected ApplicationError because glpsol is not available: '%s'" % str(err))
270           return
271        except ConverterError:
272          pass
273
274if __name__ == "__main__":
275   unittest.main()
Note: See TracBrowser for help on using the repository browser.