我一直在使用PuLP来解决我感兴趣的特定混合整数线性程序(MIP)。但是,随着问题规模的增长,PuLP耗时太长。我希望能够运行求解器一段时间并且如果它需要很长时间并且获得迄今为止计算出的最佳可行解,那么它会过早地终止它。我已经尝试用信号手动计算解算器,但变量都是“无”。
我查看了文档,PuLP似乎并不支持这一点,但据我了解,它调用的大多数求解器例程都可以。有没有办法对PuLP施加时间限制?
答案 0 :(得分:2)
您可以自己调用solve()中执行的步骤,而不是直接调用solve()。这是使用cplex python api
时的一个例子#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
答案 1 :(得分:0)
在纸浆中,您可以调用其他外部解算器,例如cplex和gurobi。通常,您可以在调用求解器时在其参数中设置时间限制和最佳间隙。以gurobi为例:
prob = LpProblem("anything", LpMinimize)
prob.solve(GUROBI(timeLimit=1200))
您可以从纸浆的源代码中找到特定的参数。 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)