Search code examples
pythontimeoutsolverpyomo

How to set Pyomo solver timeout?


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


Solution

  • 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.