예제 #1
0
    def _check_skip_add(self, index, expr):
        _expr_type = expr.__class__
        if expr is None:
            raise ValueError(
                _rule_returned_none_error %
                (_get_indexed_component_data_name(self, index),))

        if expr is True:
            raise ValueError(
                "LogicalConstraint '%s' is always True."
                % (_get_indexed_component_data_name(self, index),))
        if expr is False:
            raise ValueError(
                "LogicalConstraint '%s' is always False."
                % (_get_indexed_component_data_name(self, index),))

        if _expr_type is tuple and len(expr) == 1:
            if expr is LogicalConstraint.Skip:
                # Note: LogicalConstraint.Feasible is Skip
                return None
            if expr is LogicalConstraint.Infeasible:
                raise ValueError(
                    "LogicalConstraint '%s' cannot be passed 'Infeasible' as a value."
                    % (_get_indexed_component_data_name(self, index),))

        return expr
예제 #2
0
    def _check_skip_add(self, index, expr, objdata=None):
        #
        # Convert deprecated expression values
        #
        if expr is None:
            raise ValueError(_rule_returned_none_error %
                             (_get_indexed_component_data_name(self, index), ))

        #
        # Ignore an 'empty' objective
        #
        if expr.__class__ is tuple:
            if expr == Objective.Skip:
                return None

        return expr
예제 #3
0
    def _check_skip_add(self, index, expr):
        _expr_type = expr.__class__
        #
        # Convert deprecated expression values
        #
        if _expr_type in _simple_constraint_rule_types:
            if expr is None:
                raise ValueError(
                    _rule_returned_none_error %
                    (_get_indexed_component_data_name(self, index),) )

            #
            # There are cases where a user thinks they are generating
            # a valid 2-sided inequality, but Python's internal
            # systems for handling chained inequalities is doing
            # something very different and resolving it to True /
            # False.  In this case, chainedInequality will be
            # non-None, but the expression will be a bool.  For
            # example, model.a < 1 > 0.
            #
            if EXPR.generate_relational_expression.\
                    chainedInequality is not None:

                buf = StringIO()
                EXPR.generate_relational_expression.\
                    chainedInequality.pprint(buf)
                #
                # We are about to raise an exception, so it's OK to
                # reset chainedInequality
                #
                EXPR.generate_relational_expression.\
                    chainedInequality = None
                raise ValueError(
                    "Invalid chained (2-sided) inequality detected. "
                    "The expression is resolving to %s instead of a "
                    "Pyomo Expression object. This can occur when "
                    "the middle term of a chained inequality is a "
                    "constant or immutable parameter, for example, "
                    "'model.a <= 1 >= 0'.  The proper form for "
                    "2-sided inequalities is '0 <= model.a <= 1'."
                    "\n\nError thrown for Constraint '%s'"
                    "\n\nUnresolved (dangling) inequality "
                    "expression: %s"
                    % (expr, _get_indexed_component_data_name(self,index), buf))
            else:
                raise ValueError(
                    "Invalid constraint expression. The constraint "
                    "expression resolved to a trivial Boolean (%s) "
                    "instead of a Pyomo object. Please modify your "
                    "rule to return Constraint.%s instead of %s."
                    "\n\nError thrown for Constraint '%s'"
                    % (expr,
                       expr and "Feasible" or "Infeasible",
                       expr,
                       _get_indexed_component_data_name(self,index)))

        #
        # Ignore an 'empty' constraint
        #
        if _expr_type is tuple and len(expr) == 1:
            if (expr == Constraint.Skip) or \
               (expr == Constraint.Feasible):
                return None
            if expr == Constraint.Infeasible:
                raise ValueError(
                    "Constraint '%s' is always infeasible"
                    % (_get_indexed_component_data_name(self,index),) )

        return expr
예제 #4
0
파일: constraint.py 프로젝트: Pyomo/pyomo
    def _check_skip_add(self, index, expr):
        _expr_type = expr.__class__
        #
        # Convert deprecated expression values
        #
        if _expr_type in _simple_constraint_rule_types:
            if expr is None:
                raise ValueError(
                    _rule_returned_none_error %
                    (_get_indexed_component_data_name(self, index),) )

            #
            # There are cases where a user thinks they are generating
            # a valid 2-sided inequality, but Python's internal
            # systems for handling chained inequalities is doing
            # something very different and resolving it to True /
            # False.  In this case, chainedInequality will be
            # non-None, but the expression will be a bool.  For
            # example, model.a < 1 > 0.
            #
            if logical_expr._using_chained_inequality and logical_expr._chainedInequality.prev is not None:

                buf = StringIO()
                logical_expr._chainedInequality.prev.pprint(buf)
                #
                # We are about to raise an exception, so it's OK to
                # reset chainedInequality
                #
                logical_expr._chainedInequality.prev = None
                raise ValueError(
                    "Invalid chained (2-sided) inequality detected. "
                    "The expression is resolving to %s instead of a "
                    "Pyomo Expression object. This can occur when "
                    "the middle term of a chained inequality is a "
                    "constant or immutable parameter, for example, "
                    "'model.a <= 1 >= 0'.  The proper form for "
                    "2-sided inequalities is '0 <= model.a <= 1'."
                    "\n\nError thrown for Constraint '%s'"
                    "\n\nUnresolved (dangling) inequality "
                    "expression: %s"
                    % (expr, _get_indexed_component_data_name(self,index), buf))
            else:
                raise ValueError(
                    "Invalid constraint expression. The constraint "
                    "expression resolved to a trivial Boolean (%s) "
                    "instead of a Pyomo object. Please modify your "
                    "rule to return Constraint.%s instead of %s."
                    "\n\nError thrown for Constraint '%s'"
                    % (expr,
                       expr and "Feasible" or "Infeasible",
                       expr,
                       _get_indexed_component_data_name(self,index)))

        #
        # Ignore an 'empty' constraint
        #
        if _expr_type is tuple and len(expr) == 1:
            if (expr == Constraint.Skip) or \
               (expr == Constraint.Feasible):
                return None
            if expr == Constraint.Infeasible:
                raise ValueError(
                    "Constraint '%s' is always infeasible"
                    % (_get_indexed_component_data_name(self,index),) )

        return expr