示例#1
0
 def lower(self, lb):
     if (lb is not None) and \
        (not is_numeric_data(lb)):
         raise ValueError(
                 "Variable lower bounds must be numbers or "
                 "expressions restricted to numeric data.")
     self._lb = lb
示例#2
0
 def upper(self, ub):
     if (ub is not None) and \
        (not is_numeric_data(ub)):
         raise ValueError(
                 "Variable upper bounds must be numbers or "
                 "expressions restricted to numeric data.")
     self._ub = ub
示例#3
0
文件: variable.py 项目: Pyomo/pyomo
 def lb(self, lb):
     if (lb is not None) and \
        (not is_numeric_data(lb)):
         raise ValueError(
                 "Variable lower bounds must be numbers or "
                 "expressions restricted to numeric data.")
     self._lb = lb
示例#4
0
文件: variable.py 项目: Pyomo/pyomo
 def ub(self, ub):
     if (ub is not None) and \
        (not is_numeric_data(ub)):
         raise ValueError(
                 "Variable upper bounds must be numbers or "
                 "expressions restricted to numeric data.")
     self._ub = ub
示例#5
0
 def test_unknownNumericType(self):
     ref = MyBogusNumericType(42)
     self.assertTrue(is_numeric_data(ref))
     from pyomo.core.base.numvalue import native_numeric_types, native_types
     self.assertIn(MyBogusNumericType, native_numeric_types)
     self.assertIn(MyBogusNumericType, native_types)
     native_numeric_types.remove(MyBogusNumericType)
     native_types.remove(MyBogusNumericType)
示例#6
0
 def test_unknownNumericType(self):
     ref = MyBogusNumericType(42)
     self.assertTrue(is_numeric_data(ref))
     from pyomo.core.base.numvalue import native_numeric_types, native_types
     self.assertIn(MyBogusNumericType, native_numeric_types)
     self.assertIn(MyBogusNumericType, native_types)
     native_numeric_types.remove(MyBogusNumericType)
     native_types.remove(MyBogusNumericType)
示例#7
0
 def lb(self, lb):
     if self.equality:
         raise ValueError("The lb property can not be set "
                          "when the equality property is True.")
     if (lb is not None) and \
        (not is_numeric_data(lb)):
         raise TypeError("Constraint lower bounds must be "
                         "expressions restricted to numeric data.")
     self._lb = lb
示例#8
0
 def ub(self, ub):
     if self.equality:
         raise ValueError("The ub property can not be set "
                          "when the equality property is True.")
     if (ub is not None) and \
        (not is_numeric_data(ub)):
         raise TypeError("Constraint upper bounds must be "
                         "expressions restricted to numeric data.")
     self._ub = ub
示例#9
0
文件: constraint.py 项目: Pyomo/pyomo
 def ub(self, ub):
     if self.equality:
         raise ValueError(
             "The ub property can not be set "
             "when the equality property is True.")
     if (ub is not None) and \
        (not is_numeric_data(ub)):
         raise TypeError(
                 "Constraint upper bounds must be "
                 "expressions restricted to numeric data.")
     self._ub = ub
示例#10
0
文件: constraint.py 项目: Pyomo/pyomo
 def lb(self, lb):
     if self.equality:
         raise ValueError(
             "The lb property can not be set "
             "when the equality property is True.")
     if (lb is not None) and \
        (not is_numeric_data(lb)):
         raise TypeError(
                 "Constraint lower bounds must be "
                 "expressions restricted to numeric data.")
     self._lb = lb
示例#11
0
文件: conic.py 项目: mdruger/pyomo
 def __init__(self, x, r1, r2, alpha):
     super(dual_power, self).__init__()
     self._x = tuple(x)
     self._r1 = r1
     self._r2 = r2
     self._alpha = alpha
     assert all(isinstance(xi, IVariable) for xi in self._x)
     assert isinstance(self._r1, IVariable)
     assert isinstance(self._r2, IVariable)
     if not is_numeric_data(self._alpha):
         raise TypeError("The type of the alpha parameter of a conic "
                         "constraint is restricted numeric data or "
                         "objects that store numeric data.")
