#/usr/bin/env perl #---AUTOPRAGMASTART--- use 5.012; use strict; use warnings; use diagnostics; use mro 'c3'; use English qw( -no_match_vars ); use Carp; our $VERSION = 1.7; no if $] >= 5.017011, warnings => 'experimental::smartmatch'; use Fatal qw( close ); #---AUTOPRAGMAEND--- BEGIN { push @INC, '.'; } use Evolver; use FileSlurp qw(slurpBinFile writeBinFile); use Array::Contains; my $bestscore = -10000; my $beest = Evolver->new(); $beest->resetStates; my $saving = 0; if(contains('--save', \@ARGV)) { print "Will save fittest to fittest.dat\n"; $saving = 1; } if(contains('--load', \@ARGV)) { if(!-f 'fittest.dat') { croak("File fittest.dat not found"); } my $data = slurpBinFile('fittest.dat'); $beest->crossPolinate($data); } foreach my $arg (@ARGV) { if($arg =~ /\-\-(.*)\=(.*)/) { $beest->config($1 => $2); } } while(1) { my ($generation, $newbestscore) = $beest->evolve; if($newbestscore > $bestscore) { print "** New best fittness score $bestscore\n"; my $fittest = $beest->getFittest; $bestscore = $newbestscore; if($saving) { writeBinFile('fittest.dat', $fittest); } } } sub doSpacePad { my ($val, $len) = @_; croak("$val longer than $len") if(length($val) > $len); #return $val if(length($val) == $len); $val .= ' ' x ($len - length($val)); return $val; } sub doTrim { my ($val) = @_; $val =~ s/\ +$//; return $val; }