使用 Python PuLP 混合整数规划的时间限制

eep*_*y16 8 python linear-programming pulp

我一直在使用PuLP来解决我感兴趣的特定混合整数线性程序 (MIP)。但是,随着问题规模的增长,PuLP 花费的时间太长。我希望能够运行求解器一段时间,并在需要很长时间的情况下提前终止它,并获得迄今为止计算出的最佳可行解决方案。我尝试过用信号手动对求解器进行计时,但变量都是“无”。

我查看了文档,PuLP 似乎不支持这一点,尽管据我了解,它调用的大多数求解器例程都支持这一点。有没有办法对 PuLP 施加时间限制?

小智 1

在pulp中,您可以调用其他外部求解器,例如cplex和gurobi。通常,您可以在调用求解器时设置时间限制和参数的最佳间隙。以古罗比为例:

prob = LpProblem("anything", LpMinimize) prob.solve(GUROBI(timeLimit=1200))

具体参数可以从pull源码中找到。https://github.com/coin-or/pulp/blob/master/src/pulp/solvers.py

例如,如果您使用 gurobi,请参阅 init params

class GUROBI(LpSolver):
"""
The Gurobi LP/MIP solver (via its python interface)
The Gurobi variables are available (after a solve) in var.solverVar
Constriaints in constraint.solverConstraint
and the Model is in prob.solverModel
"""
try:
    sys.path.append(gurobi_path)
    # to import the name into the module scope
    global gurobipy
    import gurobipy
except: #FIXME: Bug because gurobi returns
        #a gurobi exception on failed imports
    def available(self):
        """True if the solver is available"""
        return False
    def actualSolve(self, lp, callback = None):
        """Solve a well formulated lp problem"""
        raise PulpSolverError("GUROBI: Not Available")
else:
    def __init__(self,
                mip = True,
                msg = True,
                timeLimit = None,
                epgap = None,
                **solverParams):
        """
        Initializes the Gurobi solver.
        @param mip: if False the solver will solve a MIP as an LP
        @param msg: displays information from the solver to stdout
        @param timeLimit: sets the maximum time for solution
        @param epgap: sets the integer bound gap
        """
        LpSolver.__init__(self, mip, msg)
        self.timeLimit = timeLimit
        self.epgap = epgap
        #set the output of gurobi
        if not self.msg:
            gurobipy.setParam("OutputFlag", 0)
        #set the gurobi parameter values
        for key,value in solverParams.items():
            gurobipy.setParam(key, value)
Run Code Online (Sandbox Code Playgroud)