source: trunk/coopr/pyomo/presolve/simple_presolver.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: 2.7 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 FAST README.txt file.
9#  _________________________________________________________________________
10
11import pyutilib
12import pyutilib.plugin
13from coopr.pyomo.base import IPyomoPresolver, IPyomoPresolveAction
14
15
16class SimplePresolver(pyutilib.plugin.SingletonPlugin):
17    """
18    This plugin simply applies presolve actions in a fixed order
19    and then returns the modified model instance.
20    """
21
22    pyutilib.plugin.implements(IPyomoPresolver)
23
24    def __init__(self, **kwds):
25        kwds['name'] = "simple_presolver"
26        pyutilib.plugin.Plugin.__init__(self, **kwds)
27        self.actions = pyutilib.plugin.ExtensionPoint(IPyomoPresolveAction)
28        self.active_actions = set()
29        self.action_rank = {}
30        self.initialized = False
31
32    def _initialize(self):
33        if self.initialized:
34            return
35        self.initialized = True
36        for action in self.actions():
37            self.activate_action(action.name)
38
39    def get_actions(self):
40        """Return a list of presolve actions, with their ranks"""
41        self._initialize()
42        ans = []
43        for action in self._order_actions():
44            ans.append( (action, self.action_rank[action]) )
45        return ans
46
47    def activate_action(self, action, rank=None):
48        """Activate an action"""
49        self._initialize()
50        tmp = self.actions.service(action)
51        self.active_actions.add(action)
52        if rank is None:
53            rank = tmp.rank()
54        self.action_rank[action] = rank
55
56    def deactivate_action(self, action):
57        """Deactivate an action"""
58        self._initialize()
59        if action in self.active_actions:
60            self.active_actions.remove(action)
61            del self.action_rank[action]
62
63    def presolve(self,model):
64        """
65        Apply the presolve actions to this instance, and return the revised instance.
66        """
67        self._initialize()
68        for action in self._order_actions():
69            model = self.actions.service(action).presolve(model)
70        return model
71
72    def _order_actions(self):
73        actions = list(self.active_actions)
74        ranks = []
75        for item in actions:
76            ranks.append(self.action_rank[item])
77        index = pyutilib.sort_index(ranks)
78        sorted=[]
79        for i in index:
80            sorted.append( actions[i] )
81        return sorted
82           
Note: See TracBrowser for help on using the repository browser.