I have this code:
package org.optimization.geneticAlgorithm;
import org.optimization.geneticAlgorithm.selection.Pair;
public abstract class Chromosome imple
Most probably your fitness function is broken, in one of two ways:
compareTo()
is not transitive in the presence of NaNs, as explained by Jon Skeet.You could rewrite your comparison function using Double.compare()
:
public int compareTo(Chromosome o) {
return Double.compare(o.fitness(), this.fitness());
}
This requires less code and takes care of corner cases (NaNs, the negative zero etc). Of course, whether these corner cases should be arising in the first place is for you to decide and address.