alexaa - what you can do is compare, at each generation, the best found solution with the best one from the previous generation/iteration. If the absolute difference between the two is less than some tolerance, then they can be considered equal, and you just increment a counter. When that counter hits 100, exit the loop (I'm guessing that you will be using a for or while loop).
Try the following
prevGenBestFitness = [];
numGensWithSameFitness = 0;
tol = 0.00001;
while true
bestFitness = …;
if ~isempty(prevGenBestFitness)
if abs(prevGenBestFitness-bestFitness)<tol
numGensWithSameFitness = numGensWithSameFitness + 1;
if numGensWithSameFitness==100
break;
end
else
numGensWithSameFitness = 0;
end
end
prevGenBestFitness = bestFitness;
end
Note that the above does make certain assumptions about the fitness/score for your population and so may have to be adjusted to take into account a different scoring scheme (or data type).
Try the above and see what happens!
Best Answer