Example #1
0
    def copy(self: Solver, solver_name: str = "") -> "Model":
        """ Creates a copy of the current model

        Args:
            solver_name(str): solver name (optional)

        Returns:
            clone of current model
        """
        if not solver_name:
            solver_name = self.solver_name
        copy = Model(self.name, self.sense, solver_name)

        # adding variables
        for v in self.vars:
            copy.add_var(name=v.name,
                         lb=v.lb,
                         ub=v.ub,
                         obj=v.obj,
                         var_type=v.var_type)

        # adding constraints
        for c in self.constrs:
            orig_expr = c.expr
            expr = LinExpr(const=orig_expr.const, sense=orig_expr.sense)
            for (var, value) in orig_expr.expr.items():
                expr.add_term(self.vars[var.idx], value)
            copy.add_constr(lin_expr=expr, name=c.name)

        # setting objective function"s constant
        copy.objective_const = self.objective_const

        return copy
Example #2
0
def xsum(terms) -> LinExpr:
    """
    Function that should be used to create a linear expression from a summation.
    While the python function sum() can also be used, this function is optimized
    version for quickly generating the linear expression.

    Args:
        terms: set (ideally a list) of terms to be summed
    """
    result = LinExpr()
    for term in terms:
        result.add_term(term)
    return result