Re: sorting failed error

2012-07-31 Thread monarch_dodra
On Tuesday, 31 July 2012 at 00:21:59 UTC, bearophile wrote: Jonathan M Davis: I'd actually argue that structs without opCmp shouldn't have it implicitly defined. It's just begging for bugs otherwise. For reference, this is the issue we are talking about:

Re: sorting failed error

2012-07-31 Thread monarch_dodra
On Monday, 30 July 2012 at 22:58:28 UTC, Timon Gehr wrote: On 07/31/2012 12:30 AM, maarten van damme wrote: 2012/7/31 Timon Gehrtimon.g...@gmx.ch: ... further comments whose application does not lead to immediate benefit: - in contracts are better specified in their dedicated section to

Re: sorting failed error

2012-07-31 Thread monarch_dodra
On Monday, 30 July 2012 at 22:23:10 UTC, Timon Gehr wrote: On 07/30/2012 03:52 PM, monarch_dodra wrote: ... NaN then compares false with everything, making it un-transitive, and potentially breaking your cmp. COuld you try the algo with return 1/(1+travelled). That, or because of the

Re: sorting failed error

2012-07-31 Thread maarten van damme
I now tried to bring it to the next level, using concurrency to bread a couple of populations and make the best solutions migrate between them. But, to use concurrency in D, one has to continually cast between immutable, cast immutable away, Not only that, casting away from immutable (or too,

Re: sorting failed error

2012-07-31 Thread Jonathan M Davis
On Tuesday, July 31, 2012 11:41:19 maarten van damme wrote: I now tried to bring it to the next level, using concurrency to bread a couple of populations and make the best solutions migrate between them. But, to use concurrency in D, one has to continually cast between immutable, cast

Re: sorting failed error

2012-07-31 Thread Minas
On Tuesday, 31 July 2012 at 09:41:31 UTC, maarten van damme wrote: I now tried to bring it to the next level, using concurrency to bread a couple of populations and make the best solutions migrate between them. But, to use concurrency in D, one has to continually cast between immutable, cast

sorting failed error

2012-07-30 Thread maarten van damme
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.

Re: sorting failed error

2012-07-30 Thread monarch_dodra
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:

Re: sorting failed error

2012-07-30 Thread maarten van damme
My cmp is : bool fitnessCompare(chromosome first,chromosome second){ return fitness(first)fitness(second); } float fitness(chromosome victim){ city[] cities=victim.dna; //we need to start from home and return to home cities=city(0,0) ~ cities;

Re: sorting failed error

2012-07-30 Thread bearophile
maarten van damme: I have no idea what is wrong with my code and the error is not very informative. As first step I suggest to add a space after commas, before brackets, around operators, etc. The improved readability helps spot the bugs. Adding Contracts and other asserts sometimes

Re: sorting failed error

2012-07-30 Thread maarten van damme
it is guaranteed to never be 0 because it always contains all cities so it will always travel something unless there are no cities or when all cities are in the same place (in which case the task to solve is senseless). I'll create an array containing all finesses and try sorting using that. And

Re: sorting failed error

2012-07-30 Thread Jonathan M Davis
On Monday, July 30, 2012 14:36:07 maarten van damme wrote: 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? My first guess would be that your sorting function is not antisymmetric. I ran into such an

Re: sorting failed error

2012-07-30 Thread maarten van damme
About float not beeing deterministic, would this behavior also exist when I use doubles? I've changed everything to use doubles now and the same error comes up. My sorting function used so I guess it can never return on (a,b) when (b,a) was true. But on the other hand, it can return false twice.

Re: sorting failed error

2012-07-30 Thread bearophile
maarten van damme: I really dislike contracts, they kind off blow your function up while adding not that much. I'll add some unit tests. They make your programs more robust. Contracts are used to spot bugs in your code and not to validate data coming from the outside world. As most other

Re: sorting failed error

2012-07-30 Thread maarten van damme
2012/7/30 bearophile bearophileh...@lycos.com: maarten van damme: I really dislike contracts, they kind off blow your function up while adding not that much. I'll add some unit tests. They make your programs more robust. Contracts are used to spot bugs in your code and not to validate

Re: sorting failed error

2012-07-30 Thread bearophile
maarten van damme: Still my sorting problem isn't sorted out. Another possible cause: in D if you sort structs that contain dynamic arrays, the actual contents of the arrays is ignored. This is a HUGE bug in DMD. Bye, bearophile

Re: sorting failed error

2012-07-30 Thread bearophile
monarch_dodra: struct S { int opCmp(S b) { if(v[0] b.v[0]) return -1; if(v[0] b.v[0]) return 1; return 0; } int[] v; } ... Or did you mean something else? I meant without a definend opCmp. So this is not your problem. Bye, bearophile

Re: sorting failed error

2012-07-30 Thread monarch_dodra
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:

Re: sorting failed error

2012-07-30 Thread maarten van damme
Great, with that workaround everything works correctly now. I can finally start playing around with my salesman's dna :p There is one little problem left, the comparing problem. I can't really define an opcmp because a city isn't smaller or bigger than another city, it's simply another city. I

Re: sorting failed error

2012-07-30 Thread Jonathan M Davis
On Monday, July 30, 2012 21:17:29 bearophile wrote: I meant without a definend opCmp. So this is not your problem. I'd actually argue that structs without opCmp shouldn't have it implicitly defined. It's just begging for bugs otherwise. - Jonathan M Davis

Re: sorting failed error

2012-07-30 Thread monarch_dodra
On Monday, 30 July 2012 at 20:01:59 UTC, maarten van damme wrote: Great, with that workaround everything works correctly now. I can finally start playing around with my salesman's dna :p There is one little problem left, the comparing problem. I can't really define an opcmp because a city

Re: sorting failed error

2012-07-30 Thread monarch_dodra
On Monday, 30 July 2012 at 19:17:30 UTC, bearophile wrote: monarch_dodra: struct S { int opCmp(S b) { if(v[0] b.v[0]) return -1; if(v[0] b.v[0]) return 1; return 0; } int[] v; } ... Or did you mean something else? I meant without a definend opCmp. So this is not

Re: sorting failed error

2012-07-30 Thread Ellery Newcomer
On 07/30/2012 05:36 AM, maarten van damme wrote: 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? Congratulations, it looks like you've hit a compiler bug.

Re: sorting failed error

2012-07-30 Thread maarten van damme
monarch_dodra, I'm not trying to order cities, I'm trying to filter out duplicate's in my dna chromosomes and == isn't working on structs that encapsulates dynamic arrays. And I can compare elements without defining opCmp. I've written a function that calculates the distance travelled when going

Re: sorting failed error

2012-07-30 Thread Timon Gehr
On 07/30/2012 02:36 PM, 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:

Re: sorting failed error

2012-07-30 Thread Timon Gehr
On 07/30/2012 03:52 PM, monarch_dodra wrote: ... Your looks OK, and I doubt you are mutating. I only see floating point gotchas: If a chromosome travels nothing, then his fitness is 1/0 = NaN. 1/0 evaluates to Inf NaN then compares false with everything, making it un-transitive, and

Re: sorting failed error

2012-07-30 Thread maarten van damme
2012/7/31 Timon Gehr timon.g...@gmx.ch: I realize that the code is just for fun, but I have some comments: - don't .dup stuff just in order to index directly. it never has any effect other than performance degradation. (this could be a compiler warning) - crossOver and mutate mutate their

Re: sorting failed error

2012-07-30 Thread Timon Gehr
On 07/31/2012 12:30 AM, maarten van damme wrote: 2012/7/31 Timon Gehrtimon.g...@gmx.ch: ... further comments whose application does not lead to immediate benefit: - in contracts are better specified in their dedicated section to push the requirements onto the caller. - consider for(;;) as a

Re: sorting failed error

2012-07-30 Thread Andrej Mitrovic
On 7/31/12, Timon Gehr timon.g...@gmx.ch wrote: I like it more because it says loop. I'd love to have loop { }. But keyword bloat yada yada. :)

Re: sorting failed error

2012-07-30 Thread bearophile
Jonathan M Davis: I'd actually argue that structs without opCmp shouldn't have it implicitly defined. It's just begging for bugs otherwise. For reference, this is the issue we are talking about: http://d.puremagic.com/issues/show_bug.cgi?id=3789 The current situation is totally not