Pro/Crossover/Points.pm

filter: Statementstmt 93.8% 46 / 49 Branchbran 85.7% 12 / 14 Conditioncond 100% 3 / 3 MC/DCmcdc 100% 2 / 2 Subroutinesub 100% 6 / 6 Podpod 0.0% 0 / 2 totaltotal 90.7% 69 / 76 CC 11 SCAR 19.2
CC
11
Cov
92%
run
10.0
new
2.1
BEGIN
1.0
CRAP 6.8
« Pro/Selection/Roulette.pm ↑ Summary Pro/Crossover/PMX.pm »
1 package AI::Genetic::Pro::Crossover::Points;
2  
3 400 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 400 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 400 ▶use List::MoreUtils qw(first_index);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 #use Data::Dumper; $Data::Dumper::Sortkeys = 1;
7 #=======================================================================
8 400 ▶sub new { bless { points => $_[1] ? $_[1] : 1 }, $_[0]; }
Subroutine new: called CC=2 SCAR=7.6
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
$_[1] ? : 400 0 400
9 #=======================================================================
10 sub run {
11 414 ▶ my ($self, $ga) = @_;
Subroutine run: called CC=10 SCAR=23.1
Pod undocumented
12
13 414 my ($chromosomes, $parents, $crossover) = ($ga->chromosomes, $ga->_parents, $ga->crossover);
14 414 my ($fitness, $_fitness) = ($ga->fitness, $ga->_fitness);
15 414 my @children;
16 #-------------------------------------------------------------------
17 414 ▶ while(my $elders = shift @$parents){
Branch: 1 branch
BranchTrueFalseTotal
if (my $elders = shift @$parents) 18368 414 18782
18 18368 my @elders = unpack 'I*', $elders;
19
20 18368 ▶ unless(scalar @elders){
Branch: 1 branch
BranchTrueFalseTotal
unless (scalar @elders) 0 18368 18368
21 0 $_fitness->{scalar(@children)} = $fitness->($ga, $chromosomes->[$elders[0]]);
22 0 push @children, $chromosomes->[$elders[0]];
23 0 next;
24 }
25  
26 18368 my ($min, $max) = (0, $#{$chromosomes->[0]});
27 18368 ▶ if($ga->variable_length){
Branch: 1 branch
BranchTrueFalseTotal
if ($ga->variable_length) 11600 6768 18368
28 11600 for my $el(@elders){
29 22009 ▶ my $idx = first_index { $_ } @{$chromosomes->[$el]};
Subroutine __ANON__: called CC=1 SCAR=0.0
30 22009 ▶ $min = $idx if $idx > $min;
Branch: 1 branch
BranchTrueFalseTotal
if $idx > $min 306 21703 22009
31 22009 ▶ $max = $#{$chromosomes->[$el]} if $#{$chromosomes->[$el]} < $max;
Branch: 1 branch
BranchTrueFalseTotal
if $#{$$chromosomes[$el];} < $max 333 21676 22009
32 }
33 }
34
35 18368 my @points;
36 18368 ▶ if($min < $max and $max - $min > 2){
Branch: 1 branch
BranchTrueFalseTotal
if ($min < $max and $max - $min > 2) 14193 4175 18368
Condition100%3/3
$min < $max and $max - $min > 2
!ll&&!rl&&r
5417014193
MC/DC100%2/2
$min < $max and $max - $min > 2
$min < $max $max - $min > 2
Truth table100%3/3
A and B
A= $min < $maxB= $max - $min > 2
ABresult
0X0
100
111
37 14193 my $range = $max - $min;
38 14193 @points = map { $min + int(rand $range) } 1..$self->{points};
39 }
40  
41 18368 @elders = map { $chromosomes->[$_]->clone } @elders;
42
43 18368 for my $pt(@points){
44 @elders = sort {
45 28386 splice @$b, 0, $pt, splice( @$a, 0, $pt, @$b[0..$pt-1] );
46 0;
47 } @elders;
48 }
49
50 18368 my %elders = map { $_ => $fitness->($ga, $elders[$_]) } 0..$#elders;
51 18368 my $maximum = (sort { $elders{$a} <=> $elders{$b} } keys %elders)[-1];
52 18368 $_fitness->{scalar(@children)} = $elders{$maximum};
53
54 18368 push @children, $elders[$maximum];
55 }
56 #-------------------------------------------------------------------
57 414 return \@children;
58 }
59 #=======================================================================
60 400 1;
« Pro/Selection/Roulette.pm ↑ Summary Pro/Crossover/PMX.pm »