source: trunk/coopr/plugins/converter/pico.py @ 1772

Last change on this file since 1772 was 1772, checked in by jwatson, 11 years ago

Updates to allow for proper handling of variable maps in cases where formats cannot handle fully qualified,
human-readable variable names. Many ripple effects, as problem writers now must return a variable map (or None,
if not applicable. CBC now properly handles and reads NL and SOL input/output combinations!

File size: 3.3 KB
Line 
1#  _________________________________________________________________________
2#
3#  Coopr: A COmmon Optimization Python Repository
4#  Copyright (c) 2008 Sandia Corporation.
5#  This software is distributed under the BSD License.
6#  Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
7#  the U.S. Government retains certain rights in this software.
8#  For more information, see the Coopr README.txt file.
9#  _________________________________________________________________________
10
11__all__ = ['PicoMIPConverter']
12
13from coopr.opt.base import *
14
15from pyutilib.plugin.core import *
16from pyutilib.plugin.config import *
17from pyutilib.plugin.executables import *
18import pyutilib.services
19import pyutilib.common
20import pyutilib.subprocess
21
22class PicoMIPConverter(ManagedSingletonPlugin):
23
24    implements(IProblemConverter)
25
26    executable = ExtensionPoint(IExternalExecutable)
27
28    def __init__(self,**kwds):
29        ManagedSingletonPlugin.__init__(self,**kwds)
30
31    def can_convert(self, from_type, to_type):
32        """Returns true if this object supports the specified conversion"""
33        #
34        # Test if the glpsol executable is available
35        #
36        if self.executable.service("pico_convert") is None:
37            return False
38        #
39        # Return True for specific from/to pairs
40        #
41        if from_type == ProblemFormat.nl and to_type == ProblemFormat.cpxlp:
42            return True
43        if from_type == ProblemFormat.nl and to_type == ProblemFormat.mps:
44            return True
45        if from_type == ProblemFormat.mps and to_type == ProblemFormat.cpxlp:
46            return True
47        if from_type == ProblemFormat.cpxlp and to_type == ProblemFormat.mps:
48            return True
49        return False
50
51    def apply(self, *args):
52        """
53        Run the external pico_convert utility
54        """
55        if len(args) != 3:
56            raise ConverterError, "Cannot apply pico_convert with more than one filename or model"
57        cmd = self.executable.service("pico_convert")
58        if cmd is None:
59            raise ConverterError, "The 'pico_convert' application cannot be found"
60
61        pico_convert_cmd = cmd.get_path()
62        target=str(args[1])
63        if target=="cpxlp":
64            target="lp"
65        # NOTE: if you have an extra "." in the suffix, the pico_convert program fails to output to the correct filename.
66        output_filename = pyutilib.services.TempfileManager.create_tempfile(suffix = 'pico_convert.' + target)
67        if not isinstance(args[2],basestring):
68            fname= pyutilib.services.TempfileManager.create_tempfile(suffix= 'pico_convert.' +str(args[0]))
69            args[2].write(filename=fname, format=args[1])
70            cmd = pico_convert_cmd +" --output="+output_filename+" "+target+" "+fname
71        else:
72            cmd = pico_convert_cmd +" --output="+output_filename+" "+target
73            for item in args[2:]:
74                if not os.path.exists(item):
75                    raise ConverterError, "File "+item+" does not exist!"
76                cmd = cmd + " "+item
77        print "Running command: "+cmd
78        pyutilib.subprocess.run(cmd)
79        if not os.path.exists(output_filename):       #pragma:nocover
80            raise pyutilib.common.ApplicationError, \
81                    "Problem launching 'pico_convert' to create "+output_filename
82        return (output_filename,),None # no variable map at the momoent
83
84
Note: See TracBrowser for help on using the repository browser.