I'm writing a genetic algorithm to solve a classification problem.
I'm setting up my configuration exactly how I've seen others do online, but using my own fitness function (required). I generate a random Genotype which holds my population and then evolve this population. However, sometimes I get an error 'Comparison method violates its general contract!'
I understand what this error means, but since it's being called on the framework method .evolve() I'm not sure what I can do...
Any thoughts/help? Thanks.
My Setup:
DefaultConfiguration.reset();
Configuration config = new DefaultConfiguration();
config.setPopulationSize(100);
// Setup fitness function
FitnessFunction fit = new HyperrectFitnessFunction(is);
config.setFitnessFunction(fit);
// Get bounds
double[][] bounds = getInstanceSetBounds(is);
// Setup chromosome
Chromosome sample = new Chromosome(config, createSampleGenes(config, attrCount, bounds));
config.setSampleChromosome(sample);
// Generate initial population
Genotype population = Genotype.randomInitialGenotype(config);
// Evolve
int i = 0;
IChromosome bestSolution = null;
for (i = 1; i < 100 + 1; i++) {
population.evolve();
bestSolution = population.getFittestChromosome();
double bestFitness = bestSolution.getFitnessValue();
if (bestFitness > 0.8)
break;
}
It means exactly what it says. This error generally means that your
compareTo
method is not consistent which may mean the order is dependant on the order of the parameters.For example if
compareTo(a,b)
is0
, thencompareTo(b,a)
must also be 0. The same can be said for "greater than" and "less than" relations. Inverting the parameters should also invert the answer.If this does not hold, it is impossible to find sort the values, since the order is dependant on the exact input of the comparator.