Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

AI::Genetic gives me strange warnings

by fizbin (Chaplain)
on Feb 23, 2005 at 03:17 UTC ( [id://433559]=perlquestion: print w/replies, xml ) Need Help??

fizbin has asked for the wisdom of the Perl Monks concerning the following question:

It appears that I've run across a bug in either AI::Genetic or possibly in perl itself. I ran across this while attempting to follow up to node 433087.

The basic situation is this: in my fitness function, I use the genes of the AI::Genetic::Individual object to sort another list. On the second and subsequent times through my fitness function, it appears that the magic variables $a and $b inside the block passed to sort are being set to undef.

My current wild guess is that perl has problems when a sort block inside one package calls something that results in a sort block being called in a different package, but I'm not at all sure about that. In any case, I'm seeing this both in my cygwin 5.8.5 perl and my 5.6.1 ActiveState perl. I've simplified the code down as far as I can; it's behind the readmore tag:
Update: It's not AI::Genetic, at least not directly. It appears that I can duplicate this without depending on any external modules (see reply)

#!/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; my @order = (0,1,2,3); my @sorted = sort { local $SIG{__WARN__} = sub {print Dumper($a,$b); print @_; sleep 1 +;}; $genes->[$a] <=> $genes->[$b] } @order; my $score = $sorted[1] + 2*$sorted[2] + 3*$sorted[3]; print "DEBUG: score was $score\n"; return $score; }
Am I missing something obvious here?
-- @/=map{[/./g]}qw/.h_nJ Xapou cets krht ele_ r_ra/; map{y/X_/\n /;print}map{pop@$_}@/for@/

Replies are listed 'Best First'.
Re: AI::Genetic gives me strange warnings
by kvale (Monsignor) on Feb 23, 2005 at 04:25 UTC
    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

      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.

Shorter code
by fizbin (Chaplain) on Feb 23, 2005 at 04:21 UTC
    Yeah, it appears to be a perl bug, as I've now gotten something that demonstrates this problem without using AI::Genetic:

    #!/usr/bin/perl use strict; use warnings; package yo; use Data::Dumper; sub my_f { my $genes = shift; my @order = (0,1,2,3); my @sorted = sort { local $SIG{__WARN__} = sub {print Dumper($a,$b); print @_; sleep 1 +;}; $genes->[$a] <=> $genes->[$b] } @order; my $score = $sorted[1] + 2*$sorted[2] + 3*$sorted[3]; print "DEBUG: score was $score\n"; return $score; } package main; $|=1; my @populace = map { [ map {rand 100;} (1..4) ] } (1..100); my @spopu = sort {yo::my_f($a) <=> yo::my_f($b)} @populace;
    Well, hell. This sucks - I wonder if this is a documented perl bug anywhere?
    -- @/=map{[/./g]}qw/.h_nJ Xapou cets krht ele_ r_ra/; map{y/X_/\n /;print}map{pop@$_}@/for@/
Re: AI::Genetic gives me strange warnings
by halley (Prior) on Feb 23, 2005 at 15:47 UTC
    There are two mechanisms by which you can get the sort arguments. One mechanism is to trust that package globals $a and $b (they're not lexicals) will be defined by perl for evaluation.

    The other mechanism is to use a subroutine with a ($$) prototype; then the arguments $_[0] and $_[1] will be used instead.

    Maybe the second mechanism is useful for a workaround in either the inner loop or outer loop?

    --
    [ e d @ h a l l e y . c c ]

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://433559]
Approved by kvale
Front-paged by Old_Gray_Bear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (3)
As of 2024-04-20 01:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found