#!/usr/bin/perl -w use strict; sub fitness { my ($chars) = @_; my $s = join '', @$chars; my $f = system('perl', '-ce', $s); ## zero and an immediate return if it fails to compile if ($f == 0) { $ret = 10; } else { return 0; } ## quotes and POD is cheating. Comment char isn't included, ## so don't bother with it. $ret-=10 if (substr($s, 0, 1) eq '='); #subtract if it's a POD $ret-=10 if ($s =~ /^q(.).*?(\1)$/); #q() string? $ret-=10 if ($s =~ /^([`'"]).*(\1)$/); # quoted strings or backticks ## Spaces at the start and end are a lesser form of cheating $ret-=2 if ($s =~ /^ /); $ret-=2 if ($s =~ / $/); $ret++ if ($s =~ /\w \w/); # space separated words are kinda cool. if ($ret < 0) { $ret = 0 }; return $ret; } use AI::Genetic; my $ga = new AI::Genetic( -fitness => \&fitness, -type => 'listvector', ); my @chars = ( 32..34,36..64,91..126 ); $_ = chr($_) for @chars; $ga->init([ map { [@chars] } 1..20 ]); $ga->evolve('randomTwoPoint', 50); my $chars = $ga->getFittest->genes();; print "Best score = ", $ga->getFittest->score(), ".\n"; my $s = join '',@$chars; print "$s\n";