Beispiel #1
0
 def _quadratic_expr(
     self, expr: QuadExpr
 ) -> Tuple[Dict[str, float], Dict[Tuple[str, str], float]]:
     linear = self._linear_expr(expr.get_linear_part())
     quad = {}
     for x, y, coeff in expr.iter_quad_triplets():
         i = self._var_names[x]
         j = self._var_names[y]
         quad[i, j] = coeff
     return linear, quad
Beispiel #2
0
    def new_linexpr_product(self, linexpr, other):
        if isinstance(other, Var):
            return self.new_var_product(other, linexpr)

        elif isinstance(other, MonomialExpr):
            return self.new_monomial_product(other, linexpr)

        elif isinstance(other, LinearExpr):
            cst1 = linexpr.constant
            cst2 = other.constant

            fcc = self.term_dict_type()
            for lv1, lk1 in linexpr.iter_terms():
                for lv2, lk2 in other.iter_terms():
                    update_dict_from_item_value(fcc, VarPair(lv1, lv2), lk1 * lk2)
            # this is quad
            qlinexpr = self.new_linear_expr()
            # add cst2 * linexp1
            qlinexpr._add_expr_scaled(expr=linexpr, factor=cst2)
            # add cst1 * linexpr2
            qlinexpr._add_expr_scaled(expr=other, factor=cst1)

            # and that's it
            # fix the constant
            qlinexpr.constant = cst1 * cst2
            quad = QuadExpr(self._model, quads=fcc, linexpr=qlinexpr, safe=True)
            return quad

        else:
            self._unexpected_product_error(linexpr, other)
 def new_quad(self, quads=None, linexpr=None, name=None, safe=False):
     self._quad_count += 1
     return QuadExpr(self._model,
                     quads=quads,
                     linexpr=linexpr,
                     name=name,
                     safe=safe)