source: trunk/coopr/plugins/converter/pico.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: 3.1 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 *
14from pyutilib.plugin import *
15import pyutilib
16
17
18class PicoMIPConverter(ManagedSingletonPlugin):
19
20    implements(IProblemConverter)
21
22    executable = ExtensionPoint(IExternalExecutable)
23
24    def __init__(self,**kwds):
25        ManagedSingletonPlugin.__init__(self,**kwds)
26
27    def can_convert(self, from_type, to_type):
28        """Returns true if this object supports the specified conversion"""
29        #
30        # Test if the glpsol executable is available
31        #
32        if self.executable.service("pico_convert") is None:
33            return False
34        #
35        # Return True for specific from/to pairs
36        #
37        if from_type == ProblemFormat.nl and to_type == ProblemFormat.cpxlp:
38            return True
39        if from_type == ProblemFormat.nl and to_type == ProblemFormat.mps:
40            return True
41        if from_type == ProblemFormat.mps and to_type == ProblemFormat.cpxlp:
42            return True
43        if from_type == ProblemFormat.cpxlp and to_type == ProblemFormat.mps:
44            return True
45        return False
46
47    def apply(self, *args):
48        """
49        Run the external pico_convert utility
50        """
51        if len(args) != 3:
52            raise ConverterError, "Cannot apply pico_convert with more than one filename or model"
53        cmd = self.executable.service("pico_convert")
54        if cmd is None:
55            raise ConverterError, "The 'pico_convert' application cannot be found"
56
57        pico_convert_cmd = cmd.get_path()
58        target=str(args[1])
59        if target=="cpxlp":
60            target="lp"
61        # NOTE: if you have an extra "." in the suffix, the pico_convert program fails to output to the correct filename.
62        output_filename = pyutilib.TempfileManager.create_tempfile(suffix = 'pico_convert.' + target)
63        if not isinstance(args[2],basestring):
64            fname= pyutilib.TempfileManager.create_tempfile(suffix= 'pico_convert.' +str(args[0]))
65            args[2].write(filename=fname, format=args[1])
66            cmd = pico_convert_cmd +" --output="+output_filename+" "+target+" "+fname
67        else:
68            cmd = pico_convert_cmd +" --output="+output_filename+" "+target
69            for item in args[2:]:
70                if not os.path.exists(item):
71                    raise ConverterError, "File "+item+" does not exist!"
72                cmd = cmd + " "+item
73        print "Running command: "+cmd
74        pyutilib.run_command(cmd)
75        if not os.path.exists(output_filename):       #pragma:nocover
76            raise pyutilib.ApplicationError, \
77                    "Problem launching 'pico_convert' to create "+output_filename
78        return (output_filename,)
79
80
Note: See TracBrowser for help on using the repository browser.