lib/Algorithm/Evolutionary/Wheel.pm

filter: Statementstmt 100% 52 / 52 Branchbran 90.0% 9 / 10 Conditioncond 75.0% 3 / 4 MC/DCmcdc 50.0% 1 / 2 Subroutinesub 100% 6 / 6 Podpod 100% 3 / 3 totaltotal 96.1% 74 / 77 CC 12 SCAR 14.0
CC
12
Cov
97%
new
5.0
spin
5.0
first
4.0
CRAP 4.1
« blib/lib/Algorithm/Evolutionary/Fitness/P_Peaks.pm ↑ Summary blib/lib/Algorithm/Evolutionary/Op/Canonical_GA_NN.pm »
1 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 =head1 NAME
5  
6 Algorithm::Evolutionary::Wheel - Random selector of things depending on probabilities
7  
8 =head1 SYNOPSIS
9  
10 my $wheel = new Algorithm::Evolutionary::Wheel( @probs );
11 print $wheel->spin(); #Returns an element according to probabilities;
12  
13 =head1 DESCRIPTION
14  
15 Creates a "roulette wheel" for spinning and selecting stuff. It will
16 be used in several places; mainly in the
17 L<Algorithm::Evolutionary::Op::CanonicalGA>. Take care that fitness
18 must be non-zero positives; since if they aren't, roulette wheel won't
19 work at all
20  
21 =head1 METHODS
22  
23 =cut
24  
25 package Algorithm::Evolutionary::Wheel;
26 4 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 4 our ($VERSION) = ( '$Revision: 3.6 $ ' =~ / (\d+\.\d+)/ ) ;
29  
30 =head2 new( @probabilites )
31  
32 Creates a new roulette wheel. Takes an array of numbers, which need not be
33 normalized
34  
35 =cut
36  
37 1 ▶sub new {
Subroutine new: called CC=5 SCAR=16.1
Pod documented
38 1 my $class = shift;
39 1 my @probs = @_;
40
41 1 my $self;
42 1 $self->{'_accProbs'} = [ 0 ];
43
44 1 my $acc = 0;
45 1 for ( @probs ) { $acc += $_;}
46 1 ▶ croak "The sum of fitness is 0, can't use roulette wheel\n" if ! $acc;
Branch: 1 branch
BranchTrueFalseTotal
unless $acc 0 1 1
47 1 for ( @probs ) { $_ /= $acc;} #Normalizes array
48
49 #Now creates the accumulated array
50 1 my $aux = 0;
51 1 for ( @probs ) {
52 5 push @{$self->{'_accProbs'}}, $_ + $aux;
53 5 $aux += $_;
54 }
55 1 bless $self, $class;
56 1 return $self;
57 }
58  
59 =head2 spin( [$number_of_individuals = 1])
60  
61 Returns an individual whose probability is related to its fitness
62  
63 =cut
64  
65 104 ▶sub spin {
Subroutine spin: called CC=5 SCAR=16.1
Pod documented
66 104 my $self = shift;
67 104 ▶ my $number_of_individuals = shift || 1;
Condition100%2/2
shift() || 1
l!l
4100
MC/DC100%1/1
shift() || 1
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
68 104 my $i = 0;
69 104 my @rand;
70 104 for my $n ( 1..$number_of_individuals ) {
71 114 push @rand, rand();
72 }
73 104 my @individuals;
74 104 ▶ for ( my $r=0; $r<= $#rand; $r++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r <= $#rand) 114 104 218
75 114 my $i = first( $rand[$r], $self->{'_accProbs'} );
76 # my $i = -1; # First iteration must be 0
77 # do {
78 # $i++;
79 # } until (( $acc_probs[$i+1] > $rand[$r] ) || ($i >= $#acc_probs ));
80 # $individuals[$r] = $i;
81 114 push @individuals, $i;
82 }
83 104 ▶ if ( $number_of_individuals > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($number_of_individuals > 1) { } 4 100 104
84 4 return @individuals;
85 } else {
86 100 return $individuals[0];
87 }
88
89 }
90  
91 =head2 first( $item, $ref_to_list )
92  
93 Returns the index of the first individual smaller than the item
94  
95 =cut
96  
97 114 ▶sub first {
Subroutine first: called CC=4 SCAR=13.9
Pod documented
98 114 my $item = shift;
99 114 ▶ my $list = shift || croak "No list";
Condition50%1/2
shift() || croak('No list')
l!l
1140
MC/DC0%0/1
shift() || croak('No list')
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
100 114 my $first=0;
101 114 my $last= scalar @$list -1;
102 114 my $mid=int($last/2);
103 114 ▶ while ($first <= $last ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($first <= $last) 342 114 456
104 342 ▶ if ( $item > $list->[$mid] ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($item > $list->[$mid]) { } 202 140 342
105 202 $first = $mid + 1;
106 } else {
107 140 $last = $mid -1;
108 }
109 342 $mid = $first+ int(($last - $first )/2);
110 }
111 114 return $last;
112
113 }
114 =head1 Copyright
115
116 This file is released under the GPL. See the LICENSE file included in this distribution,
117 or go to http://www.fsf.org/licenses/gpl.txt
118  
119 CVS Info: $Date: 2010/12/08 09:31:24 $
120 $Header: /media/Backup/Repos/opeal/opeal/Algorithm-Evolutionary/lib/Algorithm/Evolutionary/Wheel.pm,v 3.6 2010/12/08 09:31:24 jmerelo Exp $
121 $Author: jmerelo $
122  
123 =cut
124  
125 4 "The truth is by here";
126  
127 #Test code
128 #my @array = qw( 5 4 3 2 1 );
129 #my $wheel = new Wheel @array;
130  
131 #my @histo;
132 #for ( 0..100 ){
133 # my $s = $wheel->spin();
134 # print "$s\n";
135 # $histo[$s]++;
136 #}
137  
138 #for ( 0..(@histo - 1)){
139 # print $_, " => $histo[$_] \n";
140 #}
141  
142 #my @array2 = qw( 1 3 7 4 2 1 );
143 #my $wheel2 = new Wheel @array2;
144  
145 #my @histo2;
146 #for ( 0..100 ){
147 # my $s = $wheel2->spin();
148 # print "$s\n";
149 # $histo2[$s]++;
150 #}
151  
152 #for ( 0..(@histo2 - 1)){
153 # print $_, " => $histo2[$_] \n";
154 #}
« blib/lib/Algorithm/Evolutionary/Fitness/P_Peaks.pm ↑ Summary blib/lib/Algorithm/Evolutionary/Op/Canonical_GA_NN.pm »