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 365 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 365 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 365 ▶use List::MoreUtils qw(first_index);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 #use Data::Dumper; $Data::Dumper::Sortkeys = 1;
7 #=======================================================================
8 374 ▶sub new { bless { points => $_[1] ? $_[1] : 1 }, $_[0]; }
Subroutine new: called CC=2 SCAR=7.6
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
$_[1] ? : 374 0 374
9 #=======================================================================
10 sub run {
11 386 ▶ my ($self, $ga) = @_;
Subroutine run: called CC=10 SCAR=23.1
Pod undocumented
12
13 386 my ($chromosomes, $parents, $crossover) = ($ga->chromosomes, $ga->_parents, $ga->crossover);
14 386 my ($fitness, $_fitness) = ($ga->fitness, $ga->_fitness);
15 386 my @children;
16 #-------------------------------------------------------------------
17 386 ▶ while(my $elders = shift @$parents){
Branch: 1 branch
BranchTrueFalseTotal
if (my $elders = shift @$parents) 15408 386 15794
18 15408 my @elders = unpack 'I*', $elders;
19
20 15408 ▶ unless(scalar @elders){
Branch: 1 branch
BranchTrueFalseTotal
unless (scalar @elders) 0 15408 15408
21 0 $_fitness->{scalar(@children)} = $fitness->($ga, $chromosomes->[$elders[0]]);
22 0 push @children, $chromosomes->[$elders[0]];
23 0 next;
24 }
25  
26 15408 my ($min, $max) = (0, $#{$chromosomes->[0]});
27 15408 ▶ if($ga->variable_length){
Branch: 1 branch
BranchTrueFalseTotal
if ($ga->variable_length) 8952 6456 15408
28 8952 for my $el(@elders){
29 17017 ▶ my $idx = first_index { $_ } @{$chromosomes->[$el]};
Subroutine __ANON__: called CC=1 SCAR=0.0
30 17017 ▶ $min = $idx if $idx > $min;
Branch: 1 branch
BranchTrueFalseTotal
if $idx > $min 422 16595 17017
31 17017 ▶ $max = $#{$chromosomes->[$el]} if $#{$chromosomes->[$el]} < $max;
Branch: 1 branch
BranchTrueFalseTotal
if $#{$$chromosomes[$el];} < $max 382 16635 17017
32 }
33 }
34
35 15408 my @points;
36 15408 ▶ if($min < $max and $max - $min > 2){
Branch: 1 branch
BranchTrueFalseTotal
if ($min < $max and $max - $min > 2) 12522 2886 15408
Condition100%3/3
$min < $max and $max - $min > 2
!ll&&!rl&&r
61282512522
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 12522 my $range = $max - $min;
38 12522 @points = map { $min + int(rand $range) } 1..$self->{points};
39 }
40  
41 15408 @elders = map { $chromosomes->[$_]->clone } @elders;
42
43 15408 for my $pt(@points){
44 @elders = sort {
45 25044 splice @$b, 0, $pt, splice( @$a, 0, $pt, @$b[0..$pt-1] );
46 0;
47 } @elders;
48 }
49
50 15408 my %elders = map { $_ => $fitness->($ga, $elders[$_]) } 0..$#elders;
51 15408 my $maximum = (sort { $elders{$a} <=> $elders{$b} } keys %elders)[-1];
52 15408 $_fitness->{scalar(@children)} = $elders{$maximum};
53
54 15408 push @children, $elders[$maximum];
55 }
56 #-------------------------------------------------------------------
57 386 return \@children;
58 }
59 #=======================================================================
60 365 1;
« Pro/Selection/Roulette.pm ↑ Summary Pro/Crossover/PMX.pm »