source: trunk/coopr/plugins/converter/pyomo.py @ 1768

Last change on this file since 1768 was 1768, checked in by wehart, 11 years ago

Rework of Coopr to use the new PyUtilib? package decomposition.

NOTE: to use Coopr with this update, we need to work with a new version of coopr_install.

File size: 2.5 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 pyutilib.plugin.core import *
16from pyutilib.plugin.config import *
17from pyutilib.plugin.executables import *
18import pyutilib.services
19from pico import PicoMIPConverter
20
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        if args[1] is ProblemFormat.cpxlp:
51            problem_filename = pyutilib.services.TempfileManager.create_tempfile(suffix = '.pyomo.lp')
52            args[2].write(filename=problem_filename,format=ProblemFormat.cpxlp)
53            return (problem_filename,)
54        elif args[1] is ProblemFormat.mps:
55            problem_filename = pyutilib.services.TempfileManager.create_tempfile(suffix = '.pyomo.nl')
56            args[2].write(filename=problem_filename,format=ProblemFormat.nl)
57            ans = self.pico_converter.apply(ProblemFormat.nl,ProblemFormat.mps,problem_filename)
58            os.remove(problem_filename)
59            return ans
60        elif args[1] is ProblemFormat.nl:
61            problem_filename = pyutilib.services.TempfileManager.create_tempfile(suffix = '.pyomo.nl')
62            args[2].write(filename=problem_filename,format=ProblemFormat.nl)
63            return (problem_filename,)
64
65
66
Note: See TracBrowser for help on using the repository browser.