source: trunk/coopr/plugins/converter/pyomo.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: 2.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
12__all__ = ['PyomoMIPConverter']
13
14from coopr.opt.base import *
15from pico import PicoMIPConverter
16
17from pyutilib.plugin.core import *
18from pyutilib.plugin.config import *
19from pyutilib.plugin.executables import *
20import pyutilib.services
21
22class PyomoMIPConverter(ManagedSingletonPlugin):
23
24    implements(IProblemConverter)
25
26    cmd = ExtensionPoint(IExternalExecutable)
27    pico_converter = PicoMIPConverter()
28
29    def __init__(self,**kwds):
30        ManagedSingletonPlugin.__init__(self,**kwds)
31
32    def can_convert(self, from_type, to_type):
33        """Returns true if this object supports the specified conversion"""
34        #
35        # Return True for specific from/to pairs
36        #
37        if from_type == ProblemFormat.pyomo and to_type == ProblemFormat.nl:
38            return True
39        if from_type == ProblemFormat.pyomo and to_type == ProblemFormat.cpxlp:
40            return True
41        if from_type == ProblemFormat.pyomo and to_type == ProblemFormat.mps:
42            return True
43        return False
44
45    def apply(self, *args):
46        """
47        Generate a NL or LP file from Pyomo, and then do subsequent
48        conversions.
49        """
50
51        if args[1] is ProblemFormat.cpxlp:
52            problem_filename = pyutilib.services.TempfileManager.create_tempfile(suffix = '.pyomo.lp')
53            (problem_filename, varmap) = args[2].write(filename=problem_filename,format=ProblemFormat.cpxlp)
54            return (problem_filename,),None # no map file is necessary
55        elif args[1] is ProblemFormat.mps:
56            # TBD: We don't support a variable map file when going from NL to MPS within the PICO converter.
57            problem_filename = pyutilib.services.TempfileManager.create_tempfile(suffix = '.pyomo.nl')
58            (problem_filename, varmap) = args[2].write(filename=problem_filename,format=ProblemFormat.nl)
59            ans = self.pico_converter.apply(ProblemFormat.nl,ProblemFormat.mps,problem_filename)
60            os.remove(problem_filename)
61            return ans
62        elif args[1] is ProblemFormat.nl:
63            problem_filename = pyutilib.services.TempfileManager.create_tempfile(suffix = '.pyomo.nl')
64            (problem_filename, variable_map) = args[2].write(filename=problem_filename,format=ProblemFormat.nl)
65            return (problem_filename,),variable_map # map file is necessary
Note: See TracBrowser for help on using the repository browser.