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