#!/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;
}
####
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...
##
##