def test_is_constant(self): p = parameter() self.assertEqual(p.is_constant(), False) self.assertEqual(is_constant(p), False) p.value = 1.0 self.assertEqual(p.is_constant(), False) self.assertEqual(is_constant(p), False)
def test_is_constant(self): v = variable() self.assertEqual(v.is_constant(), False) self.assertEqual(is_constant(v), False) self.assertEqual(v.fixed, False) self.assertEqual(v.value, None) v.value = 1.0 self.assertEqual(v.is_constant(), False) self.assertEqual(is_constant(v), False) self.assertEqual(v.fixed, False) self.assertEqual(v.value, 1.0) v.fix() self.assertEqual(v.is_constant(), False) self.assertEqual(is_constant(v), False) self.assertEqual(v.fixed, True) self.assertEqual(v.value, 1.0) v.value = None self.assertEqual(v.is_constant(), False) self.assertEqual(is_constant(v), False) self.assertEqual(v.fixed, True) self.assertEqual(v.value, None) v.free() self.assertEqual(v.is_constant(), False) self.assertEqual(is_constant(v), False) self.assertEqual(v.fixed, False) self.assertEqual(v.value, None)
def test_is_constant(self): e = self._ctype_factory() self.assertEqual(e.is_constant(), False) self.assertEqual(is_constant(e), False) e.expr = 1 self.assertEqual(e.is_constant(), False) self.assertEqual(is_constant(e), False) p = parameter() self.assertEqual(p.is_constant(), False) self.assertEqual(is_constant(p), False) p.value = 2 e.expr = p + 1 self.assertEqual(e.is_constant(), False) self.assertEqual(is_constant(e), False)
def test_is_constant(self): v = variable() e = noclone(v) self.assertEqual(e.is_constant(), False) self.assertEqual(is_constant(e), False) v.fix(1) self.assertEqual(e.is_constant(), False) self.assertEqual(is_constant(e), False) p = parameter() e = noclone(p) self.assertEqual(p.is_constant(), False) self.assertEqual(is_constant(p), False) self.assertEqual(is_constant(noclone(1)), True)
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: # or expr_type is list: # # Form equality expression # if len(expr) == 2: arg0 = expr[0] if arg0 is not None: arg0 = as_numeric(arg0) arg1 = expr[1] if arg1 is not None: arg1 = as_numeric(arg1) # assigning to the rhs property # will set the equality flag to True if arg1 is None or (not arg1._potentially_variable()): self.rhs = arg1 self.body = arg0 elif arg0 is None or (not arg0._potentially_variable()): self.rhs = arg0 self.body = arg1 else: with EXPR.bypass_clone_check(): self.rhs = ZeroConstant self.body = arg0 self.body -= arg1 # # Form inequality expression # elif len(expr) == 3: arg0 = expr[0] if arg0 is not None: arg0 = as_numeric(arg0) if arg0._potentially_variable(): raise ValueError( "Constraint '%s' found a 3-tuple (lower," " expression, upper) but the lower " "value was not data or an expression " "restricted to storage of data." % (self.name)) arg1 = expr[1] if arg1 is not None: arg1 = as_numeric(arg1) arg2 = expr[2] if arg2 is not None: arg2 = as_numeric(arg2) if arg2._potentially_variable(): raise ValueError( "Constraint '%s' found a 3-tuple (lower," " expression, upper) but the upper " "value was not data or an expression " "restricted to storage of 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 summation(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 summation(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 EXPR.generate_relational_expression.chainedInequality is not None: raise TypeError(EXPR.chainedInequalityErrorMessage()) # # Process relational expressions # (i.e. explicit '==', '<', and '<=') # if relational_expr: if _expr_type is EXPR._EqualityExpression: # Equality expression: only 2 arguments! _args = expr._args # Explicitly dereference the original arglist (otherwise # this runs afoul of the getrefcount logic) expr._args = [] # assigning to the rhs property # will set the equality flag to True if not _args[1]._potentially_variable(): self.rhs = _args[1] self.body = _args[0] elif not _args[0]._potentially_variable(): self.rhs = _args[0] self.body = _args[1] else: with EXPR.bypass_clone_check(): self.rhs = ZeroConstant self.body = _args[0] self.body -= _args[1] else: # Inequality expression: 2 or 3 arguments if expr._strict: try: _strict = any(expr._strict) except: _strict = True if _strict: # # We can relax this when: # (a) we have a need for this # (b) we have problem writer that # explicitly handles this # (c) we make sure that all problem writers # that don't handle this make it known # to the user through an error or # warning # raise ValueError( "Constraint '%s' encountered a strict " "inequality expression ('>' or '<'). All" " constraints must be formulated using " "using '<=', '>=', or '=='." % (self.name)) _args = expr._args # Explicitly dereference the original arglist (otherwise # this runs afoul of the getrefcount logic) expr._args = [] if len(_args) == 3: if _args[0]._potentially_variable(): raise ValueError( "Constraint '%s' found a double-sided " "inequality expression (lower <= " "expression <= upper) but the lower " "bound was not data or an expression " "restricted to storage of data." % (self.name)) if _args[2]._potentially_variable(): raise ValueError( "Constraint '%s' found a double-sided "\ "inequality expression (lower <= " "expression <= upper) but the upper " "bound was not data or an expression " "restricted to storage of data." % (self.name)) self.lb = _args[0] self.body = _args[1] self.ub = _args[2] else: if not _args[1]._potentially_variable(): self.lb = None self.body = _args[0] self.ub = _args[1] elif not _args[0]._potentially_variable(): self.lb = _args[0] self.body = _args[1] self.ub = None else: with EXPR.bypass_clone_check(): self.lb = None self.body = _args[0] self.body -= _args[1] self.ub = ZeroConstant # # Replace numeric bound values with a NumericConstant object, # and reset the values to 'None' if they are 'infinite' # if (self.lb is not None) and is_constant(self.lb): val = self.lb() if not pyutilib.math.is_finite(val): if val > 0: raise ValueError( "Constraint '%s' created with a +Inf lower " "bound." % (self.name)) self.lb = None if (self.ub is not None) and is_constant(self.ub): val = self.ub() if not pyutilib.math.is_finite(val): if val < 0: raise ValueError( "Constraint '%s' created with a -Inf upper " "bound." % (self.name)) self.ub = None # # 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)
def is_constant(self): """A boolean indicating whether this expression is constant.""" return is_constant(self._expr)