How to set Pyomo solver timeout?
Asked Answered
P

2

9

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

Paring answered 15/2, 2016 at 9:29 Comment(0)
P
16

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.

Paring answered 15/2, 2016 at 10:2 Comment(1)
I am doing the same thing (although using 'cbc' solver, not sure if that is relevant or not) but I don't think this answers the original question: "tell pyomo, after x seconds, return the optimal solution currently found." This just seems to throw the error mentioned. Should it return an actual "solution" even if it's not optimal and/or doesn't satisfy all the constraints? Is that possible?Karynkaryo
D
12

I had success with the following in Pyomo. The name of the time limit option is different for different solvers:

    self.solver = pyomo.opt.SolverFactory(SOLVER_NAME)
    if 'cplex' in SOLVER_NAME:
        self.solver.options['timelimit'] = TIME_LIMIT
    elif 'glpk' in SOLVER_NAME:         
        self.solver.options['tmlim'] = TIME_LIMIT
    elif 'gurobi' in SOLVER_NAME:           
        self.solver.options['TimeLimit'] = TIME_LIMIT
    elif 'xpress' in SOLVER_NAME:
        self.solver.options['soltimelimit'] = TIME_LIMIT 
        # Use the below instead for XPRESS versions before 9.0
        # self.solver.options['maxtime'] = TIME_LIMIT 
        

Where TIME_LIMIT is an integer time limit in seconds.

Damara answered 20/6, 2018 at 14:42 Comment(1)
would be nice if you would add cbc as wellMuffle

© 2022 - 2024 — McMap. All rights reserved.