1#  ___________________________________________________________________________
2#
3#  Pyomo: Python Optimization Modeling Objects
4#  Copyright 2017 National Technology and Engineering Solutions of Sandia, LLC
5#  Under the terms of Contract DE-NA0003525 with National Technology and
6#  Engineering Solutions of Sandia, LLC, the U.S. Government retains certain
7#  rights in this software.
8#  This software is distributed under the 3-clause BSD License.
9#  ___________________________________________________________________________
10
11import logging
12
13from pyomo.core.base import (Transformation,
14                             TransformationFactory,
15                             Block,
16                             SortComponents)
17from pyomo.mpec.complementarity import Complementarity
18from pyomo.gdp import Disjunct
19
20logger = logging.getLogger('pyomo.core')
21
22
23#
24# This transformation reworks each Complementarity block to
25# setup a standard form.
26#
27@TransformationFactory.register('mpec.standard_form', doc="Standard reformulation of complementarity condition")
28class MPEC3_Transformation(Transformation):
29
30
31    def __init__(self):
32        super(MPEC3_Transformation, self).__init__()
33
34    def _apply_to(self, instance, **kwds):
35        #
36        # Iterate over the model finding Complementarity components
37        #
38        for complementarity in instance.component_objects(Complementarity, active=True,
39                                                          descend_into=(Block, Disjunct),
40                                                          sort=SortComponents.deterministic):
41            block = complementarity.parent_block()
42            for index in sorted(complementarity.keys()):
43                _data = complementarity[index]
44                if not _data.active:
45                    continue
46                _data.to_standard_form()
47                #
48            block.reclassify_component_type(complementarity, Block)
49