source: trunk/coopr/pyomo/presolve/generic_varlabels.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.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 FAST README.txt file.
9#  _________________________________________________________________________
10
11from coopr.pyomo.base import IPyomoPresolver, IPyomoPresolveAction, _VarBase
12import pyutilib.plugin.core
13
14ignore = set(['"', ' ', "'", '`'])
15#underscore = set(['[', ']', "(", ')', ',', '-'])
16underscore = set(['-'])
17
18class GenericVarLabelsPresolver(pyutilib.plugin.core.SingletonPlugin):
19    """
20    This plugin changes variables names to generate a label that
21    can be used in LP/MILP file formats.
22    """
23
24    pyutilib.plugin.core.implements(IPyomoPresolveAction)
25
26    def __init__(self, **kwds):
27        kwds['name'] = "generic_varlabels"
28        pyutilib.plugin.core.Plugin.__init__(self, **kwds)
29
30    def rank(self):
31        return 1000
32
33    def _name_fix(self, name):
34        global ignore
35        global underscore
36        tmp=""
37        for c in name:
38            if c in underscore:
39                tmp += '_'
40            elif c is '(':
41                tmp += '['
42            elif c is ')':
43                tmp += ']'
44            elif not c in ignore:
45                tmp += c
46        return tmp
47
48    def presolve(self,model):
49        """
50        The main routine to perform the presolve
51        """
52        #
53        # This creates labels for all variables, not just the ones used
54        # in this instance.  This is useful when interrogating unused
55        # variables.
56        #
57        Vars = model._component[_VarBase]
58        for var in Vars.values():
59            for V in var._varval.keys():
60                var._varval[V].label = self._name_fix( var._varval[V].name )
61                model._name_varmap[ var._varval[V].label ] = var._varval[V]
62        for id in model._con:
63            model._con[id].label = self._name_fix( model._con[id].name )
64        return model
65           
Note: See TracBrowser for help on using the repository browser.