source: pyomo/trunk/pyomo/core/plugins/transform/eliminate_fixed_vars.py @ 9475

Last change on this file since 9475 was 9475, checked in by wehart, 4 years ago

Updating the version #.

Renaming transformations to 'base.*' if they didn't have a specific
naming. NOTE: these are in the pyomo.core package ... which seems
odd. But we agreed that 'core' provides a connotation that we don't
intend.

Adding some documentation changes for 'pyomo help -t'.

File size: 3.2 KB
Line 
1#  _________________________________________________________________________
2#
3#  Pyomo: Python Optimization Modeling Objects
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#  _________________________________________________________________________
9
10from pyomo.util.plugin import alias
11from pyomo.core import Constraint, Objective, NumericConstant
12from pyomo.core.base.expr import _ExpressionBase
13from pyomo.core.base.var import Var, _VarData
14from pyomo.core.base.util import xsequence
15from pyomo.core.plugins.transform.hierarchy import IsomorphicTransformation
16
17
18class EliminateFixedVars(IsomorphicTransformation):
19    """
20    Create an equivalent model that omits all fixed variables.
21    """
22
23    alias('base.remove_fixed_vars', doc="Create an equivalent model that omits all fixed variables.")
24
25    def __init__(self, **kwds):
26        kwds['name'] = "eliminate_fixed_vars"
27        super(EqualityTransform, self).__init__(**kwds)
28
29    def apply(self, model, **kwds):
30        #
31        # Clone the model
32        #
33        M = model.clone()
34        #
35        # Iterate over the expressions in all objectives and constraints, replacing fixed
36        # variables with their associated constants.
37        #
38        for ctype in [Objective, Constraint]:
39            for obj in M.components(Objective).values():
40                for name in obj:
41                    if not obj[name].expr is None:
42                        obj[name].expr = self._fix_vars(obj[name].expr, model)
43        #
44        # Iterate over variables, omitting those that have fixed values
45        #
46        ctr = 0
47        for i in xsequence(M.nvariables()):
48            var = M.variable(i)
49            del M._var[ i-1 ]
50            if var.fixed:
51                if var.is_binary():
52                    M.statistics.number_of_binary_variables -= 1
53                elif var.is_integer():
54                    M.statistics.number_of_integer_variables -= 1
55                elif var.is_continuous():
56                    M.statistics.number_of_continuous_variables -= 1
57                M.statistics.number_of_variables -= 1
58                del M._label_var_map[ var.label ]
59                del var.component()._data[ var.index ]
60            else:
61                M._var[ ctr ] = var
62                var._old_id = var.id
63                var.id = ctr
64                ctr += 1
65        return M
66
67    def _fix_vars(self, expr, model):
68        """ Walk through the S-expression, fixing variables. """
69        if expr._args is None:
70            return expr
71        _args = []
72        for i in range(len(expr._args)):
73            if isinstance(expr._args[i],_ExpressionBase):
74                _args.append( self._fix_vars(expr._args[i], model) )
75            elif (isinstance(expr._args[i],Var) or isinstance(expr._args[i],_VarData)) and expr._args[i].fixed:
76                if expr._args[i].value != 0.0:
77                    _args.append( NumericConstant(None,None,expr._args[i].value) )
78            else:
79                _args.append( expr._args[i] )
80        expr._args = _args
81        return expr
Note: See TracBrowser for help on using the repository browser.