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

Revision 1994, 2.3 KB checked in by wehart, 4 years ago (diff)

Initial setup of the PyomoModel?._name_conmap data structure.
I've set this up to load data, but it currently does not correctly load
data for range constraints.

Misc rework of tests, segregating model/var/objective/constraint tests.

Added some tests for activation/deactivation of constraints. This
required some changes to the 'display()' methods in Pyomo, to
hide constraints that are deactivated.

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
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.active_components(Var)
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        Cons = model.active_components(Constraint)
63        for con in Cons.values():
64            for C in con._data:
65                con._data[C].label = self._name_fix( con._data[C].name )
66                model._name_conmap[ con._data[C].label ] = con._data[C]
67        return model
68           
Note: See TracBrowser for help on using the repository browser.