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 401 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 401 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 #use Data::Dumper; $Data::Dumper::Sortkeys = 1;
6 401 ▶use List::Util qw(sum min);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 401 ▶use List::MoreUtils qw(first_index);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 401 ▶use Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 #=======================================================================
11 401 ▶sub new { bless \$_[0], $_[0]; }
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
12 #=======================================================================
13 sub run {
14 415 ▶ my ($self, $ga) = @_;
Subroutine run: called CC=13 SCAR=25.7
Pod undocumented
15
16 415 my ($fitness) = ($ga->_fitness);
17 415 my (@parents, @elders);
18 #-------------------------------------------------------------------
19 415 my $count = $#{$ga->chromosomes};
20 415 my $const = min values %$fitness;
21 415 ▶ $const = $const < 0 ? abs($const) : 0;
Branch: 1 branch
BranchTrueFalseTotal
$const < 0 ? : 8 407 415
22 415 ▶ my $total = sum( map { $_ < 0 ? $_ + $const : $_ } values %$fitness);
Branch: 1 branch
BranchTrueFalseTotal
$_ < 0 ? : 303 19189 19492
23 415 ▶ $total ||= 1;
Condition50%1/2
$total ||= 1
l!l
4150
MC/DC0%0/1
$total ||= 1
$total
Truth table50%1/2
A
A= $total
Aresult
11
00
24
25 # elders
26 415 for my $idx (0..$count){
27 19492 push @elders, $idx for 1..int((($fitness->{$idx} + $const) / $total) * $count);
28 }
29
30 415 ▶ if((my $add = $count - scalar @elders) > 0){
Branch: 1 branch
BranchTrueFalseTotal
if ((my $add = $count - scalar(@elders)) > 0) 410 5 415
31 410 my $idx = $elders[rand @elders];
32 410 push @elders, int rand($count) for 0..$add;
33 }
34
35 415 ▶ croak "You must set a crossover probability to use the Roulette strategy"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ga->crossover 0 415 415
36 unless defined($ga->crossover);
37 415 ▶ croak "You must set a number of parents to use the Roulette strategy"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ga->parents 0 415 415
38 unless defined($ga->parents);
39  
40 # parents
41 415 for(0..$count){
42 19492 ▶ if(rand > $ga->crossover){
Branch: 1 branch
BranchTrueFalseTotal
if (rand > $ga->crossover) { } 1937 17555 19492
43 1937 push @parents, pack 'I*', $elders[ rand @elders ]
44 }else{
45 17555 my @group;
46 17555 push @group, $elders[ rand @elders ] for 1..$ga->parents;
47 17555 push @parents, pack 'I*', @group;
48 }
49 }
50  
51 #-------------------------------------------------------------------
52 415 return \@parents;
53 }
54 #=======================================================================
55  
56 401 1;
« Pro/Chromosome.pm ↑ Summary Pro/Crossover/Points.pm »