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

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

Rework of Coopr to use the new PyUtilib? package decomposition.

NOTE: to use Coopr with this update, we need to work with a new version of coopr_install.

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