Pro/Mutation/Bitvector.pm

filter: Statementstmt 79.1% 38 / 48 Branchbran 71.4% 20 / 28 Conditioncond 83.3% 10 / 12 MC/DCmcdc 75.0% 6 / 8 Subroutinesub 80.0% 4 / 5 Podpod 0.0% 0 / 3 totaltotal 75.0% 78 / 104 CC 22 SCAR 30.4
CC
22
Cov
77%
run0
42.0
run
17.1
BEGIN
1.0
CRAP 21.0
« Pro/Crossover/PointsSimple.pm ↑ Summary Pro/Array/Type.pm »
1 package AI::Genetic::Pro::Mutation::Bitvector;
2  
3 394 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 394 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 #use Data::Dumper; $Data::Dumper::Sortkeys = 1;
6 #=======================================================================
7 394 ▶sub new { bless \$_[0], $_[0]; }
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
8 #=======================================================================
9 sub run {
10 404 ▶ my ($self, $ga) = @_;
Subroutine run: called CC=17 SCAR=28.4
Pod undocumented
11  
12 # this is declared here just for speed
13 404 my $mutation = $ga->mutation;
14 404 my $chromosomes = $ga->chromosomes;
15 404 my $_translations = $ga->_translations;
16 404 my ($fitness, $_fitness) = ($ga->fitness, $ga->_fitness);
17
18 # main loop
19 404 for my $idx (0..$#$chromosomes){
20 4568 ▶ next if rand() >= $mutation;
Branch: 1 branch
BranchTrueFalseTotal
if rand() >= $mutation 4335 233 4568
21
22 233 ▶ if($ga->variable_length){
Branch: 1 branch
BranchTrueFalseTotal
if ($ga->variable_length) { } 29 204 233
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) { } 3 26 29
elsif ($rand < 0.32 and $#{$$chromosomes[$idx];} > 1) { } 9 17 26
elsif ($rand < 0.48 and $#{$$chromosomes[$idx];} < $_translations->$#*) { } 1 16 17
elsif ($rand < 0.64 and $#{$$chromosomes[$idx];} < $_translations->$#*) { } 1 15 16
elsif ($rand < 0.8) { } 10 5 15
Condition66%2/3
$rand < "0.16" and $#{$$chromosomes[$idx];} > 1
!ll&&!rl&&r
2603
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 3 pop @{$chromosomes->[$idx]};
26 9 ▶ }elsif($rand < 0.32 and $#{$chromosomes->[$idx]} > 1){
Condition66%2/3
$rand < "0.32" and $#{$$chromosomes[$idx];} > 1
!ll&&!rl&&r
1709
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 9 shift @{$chromosomes->[$idx]};
28 3 ▶ }elsif($rand < 0.48 and $#{$chromosomes->[$idx]} < $#$_translations){
Condition100%3/3
$rand < "0.48" and $#{$$chromosomes[$idx];} < $_translations->$#*
!ll&&!rl&&r
1421
MC/DC100%2/2
$rand < "0.48" and $#{$$chromosomes[$idx];} < $_translations->$#*
$rand < "0.48" $#{$$chromosomes[$idx];} < $_translations->$#*
Truth table100%3/3
A and B
A= $rand < "0.48"B= $#{$$chromosomes[$idx];} < $_translations->$#*
ABresult
0X0
100
111
29 1 ▶ push @{$chromosomes->[$idx]}, rand > 0.5 ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
rand > 0.5 ? : 1 0 1
30 6 ▶ }elsif($rand < 0.64 and $#{$chromosomes->[$idx]} < $#$_translations){
Condition100%3/3
$rand < "0.64" and $#{$$chromosomes[$idx];} < $_translations->$#*
!ll&&!rl&&r
1051
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 1 ▶ unshift @{$chromosomes->[$idx]}, rand > 0.5 ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
rand > 0.5 ? : 1 0 1
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 5 my $id = int rand @{$chromosomes->[$idx]};
44 5 ▶ $chromosomes->[$idx]->[$id] = $chromosomes->[$idx]->[$id] ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$chromosomes->[$idx][$id] ? : 3 2 5
45 }
46 }else{
47 204 my $id = int rand @{$chromosomes->[$idx]};
48 204 ▶ $chromosomes->[$idx]->[$id] = $chromosomes->[$idx]->[$id] ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$chromosomes->[$idx][$id] ? : 168 36 204
49 }
50 # we need to change fitness
51 233 $_fitness->{$idx} = $fitness->($ga, $chromosomes->[$idx]);
52 }
53
54 404 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 394 1;
« Pro/Crossover/PointsSimple.pm ↑ Summary Pro/Array/Type.pm »