Implemented automatic cancelling of an optimization task after a given number of iterations.