示例#12
0
 def rhs(self, rhs):
     if rhs is None:
         # None has a different meaning depending on the
         # context (lb or ub), so there is no way to
         # interpret this
         raise ValueError("Constraint right-hand side can not "
                          "be assigned a value of None.")
     elif not is_numeric_data(rhs):
         raise TypeError("Constraint right-hand side must be numbers "
                         "or expressions restricted to data.")
     self._lb = rhs
     self._ub = rhs
     self._equality = True
示例#13
0
 def __call__(self, exception=True):
     if self._fn is None:
         return None
     try:
         val = self._fn()
     except Exception as e:
         if exception:
             raise e
         else:
             return None
     # this exception should never be masked
     if not is_numeric_data(val):
         raise TypeError("Functional value is not numeric data")
     return val
示例#14
0
文件: constraint.py 项目: Pyomo/pyomo
 def rhs(self, rhs):
     if rhs is None:
         # None has a different meaning depending on the
         # context (lb or ub), so there is no way to
         # interpret this
         raise ValueError(
             "Constraint right-hand side can not "
             "be assigned a value of None.")
     elif not is_numeric_data(rhs):
         raise TypeError(
                 "Constraint right-hand side must be numbers "
                 "or expressions restricted to data.")
     self._lb = rhs
     self._ub = rhs
     self._equality = True
示例#15
0
 def __init__(self, r1, r2, x, alpha):
     super(dual_power, self).__init__()
     self._r1 = r1
     self._r2 = r2
     self._x = tuple(x)
     self._alpha = alpha
     assert isinstance(self._r1, IVariable)
     assert isinstance(self._r2, IVariable)
     assert all(isinstance(xi, IVariable)
                for xi in self._x)
     if not is_numeric_data(self._alpha):
         raise TypeError(
             "The type of the alpha parameter of a conic "
             "constraint is restricted numeric data or "
             "objects that store numeric data.")
示例#16
0
文件: conic.py 项目: mdruger/pyomo
def _build_linking_constraints(v, v_aux):
    assert len(v) == len(v_aux)
    c_aux = []
    for vi, vi_aux in zip(v, v_aux):
        assert vi_aux.ctype is IVariable
        if is_numeric_data(vi):
            c_aux.append(
                linear_constraint(variables=(vi_aux, ),
                                  coefficients=(1, ),
                                  rhs=vi))
        elif isinstance(vi, IVariable):
            c_aux.append(
                linear_constraint(variables=(vi_aux, vi),
                                  coefficients=(1, -1),
                                  rhs=0))
        else:
            c_aux.append(constraint(body=vi_aux - vi, rhs=0))
    return constraint_tuple(c_aux)
示例#17
0
文件: sos.py 项目: Pyomo/pyomo
    def __init__(self, variables, weights=None, level=1):
        self._parent = None
        self._storage_key = None
        self._active = True
        self._variables = tuple(variables)
        self._weights = None
        self._level = level
        if weights is None:
            self._weights = tuple(range(1,len(self._variables)+1))
        else:
            self._weights = tuple(weights)
            for w in self._weights:
                if not is_numeric_data(w):
                    raise ValueError(
                        "Weights for Special Ordered Sets must be "
                        "expressions restricted to numeric data")

        assert len(self._variables) == len(self._weights)
        assert self._level >= 1
示例#18
0
    def __init__(self, variables, weights=None, level=1):
        self._parent = None
        self._storage_key = None
        self._active = True
        self._variables = tuple(variables)
        self._weights = None
        self._level = level
        if weights is None:
            self._weights = tuple(range(1, len(self._variables) + 1))
        else:
            self._weights = tuple(weights)
            for w in self._weights:
                if not is_numeric_data(w):
                    raise ValueError(
                        "Weights for Special Ordered Sets must be "
                        "expressions restricted to numeric data")

        assert len(self._variables) == len(self._weights)
        assert self._level >= 1
