File Coverage

lib/Algorithm/MasterMind/Canonical_GA.pm
Criterion Covered Total %
statement 15 15 100.0
branch n/a
condition n/a
subroutine 5 5 100.0
pod n/a
total 20 20 100.0


line stmt bran cond sub pod time code
1             package Algorithm::MasterMind::Canonical_GA;
2              
3 2     2   715 use warnings;
  2         4  
  2         62  
4 2     2   9 use strict;
  2         2  
  2         67  
5 2     2   10 use Carp;
  2         2  
  2         119  
6              
7 2         20 use lib qw(../../lib ../../../../Algorithm-Evolutionary/lib/
8             ../../../lib
9 2     2   9 ../../Algorithm-Evolutionary/lib/);
  2         2  
10              
11             our $VERSION = sprintf "%d.%03d", q$Revision: 1.1 $ =~ /(\d+)\.(\d+)/g;
12              
13 2     2   635 use base 'Algorithm::MasterMind::Evolutionary_Base';
  2         4  
  2         1168  
14              
15             use Algorithm::Evolutionary::Op::String_Mutation;
16             use Algorithm::Evolutionary::Op::QuadXOver;
17             use Algorithm::Evolutionary::Op::CanonicalGA;
18             use Algorithm::Evolutionary::Individual::String;
19              
20             # ---------------------------------------------------------------------------
21              
22              
23             sub initialize {
24             my $self = shift;
25             my $options = shift;
26             for my $o ( keys %$options ) {
27             $self->{"_$o"} = $options->{$o};
28             }
29              
30             # Variation operators
31             my $m = new Algorithm::Evolutionary::Op::String_Mutation; # Rate = 1
32             my $c = Algorithm::Evolutionary::Op::QuadXOver->new( 1,2 );
33              
34             my $fitness = sub { $self->fitness_orig(@_) };
35             my $ga = new Algorithm::Evolutionary::Op::CanonicalGA( $fitness,
36             $options->{'replacement_rate'},
37             [ $m, $c] );
38             $self->{'_fitness'} = $fitness;
39             $self->{'_ga'} = $ga;
40              
41             }
42              
43              
44             sub issue_next {
45             my $self = shift;
46             my $rules = $self->number_of_rules();
47             my @alphabet = @{$self->{'_alphabet'}};
48             my $length = $self->{'_length'};
49             my $pop = $self->{'_pop'};
50             my $ga = $self->{'_ga'};
51             map( $_->evaluate( $self->{'_fitness'}), @$pop );
52             my @ranked_pop = sort { $b->{_fitness} <=> $a->{_fitness}; } @$pop;
53              
54             if ( $ranked_pop[0]->{'_matches'} == $rules ) { #Already found!
55             return $self->{'_last'} = $ranked_pop[0]->{'_str'};
56             } else {
57             my @pop_by_matches;
58             my $best;
59             do {
60             $ga->apply( $pop );
61             print "PoblaciĆ³n ", scalar @$pop, "\n";
62             map( $_->{'_matches'} = $_->{'_matches'}?$_->{'_matches'}:-1, @$pop ); #To avoid warnings
63             @pop_by_matches = sort { $b->{'_matches'} <=> $a->{'_matches'} } @$pop;
64             $best = $pop_by_matches[0];
65             } while ( $best->{'_matches'} < $rules );
66             return $self->{'_last'} = $best->{'_str'};
67             }
68              
69             }
70              
71             "some blacks, 0 white"; # Magic true value required at end of module
72              
73             __END__