File Coverage

blib/lib/AI/Genetic/Pro/Mutation/Listvector.pm
Criterion Covered Total %
statement 38 46 82.6
branch 9 14 64.2
condition 7 15 46.6
subroutine 6 6 100.0
pod 0 2 0.0
total 60 83 72.2


line stmt bran cond sub pod time code
1             package AI::Genetic::Pro::Mutation::Listvector;
2              
3 3     3   20 use warnings;
  3         7  
  3         125  
4 3     3   18 use strict;
  3         8  
  3         130  
5 3     3   18 use List::MoreUtils qw(first_index);
  3         7  
  3         1878  
6             #use Data::Dumper; $Data::Dumper::Sortkeys = 1;
7             #=======================================================================
8 3     3 0 33 sub new { bless \$_[0], $_[0]; }
9             #=======================================================================
10             sub run {
11 4     4 0 10 my ($self, $ga) = @_;
12              
13             # this is declared here just for speed
14 4         20 my $mutation = $ga->mutation;
15 4         19 my $chromosomes = $ga->chromosomes;
16 4         21 my $_translations = $ga->_translations;
17 4         35 my ($fitness, $_fitness) = ($ga->fitness, $ga->_fitness);
18            
19             # main loop
20 4         17 for my $idx (0..$#$chromosomes){
21 5520 100       9977 next if rand() >= $mutation;
22              
23 184 100       528 if($ga->variable_length){
24 108         166 my $rand = rand();
25 108     108   406 my $min = first_index { $_ } @{$chromosomes->[$idx]};
  108         208  
  108         2094  
26 108         555 my $range = $#{$chromosomes->[$idx]} - $min + 1;
  108         377  
27            
28 108 100 66     698 if($rand < 0.4 and $range > 2){
    50 66        
29 45 100 100     187 if($rand < 0.2 and $ga->variable_length > 1){ $chromosomes->[$idx]->[$min] = 0; }
  6         26  
30 39         44 else{ pop @{$chromosomes->[$idx]}; }
  39         151  
31             }elsif($rand < 0.8 and $range < $#$_translations){
32 0 0 0     0 if($rand < 0.6 and $ga->variable_length > 1 and not $chromosomes->[$idx]->[0]){
  0 0 0     0  
33 0         0 $chromosomes->[$idx]->[ $min - 1 ] = 1 + int rand $#{$_translations->[ $min - 1 ]};
  0         0  
34             }elsif(exists $_translations->[scalar @{$chromosomes->[$idx]}]){
35 0         0 push @{$chromosomes->[$idx]}, 1 + int rand $#{$_translations->[scalar @{$chromosomes->[$idx]}]};
  0         0  
  0         0  
  0         0  
36             }
37             }else{
38 63         130 my $id = $min + int rand($range - 1);
39 63         69 $chromosomes->[$idx]->[$id] = 1 + int rand $#{$_translations->[$id]};
  63         330  
40             }
41             }else{
42 76         72 my $id = int rand @{$chromosomes->[$idx]};
  76         281  
43 76         90 $chromosomes->[$idx]->[$id] = 1 + int rand $#{$_translations->[$id]};
  76         318  
44             }
45            
46             # we need to change fitness
47 184         684 $_fitness->{$idx} = $fitness->($ga, $chromosomes->[$idx]);
48             }
49            
50 4         32 return 1;
51             }
52             #=======================================================================
53             1;