source: trunk/coopr/pyomo/base/plugin.py @ 1290

Last change on this file since 1290 was 1290, checked in by wehart, 12 years ago

Rework of Pyomo to enable preprocessor actions to
manage all post-instance-generation activities. This includes

. New preprocess action classes in the pyomo/preprocess directory

. A simple preprocessor plugin, which simply applies preprocessor

actions in an order specified by the action-specific ranks

. The AMPL and CPXLP writers are no longer subclasses of the

problem_utils class (which will be removed shortly). Thus, these
classes are no longer directly responsible for how preprocessor
actions take place.

. The Constraint and Objective classes were reworked to integrate

their data into a single _data dictionary. This simplified
the management of id and label information.

Notes:

. The preprocessors insert some data into the model and model data.

for example, variable id's and label's are specified, which are
what is used when writing out data to NL and LP files. Right now,
this is somewhat ad-hoc; I'm looking for a more systematic way
to managed this information.

. The preprocessors are very much tailored to the needs of the NL

and LP writers. We need to figure out a mechanism to tailoring
preprocessors to specific target solvers (e.g. tailoring
preprocessing for the NL writer, or tailoring preprocessing for
MILP models).

File size: 3.1 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__ = ['IPyomoExpression', 'ExpressionRegistration', 'ExpressionFactory',
12        'IPyomoSet', 'IModelComponent', 'ComponentRegistration',
13        'ComponentFactory',
14        'IPyomoPresolver', 'IPyomoPresolveAction']
15
16from pyutilib.plugin import *
17
18
19class IPyomoPresolver(Interface):
20
21    def get_actions(self):
22        """Return a list of presolve actions, in the order in which
23        they will be applied."""
24   
25    def activate_action(self, action):
26        """Activate an action, but leave its default rank"""
27
28    def deactivate_action(self, action):
29        """Deactivate an action"""
30
31    def set_actions(self, actions):
32        """Set presolve action list"""
33
34    def presolve(self, instance):
35        """Apply the presolve actions to this instance, and return the
36        revised instance"""
37
38
39class IPyomoPresolveAction(Interface):
40
41    def presolve(self, instance):
42        """Apply the presolve action to this instance, and return the
43        revised instance"""
44
45    def rank(self):
46        """Return an integer that is used to automatically order presolve actions,
47        from low to high rank."""
48
49
50class IPyomoExpression(Interface):
51
52    def type(self):
53        """Return the type of expression"""
54
55    def create(self, args):
56        """Create an instance of this expression type"""
57
58
59class ExpressionRegistration(Plugin):
60
61    implements(IPyomoExpression)
62
63    def __init__(self, type, cls):
64        Plugin.__init__(self, name=type)
65        self._type = type
66        self._cls = cls
67
68    def type(self):
69        return self._type
70
71    def create(self, args):
72        return self._cls(args)
73
74
75def ExpressionFactory(name=None, args=[]):
76    ep = ExtensionPoint(IPyomoExpression)
77    if name is None:
78        return map(lambda x:x.name, ep())
79    return ep.service(name).create(args)
80
81
82
83
84class IPyomoSet(Interface):
85    pass
86
87
88
89class IModelComponent(Interface):
90
91    def type(self):
92        """Return the type of component"""
93
94    def cls(self):
95        """Return the class type of this component"""
96
97    def create(self):
98        """Create an instance of this component type"""
99
100
101class ComponentRegistration(Plugin):
102
103    implements(IModelComponent)
104
105    def __init__(self, type, cls, doc=""):
106        Plugin.__init__(self, name=type)
107        self._type = type
108        self._cls = cls
109        self.doc=doc
110
111    def type(self):
112        return self._type
113
114    def cls(self):
115        return self._cls
116
117    def create(self):
118        return self._cls()
119
120
121def ComponentFactory(name=None, args=[]):
122    ep = ExtensionPoint(IModelComponent)
123    if name is None:
124        return map(lambda x:x.name, ep())
125    return ep.service(name).create(*args)
126
127
Note: See TracBrowser for help on using the repository browser.