示例#19
0
def _build_linking_constraints(v, v_aux):
    assert len(v) == len(v_aux)
    c_aux = []
    for vi, vi_aux in zip(v, v_aux):
        assert vi_aux.ctype is IVariable
        if vi is None:
            continue
        elif is_numeric_data(vi):
            c_aux.append(
                linear_constraint(variables=(vi_aux,),
                                  coefficients=(1,),
                                  rhs=vi))
        elif isinstance(vi, IVariable):
            c_aux.append(
                linear_constraint(variables=(vi_aux, vi),
                                  coefficients=(1, -1),
                                  rhs=0))
        else:
            c_aux.append(
                constraint(body=vi_aux - vi,
                           rhs=0))
    return constraint_tuple(c_aux)
示例#20
0
 def test_NumericValue(self):
     self.assertEqual(is_numeric_data(NumericConstant(1.0)), True)
示例#21
0
 def test_int(self):
     self.assertEqual(is_numeric_data(0), True)
示例#22
0
 def test_float(self):
     self.assertEqual(is_numeric_data(0.0), True)
示例#23
0
 def test_string(self):
     self.assertEqual(is_numeric_data("a"), False)
     self.assertEqual(is_numeric_data(b"a"), False)
示例#24
0
    def expr(self, expr):

        self._equality = False
        if expr is None:
            self.body = None
            self.lb = None
            self.ub = None
            return

        _expr_type = expr.__class__
        if _expr_type is tuple:
            #
            # Form equality expression
            #
            if len(expr) == 2:
                arg0 = expr[0]
                arg1 = expr[1]
                # assigning to the rhs property
                # will set the equality flag to True
                if not is_potentially_variable(arg1):
                    self.rhs = arg1
                    self.body = arg0
                elif not is_potentially_variable(arg0):
                    self.rhs = arg0
                    self.body = arg1
                else:
                    self.rhs = ZeroConstant
                    self.body = arg0
                    self.body -= arg1

            #
            # Form inequality expression
            #
            elif len(expr) == 3:
                arg0 = expr[0]
                if arg0 is not None:
                    if not is_numeric_data(arg0):
                        raise ValueError(
                            "Constraint '%s' found a 3-tuple (lower,"
                            " expression, upper) but the lower "
                            "value was not numeric data or an "
                            "expression restricted to storage of "
                            "numeric data."
                            % (self.name))

                arg1 = expr[1]
                if arg1 is not None:
                    arg1 = as_numeric(arg1)

                arg2 = expr[2]
                if arg2 is not None:
                    if not is_numeric_data(arg2):
                        raise ValueError(
                            "Constraint '%s' found a 3-tuple (lower,"
                            " expression, upper) but the upper "
                            "value was not numeric data or an "
                            "expression restricted to storage of "
                            "numeric data."
                            % (self.name))

                self.lb = arg0
                self.body  = arg1
                self.ub = arg2
            else:
                raise ValueError(
                    "Constraint '%s' assigned a tuple "
                    "of length %d. Expecting a tuple of "
                    "length 2 or 3:\n"
                    "Equality:   (body, rhs)\n"
                    "Inequality: (lb, body, ub)"
                    % (self.name, len(expr)))

            relational_expr = False
        else:
            try:
                relational_expr = expr.is_relational()
                if not relational_expr:
                    raise ValueError(
                        "Constraint '%s' does not have a proper "
                        "value. Found '%s'\nExpecting a tuple or "
                        "equation. Examples:"
                        "\n   sum_product(model.costs) == model.income"
                        "\n   (0, model.price[item], 50)"
                        % (self.name, str(expr)))
            except AttributeError:
                msg = ("Constraint '%s' does not have a proper "
                       "value. Found '%s'\nExpecting a tuple or "
                       "equation. Examples:"
                       "\n   sum_product(model.costs) == model.income"
                       "\n   (0, model.price[item], 50)"
                       % (self.name, str(expr)))
                if type(expr) is bool:
                    msg += ("\nNote: constant Boolean expressions "
                            "are not valid constraint expressions. "
                            "Some apparently non-constant compound "
                            "inequalities (e.g. 'expr >= 0 <= 1') "
                            "can return boolean values; the proper "
                            "form for compound inequalities is "
                            "always 'lb <= expr <= ub'.")
                raise ValueError(msg)
        #
        # Special check for chainedInequality errors like "if var <
        # 1:" within rules.  Catching them here allows us to provide
        # the user with better (and more immediate) debugging
        # information.  We don't want to check earlier because we
        # want to provide a specific debugging message if the
        # construction rule returned True/False; for example, if the
        # user did ( var < 1 > 0 ) (which also results in a non-None
        # chainedInequality value)
        #
        if logical_expr._using_chained_inequality and \
           (logical_expr._chainedInequality.prev is not None):
            raise TypeError(logical_expr._chainedInequality.error_message())
        #
        # Process relational expressions
        # (i.e. explicit '==', '<', and '<=')
        #
        if relational_expr:
            if _expr_type is logical_expr.EqualityExpression:
                # assigning to the rhs property
                # will set the equality flag to True
                if not is_potentially_variable(expr.arg(1)):
                    self.rhs = expr.arg(1)
                    self.body = expr.arg(0)
                elif not is_potentially_variable(expr.arg(0)):
                    self.rhs = expr.arg(0)
                    self.body = expr.arg(1)
                else:
                    self.rhs = ZeroConstant
                    self.body = expr.arg(0)
                    self.body -= expr.arg(1)

            elif _expr_type is logical_expr.InequalityExpression:
                if expr._strict:
                    raise ValueError(
                        "Constraint '%s' encountered a strict "
                        "inequality expression ('>' or '<'). All"
                        " constraints must be formulated using "
                        "using '<=', '>=', or '=='."
                        % (self.name))
                if not is_potentially_variable(expr.arg(1)):
                    self.lb = None
                    self.body = expr.arg(0)
                    self.ub = expr.arg(1)
                elif not is_potentially_variable(expr.arg(0)):
                    self.lb = expr.arg(0)
                    self.body = expr.arg(1)
                    self.ub = None
                else:
                    self.lb = None
                    self.body  = expr.arg(0)
                    self.body -= expr.arg(1)
                    self.ub = ZeroConstant

            else:   # RangedExpression
                if any(expr._strict):
                    raise ValueError(
                        "Constraint '%s' encountered a strict "
                        "inequality expression ('>' or '<'). All"
                        " constraints must be formulated using "
                        "using '<=', '>=', or '=='."
                        % (self.name))

                if not is_numeric_data(expr.arg(0)):
                    raise ValueError(
                        "Constraint '%s' found a double-sided "
                        "inequality expression (lower <= "
                        "expression <= upper) but the lower "
                        "bound was not numeric data or an "
                        "expression restricted to storage of "
                        "numeric data."
                        % (self.name))
                if not is_numeric_data(expr.arg(2)):
                    raise ValueError(
                        "Constraint '%s' found a double-sided "\
                        "inequality expression (lower <= "
                        "expression <= upper) but the upper "
                        "bound was not numeric data or an "
                        "expression restricted to storage of "
                        "numeric data."
                        % (self.name))

                self.lb = expr.arg(0)
                self.body  = expr.arg(1)
                self.ub = expr.arg(2)

        #
        # Error check, to ensure that we don't have an equality
        # constraint with 'infinite' RHS
        #
        assert not (self.equality and (self.lb is None))
        assert (not self.equality) or (self.lb is self.ub)
