Python gurobipy.GurobiError() Examples

The following are 6 code examples of gurobipy.GurobiError(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module gurobipy , or try the search function .
Example #1
Source File: solvers.py    From MatchingMarkets.py with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def findSolutionValues(self):
            for var in self.varables.values():
                try:
                    var.varValue = var.solver_var.X
                except gurobipy.GurobiError:
                    pass
            GRB = gurobipy.GRB
            gurobiLPStatus = {
                GRB.OPTIMAL: LpStatusOptimal,
                GRB.INFEASIBLE: LpStatusInfeasible,
                GRB.INF_OR_UNBD: LpStatusInfeasible,
                GRB.UNBOUNDED: LpStatusUnbounded,
                GRB.ITERATION_LIMIT: LpStatusNotSolved,
                GRB.NODE_LIMIT: LpStatusNotSolved,
                GRB.TIME_LIMIT: LpStatusNotSolved,
                GRB.SOLUTION_LIMIT: LpStatusNotSolved,
                GRB.INTERRUPTED: LpStatusNotSolved,
                GRB.NUMERIC: LpStatusNotSolved,
            }
            self.status = gurobiLPStatus.get(self.gurobi_model.Status, LpStatusUndefined)
            return self.status 
Example #2
Source File: petalvrp.py    From VeRyPy with MIT License 5 votes vote down vote up
def _relax_customer_constraints_with_feasRelax(m, customer_cover_constraints, active_ptls, relaxed_ptls):
    # relax the model and relax minimal number of customer serving constraints
    pens = [1.0]*len(customer_cover_constraints)
    m.feasRelax(2, True, None, None, None, customer_cover_constraints, pens)
    # TODO: not sure if feasRelax can change Status, test it someday
    if m.Status == GRB.INTERRUPTED: 
        raise KeyboardInterrupt()
    m.optimize()

    # restore SIGINT callback handler which is changed by gurobipy
    signal(SIGINT, default_int_handler)

    status = m.Status
    if __debug__:
        log(DEBUG-2, "Relaxed problem Gurobi runtime = %.2f"%m.Runtime)
        if m.Status == GRB.OPTIMAL:
            log(DEBUG-3, "Relaxed problem Gurobi objective = %.2f"%
                         m.getObjective().getValue())

    if status == GRB.OPTIMAL:
        return _decision_variables_to_petals(m.X, active_ptls, relaxed_ptls), False
    elif status == GRB.TIME_LIMIT:
        raise GurobiError(10023, "Gurobi timeout reached when attempting to solve relaxed SCPCVRP")
    elif m.Status == GRB.INTERRUPTED:
        raise KeyboardInterrupt()
    return None,False 
Example #3
Source File: utils.py    From pytfa with Apache License 2.0 4 votes vote down vote up
def copy_solver_configuration(source, target):
    """
    Copies the solver configuration from a source model to a target model
    :param source:
    :param target:
    :return:
    """

    # LP method
    try:
        target.solver.configuration.lp_method = source.solver.configuration.lp_method
    except AttributeError:
        pass

    # Presolve
    target.solver.configuration.presolve = source.solver.configuration.presolve

    # Timeout
    target.solver.configuration.timeout = source.solver.configuration.timeout

    # Tolerances
    for tol_name in dir(source.solver.configuration.tolerances):
        tol = getattr(source.solver.configuration.tolerances, tol_name)
        setattr(target.solver.configuration.tolerances, tol_name, tol)

    # Additionnal solver-specific settings
    try:
        # Gurobi
        if source.solver.interface.__name__ == 'optlang.gurobi_interface':
            from gurobipy import GurobiError
            for k in dir(source.solver.problem.Params):
                if not k.startswith('_'):
                    try:
                        v = getattr(source.solver.problem.Params, k)
                        setattr(target.solver.problem.Params, k, v)
                    except GurobiError:
                        pass
    except ModuleNotFoundError:
        pass

    # Verbosity
    target.solver.configuration.verbosity = source.solver.configuration.verbosity 
Example #4
Source File: solvers.py    From MatchingMarkets.py with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
def findSolutionValues(self, lp):
            model = lp.solverModel
            solutionStatus = model.Status
            GRB = gurobipy.GRB
            gurobiLpStatus = {GRB.OPTIMAL: LpStatusOptimal,
                                   GRB.INFEASIBLE: LpStatusInfeasible,
                                   GRB.INF_OR_UNBD: LpStatusInfeasible,
                                   GRB.UNBOUNDED: LpStatusUnbounded,
                                   GRB.ITERATION_LIMIT: LpStatusNotSolved,
                                   GRB.NODE_LIMIT: LpStatusNotSolved,
                                   GRB.TIME_LIMIT: LpStatusNotSolved,
                                   GRB.SOLUTION_LIMIT: LpStatusNotSolved,
                                   GRB.INTERRUPTED: LpStatusNotSolved,
                                   GRB.NUMERIC: LpStatusNotSolved,
                                   }
            #populate pulp solution values
            for var in lp.variables():
                try:
                    var.varValue = var.solverVar.X
                except gurobipy.GurobiError:
                    pass
                try:
                    var.dj = var.solverVar.RC
                except gurobipy.GurobiError:
                    pass
            #put pi and slack variables against the constraints
            for constr in lp.constraints.values():
                try:
                    constr.pi = constr.solverConstraint.Pi
                except gurobipy.GurobiError:
                    pass
                try:
                    constr.slack = constr.solverConstraint.Slack
                except gurobipy.GurobiError:
                    pass
            if self.msg:
                print("Gurobi status=", solutionStatus)
            lp.resolveOK = True
            for var in lp.variables():
                var.isModified = False
            lp.status = gurobiLpStatus.get(solutionStatus, LpStatusUndefined)
            return lp.status 
Example #5
Source File: matchingvrp.py    From VeRyPy with MIT License 4 votes vote down vote up
def _mmp_solve(w1_ij, x_ij_keys, n, w2_ij = None):
    """A helper function that solves a weighted maximum matching problem.
    """
    
    m = Model("MBSA")
    
    if __debug__:
        log(DEBUG,"")
        log(DEBUG,"Solving a weighted maximum matching problem with "+
                  "%d savings weights." % len(w1_ij))

    # build model
    x_ij = m.addVars(x_ij_keys, obj=w1_ij, vtype=GRB.BINARY, name='x')    
    _mmp_add_cnts_sum(m, x_ij, x_ij_keys, w1_ij, n)
    m._vars = x_ij
    m.modelSense = GRB.MAXIMIZE
    m.update()

    # disable output
    m.setParam('OutputFlag', 0)    
    m.setParam('TimeLimit', MAX_MIP_SOLVER_RUNTIME)
    m.setParam('Threads', MIP_SOLVER_THREADS)
    #m.write("out.lp")
    m.optimize()

    # restore SIGINT callback handler which is changed by gurobipy
    signal(SIGINT, default_int_handler)

    if __debug__:
        log(DEBUG-1, "Gurobi runtime = %.2f"%m.Runtime)
    
    if m.Status == GRB.OPTIMAL:
        if w2_ij==None:
            max_wt, max_merge = max( (w1_ij[k], x_ij_keys[k])
                                      for k, v in enumerate(m.X) if v )
        else:
            max_wt, _, max_merge = max( (w1_ij[k], w2_ij[k], x_ij_keys[k])
                                      for k, v in enumerate(m.X) if v )
            
        return max_wt, max_merge[0], max_merge[1]
    elif m.Status == GRB.TIME_LIMIT:
        raise GurobiError(10023, "Gurobi timeout reached when attempting to solve GAP")
    elif m.Status == GRB.INTERRUPTED:
        raise KeyboardInterrupt()
    return None 
Example #6
Source File: gurobi.py    From GridCal with GNU General Public License v3.0 4 votes vote down vote up
def findSolutionValues(self, lp):
            """

            :param lp:
            :return:
            """
            model = lp.solverModel
            solutionStatus = model.Status
            GRB = gurobipy.GRB
            gurobiLpStatus = {GRB.OPTIMAL: LpStatusOptimal,
                              GRB.INFEASIBLE: LpStatusInfeasible,
                              GRB.INF_OR_UNBD: LpStatusInfeasible,
                              GRB.UNBOUNDED: LpStatusUnbounded,
                              GRB.ITERATION_LIMIT: LpStatusNotSolved,
                              GRB.NODE_LIMIT: LpStatusNotSolved,
                              GRB.TIME_LIMIT: LpStatusNotSolved,
                              GRB.SOLUTION_LIMIT: LpStatusNotSolved,
                              GRB.INTERRUPTED: LpStatusNotSolved,
                              GRB.NUMERIC: LpStatusNotSolved}

            # populate pulp solution values
            try:
                for var, value in zip(lp.variables(), model.getAttr(GRB.Attr.X, model.getVars())):
                    var.varValue = value
            except (gurobipy.GurobiError, AttributeError):
                pass

            try:
                for var, value in zip(lp.variables(), model.getAttr(GRB.Attr.RC, model.getVars())):
                    var.dj = value
            except (gurobipy.GurobiError, AttributeError):
                pass

            # put pi and slack variables against the constraints
            try:
                for constr, value in zip(lp.constraints.values(), model.getAttr(GRB.Pi, model.getConstrs())):
                    constr.pi = value
            except (gurobipy.GurobiError, AttributeError):
                pass

            try:
                for constr, value in zip(lp.constraints.values(), model.getAttr(GRB.Slack, model.getConstrs())):
                    constr.slack = value
            except (gurobipy.GurobiError, AttributeError):
                pass

            if self.msg:
                print("Gurobi status=", solutionStatus)
            lp.resolveOK = True
            for var in lp.variables():
                var.isModified = False
            lp.status = gurobiLpStatus.get(solutionStatus, LpStatusUndefined)
            return lp.status