#!perl

use strict;
use warnings;

use Algorithm::Evolutionary::Op::Creator;
use Algorithm::Evolutionary::Op::Permutation;
use Algorithm::Evolutionary::Op::IncMutation;
use Algorithm::Evolutionary::Op::Crossover;
use Algorithm::Evolutionary::Op::CanonicalGA;
use String::Approx qw( adistr );

my $target =shift || 'CGATACGTTGCA';
my $maxGenerations = shift || 100;
my $popSize = shift || 100;

my $fitness = sub {
  my $indi = shift;
  return 1 - abs ( adistr( $indi->Chrom, $target ) );
};

my $incmutation =  new Algorithm::Evolutionary::Op::IncMutation;
my $mutation = new Algorithm::Evolutionary::Op::Permutation;
my $crossover = new Algorithm::Evolutionary::Op::Crossover;
my $ez = new Algorithm::Evolutionary::Op::Easy $fitness, 0.4, [$mutation, $crossover, $incmutation ];
my $indiType = 'String';
my $hash = { length => length( $target ),
	     chars => ['A','C','G','T']} ;
my $creator = new Algorithm::Evolutionary::Op::Creator( $popSize, 'String', $hash);
my @population = ();
$creator->apply( \@population );
my $gen;
do { 
  $ez->apply (\@population );
  print "Best so far: ", $population[0]->asString(), "\n";
} until ( $population[0]->Chrom eq $target ) || ($gen++ > $maxGenerations) ;

print "Final\n", $population[0]->asString();
