简体   繁体   中英

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 ?

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 .

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.

I cannot do it when calling pyomo grom the comand line. For example "pyomo solve ReferenceModel_v2.py Instancia_Chica_fin_de_semana.dat --solver=gurobi --save-results=file.txt --logfile=log.txt --solver-options mipgap=0.0
--solver-options TimeLimit=1.0 --solver-options threads=8" this does not respect the time limit of one second

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM