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