示例#25
0
 def test_int(self):
     self.assertEqual(is_numeric_data(0), True)
示例#26
0
 def test_error(self):
     class A(object): pass
     val = A()
     self.assertEqual(False, is_numeric_data(val))
示例#27
0
 def expr(self, expr):
     if (expr is not None) and \
        (not is_numeric_data(expr)):
         raise ValueError("Expression is not restricted to "
                          "numeric data.")
     self._expr = expr
示例#28
0
文件: constraint.py 项目: Pyomo/pyomo
    def expr(self, expr):

        self._equality = False
        if expr is None:
            self.body = None
            self.lb = None
            self.ub = None
            return

        _expr_type = expr.__class__
        if _expr_type is tuple:
            #
            # Form equality expression
            #
            if len(expr) == 2:
                arg0 = expr[0]
                arg1 = expr[1]
                # assigning to the rhs property
                # will set the equality flag to True
                if not is_potentially_variable(arg1):
                    self.rhs = arg1
                    self.body = arg0
                elif not is_potentially_variable(arg0):
                    self.rhs = arg0
                    self.body = arg1
                else:
                    self.rhs = ZeroConstant
                    self.body = arg0
                    self.body -= arg1

            #
            # Form inequality expression
            #
            elif len(expr) == 3:
                arg0 = expr[0]
                if arg0 is not None:
                    if not is_numeric_data(arg0):
                        raise ValueError(
                            "Constraint '%s' found a 3-tuple (lower,"
                            " expression, upper) but the lower "
                            "value was not numeric data or an "
                            "expression restricted to storage of "
                            "numeric data."
                            % (self.name))

                arg1 = expr[1]
                if arg1 is not None:
                    arg1 = as_numeric(arg1)

                arg2 = expr[2]
                if arg2 is not None:
                    if not is_numeric_data(arg2):
                        raise ValueError(
                            "Constraint '%s' found a 3-tuple (lower,"
                            " expression, upper) but the upper "
                            "value was not numeric data or an "
                            "expression restricted to storage of "
                            "numeric data."
                            % (self.name))

                self.lb = arg0
                self.body  = arg1
                self.ub = arg2
            else:
                raise ValueError(
                    "Constraint '%s' assigned a tuple "
                    "of length %d. Expecting a tuple of "
                    "length 2 or 3:\n"
                    "Equality:   (body, rhs)\n"
                    "Inequality: (lb, body, ub)"
                    % (self.name, len(expr)))

            relational_expr = False
        else:
            try:
                relational_expr = expr.is_relational()
                if not relational_expr:
                    raise ValueError(
                        "Constraint '%s' does not have a proper "
                        "value. Found '%s'\nExpecting a tuple or "
                        "equation. Examples:"
                        "\n   sum_product(model.costs) == model.income"
                        "\n   (0, model.price[item], 50)"
                        % (self.name, str(expr)))
            except AttributeError:
                msg = ("Constraint '%s' does not have a proper "
                       "value. Found '%s'\nExpecting a tuple or "
                       "equation. Examples:"
                       "\n   sum_product(model.costs) == model.income"
                       "\n   (0, model.price[item], 50)"
                       % (self.name, str(expr)))
                if type(expr) is bool:
                    msg += ("\nNote: constant Boolean expressions "
                            "are not valid constraint expressions. "
                            "Some apparently non-constant compound "
                            "inequalities (e.g. 'expr >= 0 <= 1') "
                            "can return boolean values; the proper "
                            "form for compound inequalities is "
                            "always 'lb <= expr <= ub'.")
                raise ValueError(msg)
        #
        # Special check for chainedInequality errors like "if var <
        # 1:" within rules.  Catching them here allows us to provide
        # the user with better (and more immediate) debugging
        # information.  We don't want to check earlier because we
        # want to provide a specific debugging message if the
        # construction rule returned True/False; for example, if the
        # user did ( var < 1 > 0 ) (which also results in a non-None
        # chainedInequality value)
        #
        if logical_expr._using_chained_inequality and \
           (logical_expr._chainedInequality.prev is not None):
            raise TypeError(logical_expr._chainedInequality.error_message())
        #
        # Process relational expressions
        # (i.e. explicit '==', '<', and '<=')
        #
        if relational_expr:
            if _expr_type is logical_expr.EqualityExpression:
                # assigning to the rhs property
                # will set the equality flag to True
                if not is_potentially_variable(expr.arg(1)):
                    self.rhs = expr.arg(1)
                    self.body = expr.arg(0)
                elif not is_potentially_variable(expr.arg(0)):
                    self.rhs = expr.arg(0)
                    self.body = expr.arg(1)
                else:
                    self.rhs = ZeroConstant
                    self.body = expr.arg(0)
                    self.body -= expr.arg(1)

            elif _expr_type is logical_expr.InequalityExpression:
                if expr._strict:
                    raise ValueError(
                        "Constraint '%s' encountered a strict "
                        "inequality expression ('>' or '<'). All"
                        " constraints must be formulated using "
                        "using '<=', '>=', or '=='."
                        % (self.name))
                if not is_potentially_variable(expr.arg(1)):
                    self.lb = None
                    self.body = expr.arg(0)
                    self.ub = expr.arg(1)
                elif not is_potentially_variable(expr.arg(0)):
                    self.lb = expr.arg(0)
                    self.body = expr.arg(1)
                    self.ub = None
                else:
                    self.lb = None
                    self.body  = expr.arg(0)
                    self.body -= expr.arg(1)
                    self.ub = ZeroConstant

            else:   # RangedExpression
                if any(expr._strict):
                    raise ValueError(
                        "Constraint '%s' encountered a strict "
                        "inequality expression ('>' or '<'). All"
                        " constraints must be formulated using "
                        "using '<=', '>=', or '=='."
                        % (self.name))

                if not is_numeric_data(expr.arg(0)):
                    raise ValueError(
                        "Constraint '%s' found a double-sided "
                        "inequality expression (lower <= "
                        "expression <= upper) but the lower "
                        "bound was not numeric data or an "
                        "expression restricted to storage of "
                        "numeric data."
                        % (self.name))
                if not is_numeric_data(expr.arg(2)):
                    raise ValueError(
                        "Constraint '%s' found a double-sided "\
                        "inequality expression (lower <= "
                        "expression <= upper) but the upper "
                        "bound was not numeric data or an "
                        "expression restricted to storage of "
                        "numeric data."
                        % (self.name))

                self.lb = expr.arg(0)
                self.body  = expr.arg(1)
                self.ub = expr.arg(2)

        #
        # Error check, to ensure that we don't have an equality
        # constraint with 'infinite' RHS
        #
        assert not (self.equality and (self.lb is None))
        assert (not self.equality) or (self.lb is self.ub)
