如何设置 Pyomo 求解器超时?
How to set Pyomo solver timeout?
如何设置 Pyomo solve() 方法的超时时间?更具体地说,告诉 pyomo,在 x 秒后,return 当前找到的最佳解决方案?
所以我能够通过 pyomo 文档找到答案,我认为分享它会有所帮助。
设置 Pyomo 的超时 solve()
方法:
solver.solve(model, timelimit=5)
但是,如果求解器未终止,这将抛出 pyutilib.common._exceptions.ApplicationError: "Solver (%s) did not exit normally" % self.name )
。我真正想要的是将 timelimit
选项传递给我的求解器。在我的 cplex
求解器的例子中,代码将是这样的:
solver = SolverFactory('cplex')
solver.options['timelimit'] = 5
results = solver.solve(model, tee=True)
更多关于 pyomo and cplex docs。
我在 PYOMO 中取得了以下成功。 cplex 和 glpk 的时间限制选项名称不同。
self.solver = pyomo.opt.SolverFactory(SOLVER_NAME)
if SOLVER_NAME == 'cplex':
self.solver.options['timelimit'] = TIME_LIMIT
elif SOLVER_NAME == 'glpk':
self.solver.options['tmlim'] = TIME_LIMIT
elif SOLVER_NAME == 'gurobi':
self.solver.options['TimeLimit'] = TIME_LIMIT
其中 TIME_LIMIT 是以秒为单位的整数时间限制。
如何设置 Pyomo solve() 方法的超时时间?更具体地说,告诉 pyomo,在 x 秒后,return 当前找到的最佳解决方案?
所以我能够通过 pyomo 文档找到答案,我认为分享它会有所帮助。
设置 Pyomo 的超时 solve()
方法:
solver.solve(model, timelimit=5)
但是,如果求解器未终止,这将抛出 pyutilib.common._exceptions.ApplicationError: "Solver (%s) did not exit normally" % self.name )
。我真正想要的是将 timelimit
选项传递给我的求解器。在我的 cplex
求解器的例子中,代码将是这样的:
solver = SolverFactory('cplex')
solver.options['timelimit'] = 5
results = solver.solve(model, tee=True)
更多关于 pyomo and cplex docs。
我在 PYOMO 中取得了以下成功。 cplex 和 glpk 的时间限制选项名称不同。
self.solver = pyomo.opt.SolverFactory(SOLVER_NAME)
if SOLVER_NAME == 'cplex':
self.solver.options['timelimit'] = TIME_LIMIT
elif SOLVER_NAME == 'glpk':
self.solver.options['tmlim'] = TIME_LIMIT
elif SOLVER_NAME == 'gurobi':
self.solver.options['TimeLimit'] = TIME_LIMIT
其中 TIME_LIMIT 是以秒为单位的整数时间限制。