def arbitrary_partition(disjunction, P):
    """
    Returns a valid partition into P sets of the variables that appear in
    algebraic additively separable constraints in the Disjuncts in
    'disjunction'. Note that this method may return an invalid partition
    if the constraints are not additively separable!

    Arguments:
    ----------
    disjunction : A Disjunction object for which the variable partition will be
                 created.
    P : An int, the number of partitions
    """
    # collect variables
    v_set = ComponentSet()
    for disj in disjunction.disjuncts:
        v_set.update(
            get_vars_from_components(disj,
                                     Constraint,
                                     descend_into=Block,
                                     active=True))
    # assign them to partitions
    partitions = [ComponentSet() for i in range(P)]
    for i, v in enumerate(v_set):
        partitions[i % P].add(v)

    return partitions
Beispiel #2
0
    def _apply_to(self, instance, **kwds):
        if not instance.ctype in (Block, Disjunct):
            raise GDP_Error(
                "Transformation called on %s of type %s. 'instance'"
                " must be a ConcreteModel, Block, or Disjunct (in "
                "the case of nested disjunctions)." %
                (instance.name, instance.ctype))
        try:
            self._config = self.CONFIG(kwds.pop('options', {}))
            self._config.set_value(kwds)
            self._transformation_blocks = {}

            if not self._config.assume_fixed_vars_permanent:
                fixed_vars = ComponentMap()
                for v in get_vars_from_components(instance,
                                                  Constraint,
                                                  include_fixed=True,
                                                  active=True,
                                                  descend_into=(Block,
                                                                Disjunct)):
                    if v.fixed:
                        fixed_vars[v] = value(v)
                        v.fixed = False

            self._apply_to_impl(instance)

        finally:
            # restore fixed variables
            if not self._config.assume_fixed_vars_permanent:
                for v, val in fixed_vars.items():
                    v.fix(val)

            del self._config
            del self._transformation_blocks
Beispiel #3
0
def get_vars_from_component(block, ctype):
    """Determine all variables used in active components within a block.

    Parameters
    ----------
    block: Block
        The block to search for components.  This is a recursive
        generator and will descend into any active sub-Blocks as well.
    ctype:  class
        The component type (typically either :py:class:`Constraint` or
        :py:class:`Objective` to search for).

    """

    return get_vars_from_components(block, ctype, active=True,
                                    descend_into=True)