How to set Pyomo solver timeout?

戏子无情 提交于 2019-12-10 10:13:30

问题


How to set the timeout for Pyomo solve() method ? More specifically, to tell pyomo, after x seconds, return the optimal solution currently found ?


回答1:


So I was able to find the answer via pyomo documentation and I thought it would be helpful to share.

To set the timeout for Pyomo solve() method:

solver.solve(model, timelimit=5)

However this will throw pyutilib.common._exceptions.ApplicationError: "Solver (%s) did not exit normally" % self.name ) if the solver is not terminated. What I really want is to pass the timelimit option to my solver. In my case of cplex solver, the code will be like this:

solver = SolverFactory('cplex')
solver.options['timelimit'] = 5
results = solver.solve(model, tee=True)

More on pyomo and cplex docs.




回答2:


I had success with the following in PYOMO. The name of the time limit option is different for cplex and 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

Where TIME_LIMIT is an integer time limit in seconds.



来源:https://stackoverflow.com/questions/35405642/how-to-set-pyomo-solver-timeout

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!