Pro/Chromosome.pm

filter: Statementstmt 98.4% 64 / 65 Branchbran 76.3% 29 / 38 Conditioncond 57.1% 12 / 21 MC/DCmcdc 20.0% 2 / 10 Subroutinesub 100% 11 / 11 Podpod 0.0% 0 / 3 totaltotal 79.7% 118 / 148 CC 28 SCAR 23.6
CC
28
Cov
85%
new_from_data
19.8
new
6.0
__ANON__
5.9
CRAP 10.6
« Pro/Selection/RouletteBasic.pm ↑ Summary Pro/Selection/Roulette.pm »
1 package AI::Genetic::Pro::Chromosome;
2  
3 460 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 460 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 460 ▶use List::Util qw(shuffle first);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 460 ▶use List::MoreUtils qw(first_index);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 460 ▶use Tie::Array::Packed;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 #use Math::Random qw(random_uniform_integer);
9 #=======================================================================
10 sub new {
11 2454 ▶ my ($class, $data, $type, $package, $length) = @_;
Subroutine new: called CC=6 SCAR=17.9
Pod undocumented
12  
13 2454 my @genes;
14 2454 ▶ tie @genes, $package if $package;
Branch: 1 branch
BranchTrueFalseTotal
if $package 1837 617 2454
15
16 2454 ▶ if($type eq q/bitvector/){
Branch: 3 branches
BranchTrueFalseTotal
if ($type eq 'bitvector') { } 1754 700 2454
elsif ($type eq 'combination') { } 100 600 700
elsif ($type eq 'rangevector') { } 300 300 600
17 #@genes = random_uniform_integer(scalar @$data, 0, 1); # this is fastest, but uses more memory
18 1754 ▶ @genes = map { rand > 0.5 ? 1 : 0 } 0..$length; # this is faster
Branch: 1 branch
BranchTrueFalseTotal
rand > 0.5 ? : 18387 22830 41217
19 #@genes = split(q//, unpack("b*", rand 99999), $#$data + 1); # slow
20 }elsif($type eq q/combination/){
21 #@genes = shuffle 0..$#{$data->[0]};
22 100 @genes = shuffle 0..$length;
23 }elsif($type eq q/rangevector/){
24 300 @genes = map { $_->[1] + int rand($_->[2] - $_->[1] + 1) } @$data[0..$length];
25 }else{
26 300 @genes = map { 1 + int(rand( $#{ $data->[$_] })) } 0..$length;
27 }
28  
29 2454 return bless \@genes, $class;
30 }
31 #=======================================================================
32 sub new_from_data {
33 13499 ▶ my ($class, $data, $type, $package, $values, $fix_range) = @_;
Subroutine new_from_data: called CC=16 SCAR=29.9
Pod undocumented
34  
35 13499 ▶ die qq/\nToo many elements in the injected chromosome of type "$type": @$values\n/ if $#$values > $#$data;
Branch: 1 branch
BranchTrueFalseTotal
if $values->$#* > $data->$#* 0 13499 13499
36  
37 13499 my @genes;
38 13499 ▶ tie @genes, $package if $package;
Branch: 1 branch
BranchTrueFalseTotal
if $package 11135 2364 13499
39
40 13499 ▶ if($type eq q/bitvector/){
Branch: 3 branches
BranchTrueFalseTotal
if ($type eq 'bitvector') { } 4795 8704 13499
elsif ($type eq 'combination') { } 1024 7680 8704
elsif ($type eq 'rangevector') { } 3840 3840 7680
41 die qq/\nInproper value in the injected chromosome of type "$type": @$values\n/
42 4795 ▶ if first { not defined $_ or ($_ != 0 and $_ != 1) } @$values;
Subroutine __ANON__: called CC=3 SCAR=13.0
Branch: 2 branches
BranchTrueFalseTotal
if &first(sub { not defined $_ or $_ != 0 and $_ != 1; } , @$values) 0 4795 4795
not defined $_ or $_ != 0 and $_ != 1 153440 0 153440
Condition50%3/6
!defined($_) || $_ != 0 && $_ != 1
l!l&&r!l&&!r
00153440
$_ != 0 and $_ != 1
!ll&&!rl&&r
385301149100
MC/DC0%0/3
!defined($_) || $_ != 0 && $_ != 1
!defined($_) $_ != 0 $_ != 1
Truth table50%2/4
not(A) || B and C
A= !defined($_)B= $_ != 0C= $_ != 1
ABCresult
1XX1
0111
00X0
0100
43 4795 @genes = @$values;
44 }elsif($type eq q/combination/){
45 die qq/\nToo few elements in the injected chromosome of type "$type": @$values\n/
46 1024 ▶ if $#$values != $#{$data->[0]};
Branch: 1 branch
BranchTrueFalseTotal
if $values->$#* != $#{$$data[0];} 0 1024 1024
47 1024 for my $idx(0..$#$values){
48 8192 ▶ my $id = first_index { $_ eq $values->[$idx] } @{$data->[0]}; # pomijamy poczatkowy undef
Subroutine __ANON__: called CC=1 SCAR=0.0
49 8192 ▶ die qq/\nInproper element in the injected chromosome of type "$type": @$values\n/ if $id == -1;
Branch: 1 branch
BranchTrueFalseTotal
if $id == -1 0 8192 8192
50 8192 push @genes, $id;
51 }
52 }elsif($type eq q/rangevector/){
53 3840 for my $idx(0..$#$values){
54 30720 ▶ if(defined $values->[$idx]){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $values->[$idx]) { } 30720 0 30720
55 30720 my $min = $data->[$idx]->[1] - $fix_range->[$idx];
56 30720 my $max = $data->[$idx]->[2] - $fix_range->[$idx];
57 30720 ▶ die qq/\nValue out of scope in the injected chromosome of type "$type": @$values\n/
Branch: 1 branch
BranchTrueFalseTotal
if $values->[$idx] > $max or $values->[$idx] < $min 0 30720 30720
Condition33%1/3
$values->[$idx] > $max or $values->[$idx] < $min
l!l&&r!l&&!r
0030720
MC/DC0%0/2
$values->[$idx] > $max or $values->[$idx] < $min
$values->[$idx] > $max $values->[$idx] < $min
Truth table33%1/3
A or B
A= $values->[$idx] > $maxB= $values->[$idx] < $min
ABresult
1X1
011
000
58 if $values->[$idx] > $max or $values->[$idx] < $min;
59 30720 push @genes, $values->[$idx] + $fix_range->[$idx];
60 0 }else{ push @genes, 0; }
61 }
62 }else{
63 3840 for my $idx(0..$#$values){
64 my $id = first_index {
65 245760 ▶ not defined $values->[$idx] and not defined $_ or
Subroutine __ANON__: called CC=5 SCAR=17.8
Branch: 1 branch
BranchTrueFalseTotal
not defined $values->[$idx] || defined $_ or defined $_ and defined $values->[$idx] and $_ eq $values->[$idx] 245760 0 245760
Condition66%8/12
!(defined $values->[$idx] || defined $_) || defined $_ && defined $values->[$idx] && $_ eq $values->[$idx]
l!l&&r!l&&!r
030720215040
defined $values->[$idx] || defined $_
l!l&&r!l&&!r
24576000
defined $_ and defined $values->[$idx] and $_ eq $values->[$idx]
!ll&&!rl&&r
3072018432030720
defined $_ and defined $values->[$idx]
!ll&&!rl&&r
307200215040
MC/DC40%2/5
!(defined $values->[$idx] || defined $_) || defined $_ && defined $values->[$idx] && $_ eq $values->[$idx]
defined $values->[$idx] defined $_ defined $_ defined $values->[$idx] $_ eq $values->[$idx]
Truth table33%3/9
not(A || B) || C and D and E
A= defined $values->[$idx]B= defined $_C= defined $_D= defined $values->[$idx]E= $_ eq $values->[$idx]
ABCDEresult
00XXX1
1X1111
011111
1X0XX0
1X10X0
1X1100
010XX0
0110X0
011100
66 defined $_ and defined $values->[$idx] and $_ eq $values->[$idx]
67 30720 } @{$data->[$idx]}; # pomijamy poczatkowy undef
68 30720 ▶ die qq/\nInproper element in the injected chromosome of type "$type": @$values\n/ if $id == -1;
Branch: 1 branch
BranchTrueFalseTotal
if $id == -1 0 30720 30720
69 30720 push @genes, $id;
70 }
71 }
72
73 13499 return bless \@genes, $class;
74 }
75 #=======================================================================
76 sub clone
77 {
78 37047 ▶ my ( $self ) = @_;
Subroutine clone: called CC=2 SCAR=6.9
Pod undocumented
79
80 37047 my $cln;
81 37047 ▶ if( my $obj = tied( @$self ) ){
Branch: 1 branch
BranchTrueFalseTotal
if (my $obj = tied @$self) { } 33334 3713 37047
82 33334 $cln = $obj->make_clone;
83 }else{
84 3713 @$cln = @$self;
85 }
86
87 37047 return bless( $cln );
88
89 #my $genes = tied(@{$self})->make_clone;
90 #return bless($genes);
91 }
92  
93 #=======================================================================
94 460 1;
« Pro/Selection/RouletteBasic.pm ↑ Summary Pro/Selection/Roulette.pm »