When multiple termination parameters are used, Gurobi Optimizer will stop when it reaches the first one. For example, if you want to stop if you get to a 10% MIP gap or if 60 seconds have elapsed, then set MIPGap=0.1 and TimeLimit=60.
If a more complex set of stopping criteria is required, one possibility is to use warm starts. For example, suppose you need a 1% MIP gap, but a smaller MIP gap is preferable, but only if it takes less than 300 seconds. In this case, start with TimeLimit=300. If the value of the MIPGap attribute is greater than 0.01 when the time limit is reached, then increase the TimeLimit parameter, set the MIPGap parameter to 0.01, and continue to solve the MIP.
To set a time limit that is considered only if a feasible solution is found, you could use:
timeLimit = 20
try:
m = read('b1c1s1.mps.gz')
oldSolutionLimit = m.Params.SolutionLimit
m.Params.SolutionLimit = 1
m.optimize()
m.Params.TimeLimit = timeLimit - m.getAttr(GRB.Attr.Runtime)
m.Params.SolutionLimit = oldSolutionLimit - m.Params.SolutionLimit
m.optimize()
except (GurobiError, AttributeError, Exception) as e:
print('Caught: ' + e.message)
For more complex termination criteria, you may want to consider using callbacks. Please see How do I use callbacks to terminate the solver? for more information.
Further information