1use strict; #-*-cperl-*- 2use warnings; 3 4use lib qw( ../../../../lib ); 5 6=head1 NAME 7 8Algorithm::Evolutionary::Op::RouletteWheel - Fitness-proportional selection, using a roulette wheel. 9 10=head1 SYNOPSIS 11 12 use Algorithm::Evolutionary::Op::RouletteWheel; 13 my $popSize = 100; 14 my $selector = new Algorithm::Evolutionary::Op::RouletteWheel $popSize; 15 16=head1 Base Class 17 18L<Algorithm::Evolutionary::Op::Selector> 19 20=head1 DESCRIPTION 21 22Roulette wheel selection tries to select as many copies of the 23individual as it corresponds to its fitness. It is used in the 24canonical GA. Some information on this method of selection can be 25found in 26L<this GA tutorial|http://www.geatbx.com/docu/algselct.html#nameselectionrws> 27 28=head1 METHODS 29 30=cut 31 32package Algorithm::Evolutionary::Op::RouletteWheel; 33use Carp; 34 35our $VERSION = '3.1'; 36 37use base 'Algorithm::Evolutionary::Op::Selector'; 38 39use Algorithm::Evolutionary::Wheel; 40 41# Class-wide constants 42#our $APPLIESTO = 'ARRAY'; 43#our $ARITY = 2; #Needs an array for input, a reference for output 44 45=head2 new( $output_population_size ) 46 47Creates a new roulette wheel selector 48 49=cut 50 51sub new { 52 my $class = shift; 53 my $self = Algorithm::Evolutionary::Op::Selector::new($class,shift ); 54 return $self; 55} 56 57=head2 apply 58 59Applies the tournament selection to a population, returning 60another of the said size 61 62=cut 63 64sub apply (@) { 65 my $self = shift; 66 my @pop = @_; 67 croak "Small population size" if ! @_; 68 my @output; 69 #Create the value array 70 my $sum = 0; 71 my @rates; 72 for ( @pop ) { 73 $sum .= $_->Fitness() if defined $_->Fitness(); 74 push @rates, $_->Fitness(); 75 } 76 my $popWheel=new Algorithm::Evolutionary::Wheel @rates; 77 78 #Select 79 for ( my $i = 0; $i < $self->{_outputSize}; $i++ ) { 80 #Randomly select a few guys 81 push @output, $pop[$popWheel->spin()]; 82 } 83 return @output; 84} 85 86=head1 See Also 87 88L<Algorithm::Evolutionary::Op::TournamentSelect> is another option for 89selecting a pool of individuals 90 91=head1 Copyright 92 93 This file is released under the GPL. See the LICENSE file included in this distribution, 94 or go to http://www.fsf.org/licenses/gpl.txt 95 96=cut 97 98"The truth is in there"; 99