source: coopr.pyomo/trunk/coopr/pyomo/transform/relax_integrality.py @ 2097

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

Updating Model statistics after a transformation.

File size: 2.3 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.plugin.core
11from coopr.pyomo.base import IModelTransformation
12from coopr.pyomo.base import Var
13from coopr.pyomo import BooleanSet, IntegerSet, RangeSet, Reals
14
15
16class RelaxIntegrality(pyutilib.plugin.core.SingletonPlugin):
17    """
18    This plugin relaxes integrality in a Pyomo model.
19    """
20
21    pyutilib.plugin.core.implements(IModelTransformation)
22
23    def __init__(self, **kwds):
24        kwds['name'] = "relax_integrality"
25        pyutilib.plugin.core.Plugin.__init__(self, **kwds)
26
27    def apply(self, model, **kwds):
28        #
29        # Clone the model
30        #
31        M = model.clone()
32        #
33        # Iterate over all variables, replacing the domain with a real-valued domain
34        # and setting appropriate bounds.
35        #
36        # Note: this technique is not extensible.  If a user creates a new integer variable
37        # class then we'd need to add it here (unless it was a subset of one of the sets we
38        # detect here.  Alternatively, we could make the variables responsible for managing
39        # the generation of a relaxed variable.  But that would pollute the variable objects
40        # with logic about generating a specific relaxation.  That's probably a worse alternative.
41        #
42        comp = M.components(Var)
43        for var in comp.values():
44            if isinstance(var.domain, BooleanSet):
45                var.domain=Reals
46                var._bounds = (0.0,1.0)
47            elif isinstance(var.domain, IntegerSet):
48                var.domain=Reals
49                var._bounds = var.domain._bounds
50            elif isinstance(var.domain, RangeSet):
51                var.domain=Reals
52                var._bounds = var.domain._bounds
53        M.statistics.number_of_binary_variables = 0
54        M.statistics.number_of_integer_variables = 0
55        M.statistics.number_of_continuous_variables = len(M._var)
56        return M
57
Note: See TracBrowser for help on using the repository browser.