Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: AI::Genetic gives me strange warnings

by kvale (Monsignor)
on Feb 23, 2005 at 04:25 UTC ( [id://433569]=note: print w/replies, xml ) Need Help??


in reply to AI::Genetic gives me strange warnings

When I run the following code:
#!/usr/bin/perl use strict; use warnings; use AI::Genetic; use Data::Dumper; $|=1; my $ga = AI::Genetic->new ( -fitness => \&my_fitness, -type => 'rangevector' ); $ga->init([ [0,10], [0,10], [0,10], [0,10] ]); $ga->sortIndividuals($ga->people()); # Note that using this instead of sortPopulation does NOT # cause the same warnings: # my @populace = sort {$a->score <=> $b->score} @{$ga->people()}; sub my_fitness { my $genes = shift; print ref $genes, "\n"; my @sorted = sort { print Dumper($a,$b); sleep 1; $genes->[$a] <=> $genes->[$b] } 0..3; my $score = $sorted[1] + 2*$sorted[2] + 3*$sorted[3]; print "DEBUG: score was $score\n"; return $score; }
I get
1037% perl genetic.pl ARRAY $VAR1 = 0; $VAR2 = 1; $VAR1 = 2; $VAR2 = 3; $VAR1 = 1; $VAR2 = 2; $VAR1 = 1; $VAR2 = 3; $VAR1 = 0; $VAR2 = 3; DEBUG: score was 7 ARRAY $VAR1 = undef; $VAR2 = undef; Use of uninitialized value in array element at genetic.pl line 27. Use of uninitialized value in array element at genetic.pl line 27. $VAR1 = undef; $VAR2 = undef; Use of uninitialized value in array element at genetic.pl line 27. Use of uninitialized value in array element at genetic.pl line 27. $VAR1 = undef; $VAR2 = undef; Use of uninitialized value in array element at genetic.pl line 27. Use of uninitialized value in array element at genetic.pl line 27. $VAR1 = undef; $VAR2 = undef; Use of uninitialized value in array element at genetic.pl line 27. Use of uninitialized value in array element at genetic.pl line 27. DEBUG: score was 14 etc...
So the first sort seems to work fine, but it crashes on subsequent iterations. A mystery...

Update: It is an official bug: check out nested sort in perl-5.7.0 fails.

-Mark

Replies are listed 'Best First'.
Re^2: AI::Genetic gives me strange warnings
by fizbin (Chaplain) on Feb 23, 2005 at 04:30 UTC
    Right - the second time you try that sort, $a and $b are undef each time the block is evaluated. Furthermore, it appears that sort-within-sort only breaks if the inner sort is in a different package than the outer sort, as you'll notice that when I do the sort myself (inside main) there's no problem.

    Also, see my other reply that exhibits this phenomenon without using AI::Genetic.

    -- @/=map{[/./g]}qw/.h_nJ Xapou cets krht ele_ r_ra/; map{y/X_/\n /;print}map{pop@$_}@/for@/

      As ugly as it seems, a workaround (not denying a likely perl bug here) is to use $::a and $::b in the inner sort. Which may mean patching AI::Genetic... although I don't know what that will do during non-embedded sorts.

        Actually, there's a much, much easier patch to AI::Genetic that doesn't potentially screw up other non-embedded sorts. I've already communicated this to the AI::Genetic author, but you can see what's necessary in (the updated) 433606. It relies on the fact that individuals in an AI::Genetic population remember their score values once they've computed them. (Which is necessary for even vaguely decent performance when the fitness function gets a bit hairy)

        It's a very small three-line change, and does leave open the possibility of people screwing with the internals of the AI::Genetic object if they assign to $_, but you shouldn't be assigning to $_ in any case without using local to limit the effects of your changes.

        Also note that it's not the inner sort you'd be changing if you're changing AI::Genetic but the outer sort; the inner sort is already inside package main.

        -- @/=map{[/./g]}qw/.h_nJ Xapou cets krht ele_ r_ra/; map{y/X_/\n /;print}map{pop@$_}@/for@/

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://433569]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (7)
As of 2024-04-18 17:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found