I'm writing genetic algorithm for TPS. I've got class Chromosome that is derived from std::vector and has fitness as a member. I would like to sort population of chromosomes. IMO my operator< is 'strict weak ordering' relation. However, MVS thinks otherwise. Here is the code of operator:
bool Chromosome::operator<(const Chromosome & rhs) const
{
const Chromosome& lhs = *this;
if (lhs.fitness < rhs.fitness)
return true;
else
{
unsigned int size = lhs.size();
unsigned int zeroCityIndexlhs = std::find(lhs.begin(), lhs.end(), 0) - lhs.begin();
unsigned int zeroCityIndexrhs = std::find(rhs.begin(), rhs.end(), 0) - rhs.begin();
for (unsigned int i = 1; i < size; i++)
{
if (lhs[(zeroCityIndexlhs + i) % size] < rhs[(zeroCityIndexrhs + i) % size])
return true;
else if (lhs[(zeroCityIndexlhs + i) % size] == rhs[(zeroCityIndexrhs + i) % size])
continue;
else
return false;
}
return false;
}
}
Chromosome A is less than chromosome B, when it has smaller fitness, or the same fitness and a road starting from city 0 is lexicographically lesser than road in B. Program compiles but when it comes to sorting (using std::sort()), runtime error shows up with "Debug Assertion Failed!... invalid comparator".
assert faileddoesoperator<(a, b) == operator<(b, a)?std::vectorof what?? In other words, are you sureoperator<on the contained objects is sound.elsewhen you have return statement.