Pro/Mutation/Bitvector.pm

filter: Statementstmt 75.0% 36 / 48 Branchbran 67.8% 19 / 28 Conditioncond 75.0% 9 / 12 MC/DCmcdc 50.0% 4 / 8 Subroutinesub 80.0% 4 / 5 Podpod 0.0% 0 / 3 totaltotal 69.2% 72 / 104 CC 22 SCAR 30.6
CC
22
Cov
73%
run0
42.0
run
17.6
BEGIN
1.0
CRAP 21.3
« Pro/Crossover/PointsSimple.pm ↑ Summary Pro/Array/Type.pm »
1 package AI::Genetic::Pro::Mutation::Bitvector;
2  
3 359 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 359 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 #use Data::Dumper; $Data::Dumper::Sortkeys = 1;
6 #=======================================================================
7 368 ▶sub new { bless \$_[0], $_[0]; }
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
8 #=======================================================================
9 sub run {
10 378 ▶ my ($self, $ga) = @_;
Subroutine run: called CC=17 SCAR=28.7
Pod undocumented
11  
12 # this is declared here just for speed
13 378 my $mutation = $ga->mutation;
14 378 my $chromosomes = $ga->chromosomes;
15 378 my $_translations = $ga->_translations;
16 378 my ($fitness, $_fitness) = ($ga->fitness, $ga->_fitness);
17
18 # main loop
19 378 for my $idx (0..$#$chromosomes){
20 4368 ▶ next if rand() >= $mutation;
Branch: 1 branch
BranchTrueFalseTotal
if rand() >= $mutation 4133 235 4368
21
22 235 ▶ if($ga->variable_length){
Branch: 1 branch
BranchTrueFalseTotal
if ($ga->variable_length) { } 29 206 235
23 29 my $rand = rand();
24 29 ▶ if($rand < 0.16 and $#{$chromosomes->[$idx]} > 1){
Branch: 5 branches
BranchTrueFalseTotal
if ($rand < 0.16 and $#{$$chromosomes[$idx];} > 1) { } 6 23 29
elsif ($rand < 0.32 and $#{$$chromosomes[$idx];} > 1) { } 4 19 23
elsif ($rand < 0.48 and $#{$$chromosomes[$idx];} < $_translations->$#*) { } 0 19 19
elsif ($rand < 0.64 and $#{$$chromosomes[$idx];} < $_translations->$#*) { } 3 16 19
elsif ($rand < 0.8) { } 8 8 16
Condition66%2/3
$rand < "0.16" and $#{$$chromosomes[$idx];} > 1
!ll&&!rl&&r
2306
MC/DC50%1/2
$rand < "0.16" and $#{$$chromosomes[$idx];} > 1
$rand < "0.16" $#{$$chromosomes[$idx];} > 1
Truth table66%2/3
A and B
A= $rand < "0.16"B= $#{$$chromosomes[$idx];} > 1
ABresult
0X0
100
111
25 6 pop @{$chromosomes->[$idx]};
26 4 ▶ }elsif($rand < 0.32 and $#{$chromosomes->[$idx]} > 1){
Condition66%2/3
$rand < "0.32" and $#{$$chromosomes[$idx];} > 1
!ll&&!rl&&r
1904
MC/DC50%1/2
$rand < "0.32" and $#{$$chromosomes[$idx];} > 1
$rand < "0.32" $#{$$chromosomes[$idx];} > 1
Truth table66%2/3
A and B
A= $rand < "0.32"B= $#{$$chromosomes[$idx];} > 1
ABresult
0X0
100
111
27 4 shift @{$chromosomes->[$idx]};
28 2 ▶ }elsif($rand < 0.48 and $#{$chromosomes->[$idx]} < $#$_translations){
Condition66%2/3
$rand < "0.48" and $#{$$chromosomes[$idx];} < $_translations->$#*
!ll&&!rl&&r
1720
MC/DC0%0/2
$rand < "0.48" and $#{$$chromosomes[$idx];} < $_translations->$#*
$rand < "0.48" $#{$$chromosomes[$idx];} < $_translations->$#*
Truth table66%2/3
A and B
A= $rand < "0.48"B= $#{$$chromosomes[$idx];} < $_translations->$#*
ABresult
0X0
100
111
29 0 ▶ push @{$chromosomes->[$idx]}, rand > 0.5 ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
rand > 0.5 ? : 0 0 0
30 7 ▶ }elsif($rand < 0.64 and $#{$chromosomes->[$idx]} < $#$_translations){
Condition100%3/3
$rand < "0.64" and $#{$$chromosomes[$idx];} < $_translations->$#*
!ll&&!rl&&r
1243
MC/DC100%2/2
$rand < "0.64" and $#{$$chromosomes[$idx];} < $_translations->$#*
$rand < "0.64" $#{$$chromosomes[$idx];} < $_translations->$#*
Truth table100%3/3
A and B
A= $rand < "0.64"B= $#{$$chromosomes[$idx];} < $_translations->$#*
ABresult
0X0
100
111
31 3 ▶ unshift @{$chromosomes->[$idx]}, rand > 0.5 ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
rand > 0.5 ? : 2 1 3
32 }elsif($rand < 0.8){
33 ##tied(@{$chromosomes->[$idx]})->reverse;
34
35 #if( my $obj = tied( @{ $chromosomes->[ $idx ] } ) ){
36 # $obj->reverse;
37 #}else{
38 # @{ $chromosomes->[ $idx ] } = reverse @{ $chromosomes->[ $idx ] };
39 #}
40 #@{ $chromosomes->[ $idx ] } = reverse @{ $chromosomes->[ $idx ] };
41
42 }else{
43 8 my $id = int rand @{$chromosomes->[$idx]};
44 8 ▶ $chromosomes->[$idx]->[$id] = $chromosomes->[$idx]->[$id] ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$chromosomes->[$idx][$id] ? : 6 2 8
45 }
46 }else{
47 206 my $id = int rand @{$chromosomes->[$idx]};
48 206 ▶ $chromosomes->[$idx]->[$id] = $chromosomes->[$idx]->[$id] ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$chromosomes->[$idx][$id] ? : 152 54 206
49 }
50 # we need to change fitness
51 235 $_fitness->{$idx} = $fitness->($ga, $chromosomes->[$idx]);
52 }
53
54 378 return 1;
55 }
56 #=======================================================================
57 # too slow; mutation is too dangerous in this solution
58 sub run0 {
59 0 ▶ my ($self, $ga) = @_;
Subroutine run0: not called CC=6 SCAR=37.4
Pod undocumented
60  
61 0 my $mutation = $ga->mutation; # this is declared here just for speed
62 0 foreach my $chromosome (@{$ga->{chromosomes}}){
63 0 ▶ if(rand() < $mutation){ tied(@$chromosome)->reverse; }
Branch: 1 branch
BranchTrueFalseTotal
if (rand() < $mutation) { } 0 0 0
64 else{
65 0 for(0..$#$chromosome){
66 0 ▶ next if rand > $mutation;
Branch: 1 branch
BranchTrueFalseTotal
if rand > $mutation 0 0 0
67 0 ▶ $chromosome->[$_] = $chromosome->[$_] ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$chromosome->[$_] ? : 0 0 0
68 }
69 }
70 }
71
72 0 return 1;
73 }
74 #=======================================================================
75 359 1;
« Pro/Crossover/PointsSimple.pm ↑ Summary Pro/Array/Type.pm »