Pro/Selection/Roulette.pm

filter: Statementstmt 100% 41 / 41 Branchbran 83.3% 10 / 12 Conditioncond 50.0% 1 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 100% 7 / 7 Podpod 0.0% 0 / 2 totaltotal 90.7% 59 / 65 CC 13 SCAR 22.3
CC
13
Cov
95%
run
13.1
BEGIN
1.0
BEGIN
1.0
CRAP 9.3
« Pro/Chromosome.pm ↑ Summary Pro/Crossover/Points.pm »
1 package AI::Genetic::Pro::Selection::Roulette;
2  
3 366 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 366 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 #use Data::Dumper; $Data::Dumper::Sortkeys = 1;
6 366 ▶use List::Util qw(sum min);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 366 ▶use List::MoreUtils qw(first_index);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 366 ▶use Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 #=======================================================================
11 375 ▶sub new { bless \$_[0], $_[0]; }
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
12 #=======================================================================
13 sub run {
14 387 ▶ my ($self, $ga) = @_;
Subroutine run: called CC=13 SCAR=25.7
Pod undocumented
15
16 387 my ($fitness) = ($ga->_fitness);
17 387 my (@parents, @elders);
18 #-------------------------------------------------------------------
19 387 my $count = $#{$ga->chromosomes};
20 387 my $const = min values %$fitness;
21 387 ▶ $const = $const < 0 ? abs($const) : 0;
Branch: 1 branch
BranchTrueFalseTotal
$const < 0 ? : 6 381 387
22 387 ▶ my $total = sum( map { $_ < 0 ? $_ + $const : $_ } values %$fitness);
Branch: 1 branch
BranchTrueFalseTotal
$_ < 0 ? : 251 16281 16532
23 387 ▶ $total ||= 1;
Condition50%1/2
$total ||= 1
l!l
3870
MC/DC0%0/1
$total ||= 1
$total
Truth table50%1/2
A
A= $total
Aresult
11
00
24
25 # elders
26 387 for my $idx (0..$count){
27 16532 push @elders, $idx for 1..int((($fitness->{$idx} + $const) / $total) * $count);
28 }
29
30 387 ▶ if((my $add = $count - scalar @elders) > 0){
Branch: 1 branch
BranchTrueFalseTotal
if ((my $add = $count - scalar(@elders)) > 0) 382 5 387
31 382 my $idx = $elders[rand @elders];
32 382 push @elders, int rand($count) for 0..$add;
33 }
34
35 387 ▶ croak "You must set a crossover probability to use the Roulette strategy"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ga->crossover 0 387 387
36 unless defined($ga->crossover);
37 387 ▶ croak "You must set a number of parents to use the Roulette strategy"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ga->parents 0 387 387
38 unless defined($ga->parents);
39  
40 # parents
41 387 for(0..$count){
42 16532 ▶ if(rand > $ga->crossover){
Branch: 1 branch
BranchTrueFalseTotal
if (rand > $ga->crossover) { } 1643 14889 16532
43 1643 push @parents, pack 'I*', $elders[ rand @elders ]
44 }else{
45 14889 my @group;
46 14889 push @group, $elders[ rand @elders ] for 1..$ga->parents;
47 14889 push @parents, pack 'I*', @group;
48 }
49 }
50  
51 #-------------------------------------------------------------------
52 387 return \@parents;
53 }
54 #=======================================================================
55  
56 366 1;
« Pro/Chromosome.pm ↑ Summary Pro/Crossover/Points.pm »