``````use strict; #-*-cperl-*-
use warnings;

use lib qw( ../../../../lib );

Algorithm::Evolutionary::Op::RouletteWheel - Fitness-proportional selection, using a roulette wheel.

use Algorithm::Evolutionary::Op::RouletteWheel;
my \$popSize = 100;
my \$selector = new  Algorithm::Evolutionary::Op::RouletteWheel \$popSize;

L<Algorithm::Evolutionary::Op::Selector>

Roulette wheel selection tries to select as many copies of the
individual as it corresponds to its fitness. It is used in the
canonical GA. Some information on this method of selection can be
found in
L<this GA tutorial|http://www.geatbx.com/docu/algselct.html#nameselectionrws>

=cut

package  Algorithm::Evolutionary::Op::RouletteWheel;
use Carp;

our \$VERSION = '3.1';

use base 'Algorithm::Evolutionary::Op::Selector';

use Algorithm::Evolutionary::Wheel;

# Class-wide constants
#our \$APPLIESTO =  'ARRAY';
#our \$ARITY = 2; #Needs an array for input, a reference for output

Creates a new roulette wheel selector

=cut

sub new {
my \$class = shift;
my \$self = Algorithm::Evolutionary::Op::Selector::new(\$class,shift );
return \$self;
}

Applies the tournament selection to a population, returning
another of the said size

=cut

sub apply (@) {
my \$self = shift;
my @pop = @_;
croak "Small population size" if ! @_;
my @output;
#Create the value array
my \$sum = 0;
my @rates;
for ( @pop ) {
\$sum .= \$_->Fitness() if defined \$_->Fitness();
push @rates, \$_->Fitness();
}
my \$popWheel=new Algorithm::Evolutionary::Wheel @rates;

#Select
for ( my \$i = 0; \$i < \$self->{_outputSize}; \$i++ ) {
#Randomly select a few guys
push @output, \$pop[\$popWheel->spin()];
}
return @output;
}