File Coverage

lib/Algorithm/Evolutionary/Op/Tournament_Selection.pm
Criterion Covered Total %
statement 15 15 100.0
branch n/a
condition n/a
subroutine 5 5 100.0
pod n/a
total 20 20 100.0


line stmt bran cond sub pod time code
1 3     3   18 use strict; #-*-cperl-*-
  3         7  
  3         112  
2 3     3   17 use warnings;
  3         6  
  3         115  
3              
4 3     3   16 use lib qw( ../../../../lib );
  3         6  
  3         25  
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   860 use Carp;
  3         6  
  3         344  
34              
35             our ($VERSION) = ( '$Revision: 1.3 $ ' =~ / (\d+\.\d+)/ ) ;
36              
37 3     3   16 use base 'Algorithm::Evolutionary::Op::Base';
  3         6  
  3         251  
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
56             another of the same size by default or whatever size is selected
57              
58             =cut
59              
60             sub apply ($$) {
61             my $self = shift;
62             my $pop = shift || croak "No pop";
63             my $output_size = shift || @$pop;
64             my @output;
65             for ( my $i = 0; $i < $output_size; $i++ ) {
66             #Randomly select a few guys
67             my $best = $pop->[ rand( @$pop ) ];
68             for ( my $j = 1; $j < $self->{'_tournament_size'}; $j++ ) {
69             my $this_one = $pop->[ rand( @$pop ) ];
70             if ( $this_one->Fitness() > $best->Fitness() ) {
71             $best = $this_one;
72             }
73             }
74             #Sort by fitness
75             push @output, $best;
76             }
77             return @output;
78             }
79              
80             =head1 See Also
81              
82             L is another option for
83             selecting a pool of individuals
84              
85             =head1 Copyright
86            
87             This file is released under the GPL. See the LICENSE file included in this distribution,
88             or go to http://www.fsf.org/licenses/gpl.txt
89              
90             CVS Info: $Date: 2012/05/15 11:58:01 $
91             $Header: /cvsroot/opeal/Algorithm-Evolutionary/lib/Algorithm/Evolutionary/Op/Tournament_Selection.pm,v 1.3 2012/05/15 11:58:01 jmerelo Exp $
92             $Author: jmerelo $
93              
94             =cut
95              
96             "The truth is in here";