line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
3
|
|
|
3
|
|
16
|
use strict; #-*-cperl-*- |
|
3
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
95
|
|
2
|
3
|
|
|
3
|
|
15
|
use warnings; |
|
3
|
|
|
|
|
8
|
|
|
3
|
|
|
|
|
96
|
|
3
|
|
|
|
|
|
|
|
4
|
3
|
|
|
3
|
|
16
|
use lib qw( ../../../../lib ); |
|
3
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
22
|
|
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
=head1 NAME |
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
Algorithm::Evolutionary::Op::Tournament_Selection - Tournament selector, takes individuals from one population and puts them into another |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
=head1 SYNOPSIS |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
my $popSize = 100; |
13
|
|
|
|
|
|
|
my $tournamentSize = 7; |
14
|
|
|
|
|
|
|
my $selector = new Algorithm::Evolutionary::Op::Tournament_Selection $tournamentSize; |
15
|
|
|
|
|
|
|
my @newPop = $selector->apply( @pop ); #Creates a new population from old |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
=head1 Base Class |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
L |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
=head1 DESCRIPTION |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
One of the possible selectors used for selecting the pool of individuals |
24
|
|
|
|
|
|
|
that are going to be the parents of the following generation. Takes a |
25
|
|
|
|
|
|
|
set of individuals randomly out of the population, and select the best. |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
=head1 METHODS |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
=cut |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
package Algorithm::Evolutionary::Op::Tournament_Selection; |
33
|
3
|
|
|
3
|
|
510
|
use Carp; |
|
3
|
|
|
|
|
7
|
|
|
3
|
|
|
|
|
327
|
|
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
our ($VERSION) = ( '$Revision: 1.5 $ ' =~ / (\d+\.\d+)/ ) ; |
36
|
|
|
|
|
|
|
|
37
|
3
|
|
|
3
|
|
16
|
use base 'Algorithm::Evolutionary::Op::Base'; |
|
3
|
|
|
|
|
7
|
|
|
3
|
|
|
|
|
244
|
|
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
=head2 new( $output_population_size, $tournament_size ) |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
Creates a new tournament selector |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
=cut |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
sub new { |
46
|
|
|
|
|
|
|
my $class = shift; |
47
|
|
|
|
|
|
|
my $self = Algorithm::Evolutionary::Op::Base::new($class ); |
48
|
|
|
|
|
|
|
$self->{'_tournament_size'} = shift || 2; |
49
|
|
|
|
|
|
|
bless $self, $class; |
50
|
|
|
|
|
|
|
return $self; |
51
|
|
|
|
|
|
|
} |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
=head2 apply( $ref_to_population[, $output_size || @$ref_to_population] ) |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
Applies the tournament selection to a population, returning another of |
56
|
|
|
|
|
|
|
the same size by default or whatever size is selected. Please bear in |
57
|
|
|
|
|
|
|
mind that, unlike other selectors, this one uses a reference to |
58
|
|
|
|
|
|
|
population instead of a population array. |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
=cut |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
sub apply ($$) { |
63
|
|
|
|
|
|
|
my $self = shift; |
64
|
|
|
|
|
|
|
my $pop = shift || croak "No pop"; |
65
|
|
|
|
|
|
|
my $output_size = shift || @$pop; |
66
|
|
|
|
|
|
|
my @output; |
67
|
|
|
|
|
|
|
for ( my $i = 0; $i < $output_size; $i++ ) { |
68
|
|
|
|
|
|
|
#Randomly select a few guys |
69
|
|
|
|
|
|
|
my $best = $pop->[ rand( @$pop ) ]; |
70
|
|
|
|
|
|
|
for ( my $j = 1; $j < $self->{'_tournament_size'}; $j++ ) { |
71
|
|
|
|
|
|
|
my $this_one = $pop->[ rand( @$pop ) ]; |
72
|
|
|
|
|
|
|
if ( $this_one->{'_fitness'} > $best->{'_fitness'} ) { |
73
|
|
|
|
|
|
|
$best = $this_one; |
74
|
|
|
|
|
|
|
} |
75
|
|
|
|
|
|
|
} |
76
|
|
|
|
|
|
|
#Sort by fitness |
77
|
|
|
|
|
|
|
push @output, $best; |
78
|
|
|
|
|
|
|
} |
79
|
|
|
|
|
|
|
return @output; |
80
|
|
|
|
|
|
|
} |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
=head1 See Also |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
L is another option for |
85
|
|
|
|
|
|
|
selecting a pool of individuals |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
=head1 Copyright |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
This file is released under the GPL. See the LICENSE file included in this distribution, |
90
|
|
|
|
|
|
|
or go to http://www.fsf.org/licenses/gpl.txt |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
CVS Info: $Date: 2013/01/05 12:54:48 $ |
93
|
|
|
|
|
|
|
$Header: /media/Backup/Repos/opeal/opeal/Algorithm-Evolutionary/lib/Algorithm/Evolutionary/Op/Tournament_Selection.pm,v 1.5 2013/01/05 12:54:48 jmerelo Exp $ |
94
|
|
|
|
|
|
|
$Author: jmerelo $ |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
=cut |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
"The truth is in here"; |