| line | stmt | bran | cond | sub | pod | time | code | 
| 1 | 1 |  |  | 1 |  | 6 | use strict; #-*-cperl-*- | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 33 |  | 
| 2 | 1 |  |  | 1 |  | 5 | use warnings; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 36 |  | 
| 3 |  |  |  |  |  |  |  | 
| 4 | 1 |  |  | 1 |  | 5 | use lib qw( ../../../../lib ); | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 8 |  | 
| 5 |  |  |  |  |  |  |  | 
| 6 |  |  |  |  |  |  | =head1 NAME | 
| 7 |  |  |  |  |  |  |  | 
| 8 |  |  |  |  |  |  | Algorithm::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 |  |  |  |  |  |  |  | 
| 18 |  |  |  |  |  |  | L | 
| 19 |  |  |  |  |  |  |  | 
| 20 |  |  |  |  |  |  | =head1 DESCRIPTION | 
| 21 |  |  |  |  |  |  |  | 
| 22 |  |  |  |  |  |  | Roulette wheel selection tries to select as many copies of the | 
| 23 |  |  |  |  |  |  | individual as it corresponds to its fitness. It is used in the | 
| 24 |  |  |  |  |  |  | canonical GA. Some information on this method of selection can be | 
| 25 |  |  |  |  |  |  | found in | 
| 26 |  |  |  |  |  |  | L | 
| 27 |  |  |  |  |  |  |  | 
| 28 |  |  |  |  |  |  | =head1 METHODS | 
| 29 |  |  |  |  |  |  |  | 
| 30 |  |  |  |  |  |  | =cut | 
| 31 |  |  |  |  |  |  |  | 
| 32 |  |  |  |  |  |  | package  Algorithm::Evolutionary::Op::RouletteWheel; | 
| 33 | 1 |  |  | 1 |  | 171 | use Carp; | 
|  | 1 |  |  |  |  | 3 |  | 
|  | 1 |  |  |  |  | 74 |  | 
| 34 |  |  |  |  |  |  |  | 
| 35 |  |  |  |  |  |  | our $VERSION = '3.1'; | 
| 36 |  |  |  |  |  |  |  | 
| 37 | 1 |  |  | 1 |  | 5 | use base 'Algorithm::Evolutionary::Op::Selector'; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 497 |  | 
| 38 |  |  |  |  |  |  |  | 
| 39 |  |  |  |  |  |  | use 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 |  |  |  |  |  |  |  | 
| 47 |  |  |  |  |  |  | Creates a new roulette wheel selector | 
| 48 |  |  |  |  |  |  |  | 
| 49 |  |  |  |  |  |  | =cut | 
| 50 |  |  |  |  |  |  |  | 
| 51 |  |  |  |  |  |  | sub new { | 
| 52 |  |  |  |  |  |  | my $class = shift; | 
| 53 |  |  |  |  |  |  | my $self = Algorithm::Evolutionary::Op::Selector::new($class,shift ); | 
| 54 |  |  |  |  |  |  | return $self; | 
| 55 |  |  |  |  |  |  | } | 
| 56 |  |  |  |  |  |  |  | 
| 57 |  |  |  |  |  |  | =head2 apply | 
| 58 |  |  |  |  |  |  |  | 
| 59 |  |  |  |  |  |  | Applies the tournament selection to a population, returning | 
| 60 |  |  |  |  |  |  | another of the said size | 
| 61 |  |  |  |  |  |  |  | 
| 62 |  |  |  |  |  |  | =cut | 
| 63 |  |  |  |  |  |  |  | 
| 64 |  |  |  |  |  |  | sub 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 |  |  |  |  |  |  |  | 
| 88 |  |  |  |  |  |  | L is another option for | 
| 89 |  |  |  |  |  |  | selecting 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"; |