示例#29
0
文件: expression.py 项目: Pyomo/pyomo
 def expr(self, expr):
     if (expr is not None) and \
        (not is_numeric_data(expr)):
         raise ValueError("Expression is not restricted to "
                          "numeric data.")
     self._expr = expr
示例#30
0
    def expr(self, expr):

        self._equality = False
        if expr is None:
            self.body = None
            self.lb = None
            self.ub = None
            return

        _expr_type = expr.__class__
        if _expr_type is tuple:
            #
            # Form equality expression
            #
            if len(expr) == 2:
                arg0 = expr[0]
                arg1 = expr[1]
                # assigning to the rhs property
                # will set the equality flag to True
                if not is_potentially_variable(arg1):
                    self.rhs = arg1
                    self.body = arg0
                elif not is_potentially_variable(arg0):
                    self.rhs = arg0
                    self.body = arg1
                else:
                    self.rhs = ZeroConstant
                    self.body = arg0
                    self.body -= arg1

            #
            # Form inequality expression
            #
            elif len(expr) == 3:
                arg0 = expr[0]
                if arg0 is not None:
                    if not is_numeric_data(arg0):
                        raise ValueError(
                            "Constraint '%s' found a 3-tuple (lower,"
                            " expression, upper) but the lower "
                            "value was not numeric data or an "
                            "expression restricted to storage of "
                            "numeric data." % (self.name))

                arg1 = expr[1]
                if arg1 is not None:
                    arg1 = as_numeric(arg1)

                arg2 = expr[2]
                if arg2 is not None:
                    if not is_numeric_data(arg2):
                        raise ValueError(
                            "Constraint '%s' found a 3-tuple (lower,"
                            " expression, upper) but the upper "
                            "value was not numeric data or an "
                            "expression restricted to storage of "
                            "numeric data." % (self.name))
                elif arg1 is not None and is_numeric_data(arg1):
                    # Special case (reflect behavior of AML): if the
                    # upper bound is None and the "body" is only data,
                    # then shift the body to the UB and the LB to the
                    # body
                    arg0, arg1, arg2 = arg2, arg0, arg1

                self.lb = arg0
                self.body = arg1
                self.ub = arg2
            else:
                raise ValueError("Constraint '%s' assigned a tuple "
                                 "of length %d. Expecting a tuple of "
                                 "length 2 or 3:\n"
                                 "Equality:   (body, rhs)\n"
                                 "Inequality: (lb, body, ub)" %
                                 (self.name, len(expr)))

            relational_expr = False
        else:
            try:
                relational_expr = expr.is_relational()
                if not relational_expr:
                    raise ValueError(
                        "Constraint '%s' does not have a proper "
                        "value. Found '%s'\nExpecting a tuple or "
                        "equation. Examples:"
                        "\n   sum_product(model.costs) == model.income"
                        "\n   (0, model.price[item], 50)" %
                        (self.name, str(expr)))
            except AttributeError:
                msg = ("Constraint '%s' does not have a proper "
                       "value. Found '%s'\nExpecting a tuple or "
                       "equation. Examples:"
                       "\n   sum_product(model.costs) == model.income"
                       "\n   (0, model.price[item], 50)" %
                       (self.name, str(expr)))
                if type(expr) is bool:
                    msg += ("\nNote: constant Boolean expressions "
                            "are not valid constraint expressions. "
                            "Some apparently non-constant compound "
                            "inequalities (e.g. 'expr >= 0 <= 1') "
                            "can return boolean values; the proper "
                            "form for compound inequalities is "
                            "always 'lb <= expr <= ub'.")
                raise ValueError(msg)

        #
        # Process relational expressions
        # (i.e. explicit '==', '<', and '<=')
        #
        if relational_expr:
            if _expr_type is logical_expr.EqualityExpression:
                # assigning to the rhs property
                # will set the equality flag to True
                if not is_potentially_variable(expr.arg(1)):
                    self.rhs = expr.arg(1)
                    self.body = expr.arg(0)
                elif not is_potentially_variable(expr.arg(0)):
                    self.rhs = expr.arg(0)
                    self.body = expr.arg(1)
                else:
                    self.rhs = ZeroConstant
                    self.body = expr.arg(0)
                    self.body -= expr.arg(1)

            elif _expr_type is logical_expr.InequalityExpression:
                if expr._strict:
                    raise ValueError("Constraint '%s' encountered a strict "
                                     "inequality expression ('>' or '<'). All"
                                     " constraints must be formulated using "
                                     "using '<=', '>=', or '=='." %
                                     (self.name))
                if not is_potentially_variable(expr.arg(1)):
                    self.lb = None
                    self.body = expr.arg(0)
                    self.ub = expr.arg(1)
                elif not is_potentially_variable(expr.arg(0)):
                    self.lb = expr.arg(0)
                    self.body = expr.arg(1)
                    self.ub = None
                else:
                    self.lb = None
                    self.body = expr.arg(0)
                    self.body -= expr.arg(1)
                    self.ub = ZeroConstant

            else:  # RangedExpression
                if any(expr._strict):
                    raise ValueError("Constraint '%s' encountered a strict "
                                     "inequality expression ('>' or '<'). All"
                                     " constraints must be formulated using "
                                     "using '<=', '>=', or '=='." %
                                     (self.name))

                if not is_numeric_data(expr.arg(0)):
                    raise ValueError("Constraint '%s' found a double-sided "
                                     "inequality expression (lower <= "
                                     "expression <= upper) but the lower "
                                     "bound was not numeric data or an "
                                     "expression restricted to storage of "
                                     "numeric data." % (self.name))
                if not is_numeric_data(expr.arg(2)):
                    raise ValueError(
                        "Constraint '%s' found a double-sided "\
                        "inequality expression (lower <= "
                        "expression <= upper) but the upper "
                        "bound was not numeric data or an "
                        "expression restricted to storage of "
                        "numeric data."
                        % (self.name))

                self.lb = expr.arg(0)
                self.body = expr.arg(1)
                self.ub = expr.arg(2)

        #
        # Error check, to ensure that we don't have an equality
        # constraint with 'infinite' RHS
        #
        assert not (self.equality and (self.lb is None))
        assert (not self.equality) or (self.lb is self.ub)
示例#31
0
    def test_error(self):
        class A(object):
            pass

        val = A()
        self.assertEqual(False, is_numeric_data(val))
示例#32
0
 def test_NumericValue(self):
     self.assertEqual(is_numeric_data(NumericConstant(1.0)), True)
示例#33
0
 def test_string(self):
     self.assertEqual(is_numeric_data("a"), False)
     self.assertEqual(is_numeric_data(b"a"), False)
示例#34
0
 def test_float(self):
     self.assertEqual(is_numeric_data(0.0), True)