File Coverage

lib/Algorithm/MasterMind/CGA_Partitions.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::CGA_Partitions;
2              
3 1     1   828 use warnings;
  1         3  
  1         40  
4 1     1   5 use strict;
  1         2  
  1         40  
5 1     1   6 use Carp;
  1         1  
  1         76  
6              
7 1         6 use lib qw(../../lib
8             ../../../lib
9             ../../../../Algorithm-Evolutionary/lib
10             ../../../Algorithm-Evolutionary/lib
11 1     1   5 ../../Algorithm-Evolutionary/lib);
  1         1  
12              
13             our $VERSION = sprintf "%d.%03d", q$Revision: 1.3 $ =~ /(\d+)\.(\d+)/g;
14              
15 1     1   389 use base 'Algorithm::MasterMind::Canonical_GA';
  1         2  
  1         553  
16              
17             use Algorithm::MasterMind qw(partitions);
18              
19              
20             sub issue_next {
21             my $self = shift;
22             my $rules = $self->number_of_rules();
23             my @alphabet = @{$self->{'_alphabet'}};
24             my $length = $self->{'_length'};
25             my $pop = $self->{'_pop'};
26             my $cga = $self->{'_ga'};
27             map( $_->evaluate( $self->{'_fitness'}), @$pop );
28             my @ranked_pop = sort { $b->{_fitness} <=> $a->{_fitness}; } @$pop;
29              
30             my %consistent;
31             # print "Consistent in ", scalar keys %{$self->{'_consistent'}}, "\n";
32             if ( $self->{'_consistent'} ) { #Check for consistency
33             %consistent = %{$self->{'_consistent'}};
34             for my $c (keys %consistent ) {
35             my $match = $self->matches( $c );
36             if ( $match->{'matches'} < $rules ) {
37             delete $consistent{$c};
38             }
39             }
40             } else {
41             %consistent = ();
42             }
43             # print "Consistent out ", scalar keys %consistent, "\n";
44            
45             while ( $ranked_pop[0]->{'_matches'} == $rules ) {
46             $consistent{$ranked_pop[0]->{'_str'}} = $ranked_pop[0];
47             shift @ranked_pop;
48             }
49             my $generations_equal = 0;
50             # The 20 was computed in NICSO paper, valid for normal mastermind
51             my $number_of_consistent = keys %consistent;
52            
53             # print "Consistent new ", scalar keys %consistent, "\n";
54             while ( $number_of_consistent < 20 ) {
55             my $this_number_of_consistent = $number_of_consistent;
56             $cga->apply( $pop );
57             for my $p( @$pop ) {
58             my $matches = $self->matches( $p->{'_str'} );
59             # print "* ", $p->{'_str'}, " ", $matches->{'matches'}, "\n";
60             if ( $matches->{'matches'} == $rules ) {
61             # print "Combination ", $p->{'_str'}, " matches ", $p->{'_matches'}, "\n";
62             $consistent{$p->{'_str'}} = $p;
63             }
64             }
65             $number_of_consistent = keys %consistent;
66             if ( $this_number_of_consistent == $number_of_consistent ) {
67             $generations_equal++;
68             } else {
69             $generations_equal = 0;
70             }
71            
72             # print "G $generations_equal $number_of_consistent \n";
73             last if ( ( $generations_equal >= 3 ) && ( $number_of_consistent >= 1 ) );
74             }
75              
76             # print "After GA combinations ", join( " ", keys %consistent ), "\n";
77             $self->{'_consistent'} = \%consistent; #This mainly for outside info
78             if ( $number_of_consistent > 1 ) {
79             # print "Consistent ", scalar keys %consistent, "\n";
80             #Use whatever we've got to compute number of partitions
81             my $partitions = partitions( keys %consistent );
82            
83             my $max_partitions = 0;
84             my %max_c;
85             for my $c ( keys %$partitions ) {
86             my $this_max = keys %{$partitions->{$c}};
87             $max_c{$c} = $this_max;
88             if ( $this_max > $max_partitions ) {
89             $max_partitions = $this_max;
90             }
91             }
92             # Find all partitions with that max
93             my @max_c = grep( $max_c{$_} == $max_partitions, keys %max_c );
94             # Break ties
95             my $string = $max_c[ rand( @max_c )];
96             # Obtain next
97             return $self->{'_last'} = $string;
98             } else {
99             return $self->{'_last'} = (keys %consistent)[0];
100             }
101            
102             }
103              
104             "Many blacks, 0 white"; # Magic true value required at end of module
105              
106             __END__