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@/
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|