def _to_expr(self, qcc, lcc=None, constant=0): # no need to sort here, sort is done by str() on the fly. if qcc: linear_expr = LinearExpr(self._model, e=lcc, constant=constant, safe=True) quad = self._quad_factory.new_quad(quads=qcc, linexpr=linear_expr, safe=True) quad._transient = self._generate_transients return quad elif lcc or constant: linear_expr = LinearExpr(self._model, e=lcc, constant=constant, safe=True) linear_expr._transient = self._generate_transients return linear_expr else: return self.new_zero_expr()
def constant_expr(self, cst, safe_number=False, force_clone=False): if 0 == cst: return self.new_zero_expr() elif 1 == cst: if force_clone: return LinearExpr(self._model, e=None, constant=1, safe=True) else: return self._get_cached_one_expr() else: if safe_number: k = cst else: self_number_validation_fn = self.number_validation_fn k = self_number_validation_fn(cst) if self_number_validation_fn else cst return LinearExpr(self._model, e=None, constant=k, safe=True)
def _get_cached_one_expr(self): if self.one_expr is None: self.one_expr = LinearExpr(self._model, e=None, constant=1, safe=True) return self.one_expr
def constant_expr(self, cst, safe_number=False): if 0 == cst: return self.new_zero_expr() elif self.use_constant_expr: return self._new_constant_expr(cst, safe_number=safe_number) else: # pragma: no cover if safe_number: k = cst else: self_number_validation_fn = self.number_validation_fn k = self_number_validation_fn(cst) if self_number_validation_fn else cst return LinearExpr(self._model, e=None, constant=k, safe=True)
def linear_expr(self, arg=None, constant=0, name=None, safe=False, transient=False): return LinearExpr(self._model, arg, constant, name, safe=safe, transient=transient)
def new_one_expr(self): return LinearExpr(self._model, e=None, constant=1, safe=True)