On Monday, 30 July 2012 at 12:36:21 UTC, maarten van damme wrote:
For fun I started implementing a program that uses genetics
algorithm's to solve the travelling salesman problem. I want to sort an array of chromosome structures according to their fitness. I now
get the error:

core.exception.AssertError@C:\D\dmd2\windows\bin\..\..\src\phobos\std\algorithm.
d(6714): Failed to sort range of type chromosome[]. Actual result is: [chromosom e([city(20, 0), city(25, 25), city(10, 65), city(50, 50), city(75, 30), city(0, 10)]), chromosome([city(10, 65), city(50, 50), city(25, 25), city(75, 30), city(
...

I have no idea what is wrong with my code and the error is not very informative.
Does anyone have any idea as to what the problem could be?

(full code is at
https://dl.dropbox.com/u/15024434/travelling_salesman.d , If it's not something obvious then I'm going to try to reduce it to something as
small as possible)

Maarten

Just saw your code. Intriguing.

I'll have to stick with my gut feeling that sorting values by calculating a floating point value on the fly is not optimal.

BTW: This:
workers=array(sort!(fitnessCompare)(workers));
is equivalent to:
workers.sort!fitnessCompare();
The point of sort's return value is that it is typed as sorted, but you don't need to array->reassign.


Now, if instead of defining fitnessCompare, you use "schwartzSort", a sort made to sort things depending on the result of a function, calling the function one (AND ONLY ONCE) for each value:

Replace
workers.sort!fitnessCompare();
with
workers.schwartzSort!(fitness,"a > b")();

Then the sort works correctly. And schwartzSort ALSO validates the resulting sort.

Sorry I can't *certify* what the problem is, but at least you have a workaround ;)

Reply via email to