source: coopr.plugins/trunk/coopr/plugins/converter/glpsol.py @ 2201

Last change on this file since 2201 was 2201, checked in by wehart, 10 years ago

Update to Coopr to account for changes in PyUtilib? package names.

File size: 3.8 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__ = ['GlpsolMIPConverter']
12
13from coopr.opt.base import *
14
15from pyutilib.component.core import *
16from pyutilib.component.config import *
17from pyutilib.component.executables import *
18import pyutilib.subprocess
19import pyutilib.common
20
21class GlpsolMIPConverter(ManagedSingletonPlugin):
22
23    implements(IProblemConverter)
24
25    executable = ExtensionPoint(IExternalExecutable)
26 
27    def __init__(self,**kwds):
28        ManagedSingletonPlugin.__init__(self,**kwds)
29
30    def can_convert(self, from_type, to_type):
31        """Returns true if this object supports the specified conversion"""
32        #
33        # Test if the glpsol executable is available
34        #
35        if self.executable.service("glpsol") is None:
36            return False
37        #
38        # Return True for specific from/to pairs
39        #
40        if from_type == ProblemFormat.mod and to_type == ProblemFormat.cpxlp:
41            return True
42        if from_type == ProblemFormat.mod and to_type == ProblemFormat.mps:
43            return True
44        return False
45
46    def apply(self, *args):
47        """Convert an instance of one type into another"""
48        if not isinstance(args[2],basestring):
49            raise ConverterError, "Can only apply glpsol to convert file data"
50        cmd = self.executable.service("glpsol").get_path()
51        if cmd is None:
52            raise ConverterError, "The 'glpsol' executable cannot be found"
53        cmd = cmd +" --math"
54        #
55        # MPS->LP conversion is ignored in coverage because it's not being
56        #   used; instead, we're using pico_convert for this conversion
57        #
58        if args[1] == ProblemFormat.mps: #pragma:nocover
59            ofile="glpsol.mps"
60            cmd = cmd + " --check --wfreemps "+ofile
61        elif args[1] == ProblemFormat.cpxlp:
62            ofile="glpsol.lp"
63            cmd = cmd + " --check --wcpxlp "+ofile
64        if len(args[2:]) == 1:
65            cmd = cmd+" "+args[2]
66        else:
67            #
68            # Create a temporary model file, since GLPSOL can only
69            # handle one input file
70            #
71            OUTPUT=open("glpsol.mod","w")
72            flag=False
73            #
74            # Read the model file
75            #
76            INPUT= open(args[2])
77            for line in INPUT:
78                line = line.strip()
79                if line == "data;":
80                    raise ConverterError, "Problem composing mathprog model and data files - mathprog file already has data in it!"
81                if line != "end;":
82                    print >>OUTPUT, line
83            INPUT.close()
84            print >>OUTPUT, "data;"
85            #
86            # Read the data files
87            #
88            for file in args[3:]:
89                INPUT= open(file)
90                for line in INPUT:
91                    line = line.strip()
92                    if line != "end;" and line != "data;":
93                        print >>OUTPUT, line
94                INPUT.close()
95                print >>OUTPUT, "end;"
96            OUTPUT.close()
97            cmd = cmd+" glpsol.mod"
98        pyutilib.subprocess.run(cmd)
99        if not os.path.exists(ofile):       #pragma:nocover
100            raise pyutilib.common.ApplicationError, "Problem launching 'glpsol' to create "+ofile
101        if os.path.exists("glpsol.mod"):
102            os.remove("glpsol.mod")
103        return (ofile,),None # empty variable map
Note: See TracBrowser for help on using the repository browser.