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 | |
---|
16 | from pyutilib.plugin import * |
---|
17 | |
---|
18 | |
---|
19 | class 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 | |
---|
39 | class 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 | |
---|
50 | class 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 | |
---|
59 | class 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 | |
---|
75 | def 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 | |
---|
84 | class IPyomoSet(Interface): |
---|
85 | pass |
---|
86 | |
---|
87 | |
---|
88 | |
---|
89 | class 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 | |
---|
101 | class 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 | |
---|
121 | def 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 | |
---|