Pro/Crossover/PMX.pm

filter: Statementstmt 96.7% 60 / 62 Branchbran 93.7% 15 / 16 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 8 / 8 Podpod 0.0% 0 / 3 totaltotal 93.2% 83 / 89 CC 11 SCAR 16.8
CC
11
Cov
96%
run
9.0
dup
3.0
BEGIN
1.0
CRAP 5.3
« Pro/Crossover/Points.pm ↑ Summary Pro/Mutation/Combination.pm »
1 package AI::Genetic::Pro::Crossover::PMX;
2  
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use List::MoreUtils qw(indexes);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 #use Data::Dumper; $Data::Dumper::Sortkeys = 1;
7 #=======================================================================
8 1 ▶sub new { bless \$_[0], $_[0]; }
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
9 #=======================================================================
10 sub dup {
11 3166 ▶ my ($ar) = @_;
Subroutine dup: called CC=3 SCAR=11.0
Pod undocumented
12  
13 3166 my %seen;
14 3166 ▶ my @dup = grep { if($seen{$_}){ 1 }else{ $seen{$_} = 1; 0} } @$ar;
Branch: 1 branch
BranchTrueFalseTotal
if ($seen{$_}) { } 1378 23950 25328
15 3166 ▶ return \@dup if @dup;
Branch: 1 branch
BranchTrueFalseTotal
if @dup 1112 2054 3166
16 2054 return;
17 }
18 #=======================================================================
19 sub run {
20 1 ▶ my ($self, $ga) = @_;
Subroutine run: called CC=9 SCAR=22.0
Pod undocumented
21
22 1 my ($chromosomes, $parents, $crossover) = ($ga->chromosomes, $ga->_parents, $ga->crossover);
23 1 my ($fitness, $_fitness) = ($ga->fitness, $ga->_fitness);
24 1 my @children;
25 #-------------------------------------------------------------------
26 1 ▶ while(my $elders = shift @$parents){
Branch: 1 branch
BranchTrueFalseTotal
if (my $elders = shift @$parents) 1124 1 1125
27 1124 my @elders = unpack 'I*', $elders;
28
29 1124 ▶ unless(scalar @elders){
Branch: 1 branch
BranchTrueFalseTotal
unless (scalar @elders) 0 1124 1124
30 0 push @children, $chromosomes->[$elders[0]];
31 0 next;
32 }
33
34 1124 my @points = sort { $a <=> $b } map { 1 + int(rand $#{$chromosomes->[0]}) } 0..1;
35
36 @elders = sort {
37 1027 my @av = @{$a}[$points[0]..$points[1]-1];
38 1027 my @bv = splice @$b, $points[0], $points[1] - $points[0], @av;
39 1027 splice @$a, $points[0], $points[1] - $points[0], @bv;
40
41 1027 my %av; @av{@av} = @bv;
42 1027 my %bv; @bv{@bv} = @av;
43  
44 1027 ▶ while(my $dup = dup($a)){
Branch: 1 branch
BranchTrueFalseTotal
if (my $dup = dup($a)) 556 1027 1583
45 556 foreach my $val (@$dup){
46 689 ▶ my ($ind) = grep { $_ < $points[0] or $_ >= $points[1] } indexes { $_ == $val } @$a;
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
$_ < $points[0] or $_ >= $points[1] 1029 349 1378
47 689 $a->[$ind] = $bv{$val};
48 }
49 }
50  
51 1027 ▶ while(my $dup = dup($b)){
Branch: 1 branch
BranchTrueFalseTotal
if (my $dup = dup($b)) 556 1027 1583
52 556 foreach my $val (@$dup){
53 689 ▶ my ($ind) = grep { $_ < $points[0] or $_ >= $points[1] } indexes { $_ == $val } @$b;
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
$_ < $points[0] or $_ >= $points[1] 1055 323 1378
54 689 $b->[$ind] = $av{$val};
55 }
56 }
57
58 0;
59 } map {
60 1124 $chromosomes->[$_]->clone
61 } @elders;
62
63
64 1124 my %elders = map { $_ => $fitness->($ga, $elders[$_]) } 0..$#elders;
65 1124 my $max = (sort { $elders{$a} <=> $elders{$b} } keys %elders)[-1];
66 1124 $_fitness->{scalar(@children)} = $elders{$max};
67
68 1124 push @children, $elders[$max];
69 }
70 #-------------------------------------------------------------------
71
72 1 return \@children;
73 }
74 #=======================================================================
75 1 1;
« Pro/Crossover/Points.pm ↑ Summary Pro/Mutation/Combination.pm »