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(671
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.
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;
On Monday, 30 July 2012 at 13:27:58 UTC, maarten van damme wrote:
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
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 helps.
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
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 is
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.
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
2012/7/30 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
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
On Monday, 30 July 2012 at 18:50:24 UTC, bearophile wrote:
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
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
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.
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 tr
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
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 isn't
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 no
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.
http://d.puremagic.com/issues/show_bug.cgi?id
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
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:
core.exception.AssertError@C:\D\dmd
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
potentia
2012/7/31 Timon Gehr :
> 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 parameters i
On 07/31/2012 12:30 AM, maarten van damme wrote:
2012/7/31 Timon Gehr:
...
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 means for indefinit
On 7/31/12, Timon Gehr wrote:
> I like it more because it says "loop".
I'd love to have "loop { }". But keyword bloat yada yada. :)
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 acceptable
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:
http://d.puremagic.com/issues/show_bug
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 Gehr:
...
further comments whose application does not lead to immediate
benefit:
- in contracts are better specified in their dedicated
section to push
the requiremen
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 non-d
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,
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 imm
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
32 matches
Mail list logo