Exemple #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
Exemple #2
0
 def objective(self: "Model", objective):
     if isinstance(objective, (int, float)):
         self.solver.set_objective(LinExpr([], [], objective))
     elif isinstance(objective, Var):
         self.solver.set_objective(LinExpr([objective], [1]))
     elif isinstance(objective, LinExpr):
         self.solver.set_objective(objective)
Exemple #3
0
 def objective(self: Solver, objective):
     if isinstance(objective, (int, float)):
         self.solver.set_objective(LinExpr([], [], objective))
     elif isinstance(objective, Var):
         self.solver.set_objective(LinExpr([objective], [1]))
     elif isinstance(objective, LinExpr):
         if objective.sense == MAXIMIZE:
             self.solver.set_objective_sense(MAXIMIZE)
         elif objective.sense == MINIMIZE:
             self.solver.set_objective_sense(MINIMIZE)
         self.solver.set_objective(objective)
Exemple #4
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
Exemple #5
0
def minimize(expr: LinExpr) -> LinExpr:
    """
    Function that should be used to set the objective function to MINIMIZE
    a given linear expression (passed as argument).

    Args:
        expr(LinExpr): linear expression
    """
    expr.sense = MINIMIZE
    return expr