It would also give random incorrect results.
What I discovered was that in my script i was writing data to an external text file and the file was not closing properly after the second result. After commenting out the write to file line the optimizer is now working properly.
Comment