source: coopr.pyomo/trunk/coopr/pyomo/presolve/generic_varlabels.py @ 2201

Last change on this file since 2201 was 2201, checked in by wehart, 10 years ago

Update to Coopr to account for changes in PyUtilib? package names.

File size: 2.9 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, Var, Constraint, Objective
12import pyutilib.component.core
13
14ignore = set(['"', "'", '`'])
15remap = {}
16# Remap as underscores
17remap['-']='_'
18remap['_']='_'
19remap[' ']='_'
20remap['(']='['
21remap['{']='['
22remap[')']=']'
23remap['}']=']'
24# Remap as Q+ASCII-Dec
25remap['!']='_Q33_'
26remap['#']='_Q35_'
27remap['$']='_Q36_'
28remap['%']='_Q37_'
29remap['&']='_Q38_'
30remap['*']='_Q42_'
31remap['+']='_Q43_'
32#remap[',']='_Q44_'
33remap['.']='_Q46_'
34remap['/']='_Q47_'
35remap[';']='_Q59_'
36remap['<']='_Q60_'
37remap['=']='_Q61_'
38remap['>']='_Q62_'
39remap['?']='_Q63_'
40remap['@']='_Q63_'
41remap['\\']='_Q92_'
42remap['^']='_Q94_'
43remap['|']='_Q124_'
44remap['~']='_Q126_'
45
46class GenericVarLabelsPresolver(pyutilib.component.core.SingletonPlugin):
47    """
48    This plugin changes variables names to generate a label that
49    can be used in LP/MILP file formats.
50    """
51
52    pyutilib.component.core.implements(IPyomoPresolveAction)
53
54    def __init__(self, **kwds):
55        kwds['name'] = "generic_varlabels"
56        pyutilib.component.core.Plugin.__init__(self, **kwds)
57
58    def rank(self):
59        return 1000
60
61    def _name_fix(self, name):
62        global ignore
63        global remap
64        tmp=""
65        for c in name:
66            if c in remap:
67                tmp += remap[c]
68            elif not c in ignore:
69                tmp += c
70        return tmp
71
72    def presolve(self,model):
73        """
74        The main routine to perform the presolve
75        """
76        #
77        # This creates labels for all variables, not just the ones used
78        # in this instance.  This is useful when interrogating unused
79        # variables.
80        #
81        Vars = model.active_components(Var)
82        for var in Vars.values():
83            for V in var._varval.keys():
84                var._varval[V].label = self._name_fix( var._varval[V].name )
85                model._name_varmap[ var._varval[V].label ] = var._varval[V]
86        Cons = model.active_components(Constraint)
87        for con in Cons.values():
88            for C in con._data:
89                con._data[C].label = self._name_fix( con._data[C].name )
90                model._name_conmap[ con._data[C].label ] = con._data[C]
91        Objs = model.active_components(Objective)
92        for obj in Objs.values():
93            for O in obj._data:
94                obj._data[O].label = self._name_fix( obj._data[O].name )
95                model._name_objmap[ obj._data[O].label ] = obj._data[O]
96        return model
97           
Note: See TracBrowser for help on using the repository browser.