首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Python PuLP混合整数编程的时间限制

Python PuLP混合整数编程的时间限制
EN

Stack Overflow用户
提问于 2017-12-27 09:11:13
回答 2查看 5.8K关注 0票数 5

我一直在用PuLP解决我感兴趣的一个特殊的混合整数线性规划。然而,随着问题规模的增长,PuLP花费的时间太长了。我希望能够运行求解器一段时间,如果时间太长,就提前终止它,并获得到目前为止计算的最佳可行解。我尝试用signal对求解器进行手动计时,但变量都是"None“。

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

EN

回答 2

Stack Overflow用户

发布于 2018-04-12 23:18:53

您可以自己调用solve()中执行的步骤,而不是直接调用solve()。下面是使用cplex python api的一个示例。

代码语言:javascript
运行
复制
#Create your problem
P = pulp.LpProblem()

#Build the solverModel for your preferred
solver = pulp.CPLEX_PY()
solver.buildSolverModel(P)

#Modify the solvermodel
solver.solverModel.parameters.timelimit.set(60)

#Solve P
solver.callSolver(P)
status = solver.findSolutionValues(P)

在buildSolverModel()之后,solver.solverModel包含求解器API的一个实例。然后,您可以使用求解器文档中的所有函数。我使用的是cplex,但在gurobi中也可以使用与http://www.gurobi.com/documentation/7.5/refman/python_parameter_examples.html#PythonParameterExamples相同的方法。

票数 4
EN

Stack Overflow用户

发布于 2018-03-20 16:23:40

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

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

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

例如,如果您使用的是gurobi,请参见init参数

代码语言:javascript
运行
复制
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)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47985247

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档