def multiply(self, e): self.check_discrete_lock_frozen(e) if is_number(e): if 0 == e: product = self.get_linear_factory().new_zero_expr() else: self._coef *= e self.notify_modified(event=UpdateEvent.LinExprCoef) product = self elif isinstance(e, LinearExpr): product = e.times(self) elif isinstance(e, Var): product = self.model._qfactory.new_var_product(e, self) elif isinstance(e, MonomialExpr): product = self.model._qfactory.new_monomial_product(self, e) elif isinstance(e, Expr) and e.is_quad_expr(): if e.has_quadratic_term(): StaticTypeChecker.mul_quad_lin_error(self._model, self, e) else: product = self.model._qfactory.new_monomial_product( self, e.linear_part) else: product = self.to_linear_expr().multiply(e) self.notify_replaced(product) return product
def _check_is_discrete(self, ct): StaticTypeChecker.typecheck_discrete_constraint( self, ct, msg= 'Conversion from constraint to expression is available only for discrete constraint' )
def multiply(self, e): """ Multiplies this expression by an expression. Note: This method does not create a new expression but modifies the `self` instance. Args: e: The expression that is used to multiply `self`. Returns: The modified `self`. See Also: The method :func:`times` to compute a multiplication without modifying the `self` instance. """ mul_res = self event = UpdateEvent.LinExprGlobal self_constant = self.get_constant() if is_number(e): self._scale(factor=e) elif isinstance(e, LinearOperand): if e.is_constant(): # simple scaling self._scale(factor=e.get_constant()) elif self.is_constant(): # self is constant: import other terms , scaled. # set constant to zero. if self_constant: for lv, lk in e.iter_terms(): self.set_coefficient(dvar=lv, coeff=lk * self_constant) self._constant *= e.get_constant() else: # yields a quadratic mul_res = self.model._qfactory.new_linexpr_product(self, e) event = UpdateEvent.LinExprPromotedToQuad elif isinstance(e, ZeroExpr): self._scale(factor=0) elif isinstance(e, Expr) and e.is_quad_expr(): if not e.number_of_quadratic_terms: return self.multiply(e.linear_part) elif self.is_constant(): return e.multiply(self.get_constant()) else: StaticTypeChecker.mul_quad_lin_error(self._model, self, e) else: self.fatal( "Multiply expects variable, expr or number, {0!r} was passed (type is {1})", e, type(e)) self.notify_modified(event=event) return mul_res
def new_neq_constraint(self, lhs, rhs, ctname=None): m = self._model left_expr = self._to_linear_operand (lhs, msg="The `!=` operator requires two linear expressions, {0} was passed (left)") right_expr = self._to_linear_operand(rhs, msg="The `!=` operator requires two linear expressions, {0} was passed (right)") StaticTypeChecker.typecheck_discrete_expression(m, msg="NotEqualConstraint", expr=left_expr) StaticTypeChecker.typecheck_discrete_expression(m, msg="NotEqualConstraint", expr=right_expr) self._checker.typecheck_two_in_model(m, left_expr, right_expr, "new_binary_constraint") negated_ct = self.new_eq_constraint(lhs, rhs) ct = NotEqualConstraint(self._model, negated_ct, ctname) return ct
def check_lp_name(self, qualifier, new_name, accept_empty, accept_none): return StaticTypeChecker.check_lp_name(logger=self, qualifier=qualifier, obj=self, new_name=new_name, accept_empty=accept_empty, accept_none=accept_none)
def multiply(self, other): event = UpdateEvent.QuadExprGlobal if is_number(other): self._scale(other) elif self.is_constant(): this_constant = self._linexpr.get_constant() if 0 == this_constant: # do nothing event = None else: self._assign_scaled(other, this_constant) elif self.has_quadratic_term(): if other.is_constant(): return self.multiply(other.get_constant()) else: StaticTypeChecker.mul_quad_lin_error(self.model, self, other) else: # self is actually a linear expression if other.is_quad_expr(): if other.has_quadratic_term(): StaticTypeChecker.mul_quad_lin_error( self.model, self, other) else: return self.multiply(other._linexpr) else: other_linexpr = other.to_linear_expr() self_linexpr = self._linexpr for v1, k1 in self_linexpr.iter_terms(): for v2, k2 in other_linexpr.iter_terms(): self._add_one_quad_term(VarPair(v1, v2), k1 * k2) other_cst = other.get_constant() self_cst = self_linexpr.get_constant() if other_cst: self_linexpr._scale(other_cst) if self_cst: for ov, ok in other.iter_terms(): self_linexpr._add_term(ov, ok * self_cst) self.notify_modified(event) return self
def check_number(logger, arg, caller=None): StaticTypeChecker.typecheck_num_nan_inf(logger, arg, caller)
def scal_prod_functional(self, var_dict, coef_fn): StaticTypeChecker.typecheck_callable( self, coef_fn, "Functional scalar product requires a function taking variable keys as argument. A non-callable was passed: {0!r}" .format(coef_fn)) return self._aggregator._scal_prod_f(var_dict, coef_fn)
def logical_or(self, other): self._check_binary_variable_for_logical_op(op_name="or") StaticTypeChecker.typecheck_logical_op(self, other, caller="Var.logical_or") return self.get_linear_factory().new_logical_or_expr([self, other])
def check_number(logger, arg): StaticTypeChecker.typecheck_num_nan_inf(logger, arg)