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