source: trunk/coopr/pyomo/presolve/simple_presolver.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.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.misc
12import pyutilib.plugin.core
13from coopr.pyomo.base import IPyomoPresolver, IPyomoPresolveAction
14
15
16class SimplePresolver(pyutilib.plugin.core.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.core.implements(IPyomoPresolver)
23
24    def __init__(self, **kwds):
25        kwds['name'] = "simple_presolver"
26        pyutilib.plugin.core.Plugin.__init__(self, **kwds)
27        self.actions = pyutilib.plugin.core.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.misc.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.