File Coverage

lib/Algorithm/Evolutionary/Op/Breeder.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 1     1   6 use strict;
  1         2  
  1         35  
2 1     1   5 use warnings;
  1         2  
  1         62  
3              
4             =head1 NAME
5              
6             Algorithm::Evolutionary::Op::Breeder - Even more customizable single generation for an evolutionary algorithm.
7            
8             =head1 SYNOPSIS
9              
10             use Algorithm::Evolutionary qw( Individual::BitString
11             Op::Mutation Op::Crossover
12             Op::RouletteWheel
13             Op::Breeder);
14              
15             use Algorithm::Evolutionary::Utils qw(average);
16              
17             my @pop;
18             my $number_of_bits = 20;
19             my $population_size = 20;
20             my $replacement_rate = 0.5;
21             for ( 1..$population_size ) {
22             my $indi = new Algorithm::Evolutionary::Individual::BitString $number_of_bits ; #Creates random individual
23             $indi->evaluate( $onemax );
24             push( @pop, $indi );
25             }
26              
27             my $m = new Algorithm::Evolutionary::Op::Mutation 0.5;
28             my $c = new Algorithm::Evolutionary::Op::Crossover; #Classical 2-point crossover
29              
30             my $selector = new Algorithm::Evolutionary::Op::RouletteWheel $population_size; #One of the possible selectors
31              
32             my $generation =
33             new Algorithm::Evolutionary::Op::Breeder( $selector, [$m, $c] );
34              
35             my @sortPop = sort { $b->Fitness() <=> $a->Fitness() } @pop;
36             my $bestIndi = $sortPop[0];
37             my $previous_average = average( \@sortPop );
38             $generation->apply( \@sortPop );
39              
40             =head1 Base Class
41              
42             L
43              
44             =head1 DESCRIPTION
45              
46             Breeder part of the evolutionary algorithm; takes a population and returns another created from the first
47              
48             =head1 METHODS
49              
50             =cut
51              
52             package Algorithm::Evolutionary::Op::Breeder;
53              
54 1     1   6 use lib qw(../../..);
  1         2  
  1         12  
55              
56             our ($VERSION) = ( '$Revision: 1.2 $ ' =~ / (\d+\.\d+)/ ) ;
57              
58 1     1   456 use Carp;
  1         2  
  1         66  
59              
60 1     1   6 use base 'Algorithm::Evolutionary::Op::Base';
  1         2  
  1         78  
61              
62             use Algorithm::Evolutionary qw(Wheel
63             Op::Tournament_Selection);
64              
65             # Class-wide constants
66             our $APPLIESTO = 'ARRAY';
67             our $ARITY = 1;
68              
69             =head2 new( $ref_to_operator_array[, $selector = new Algorithm::Evolutionary::Op::Tournament_Selection 2 ] )
70              
71             Creates a breeder, with a selector and array of operators
72              
73             =cut
74              
75             sub new {
76             my $class = shift;
77             my $self = {};
78             $self->{_ops} = shift || croak "No operators found";
79             $self->{_selector} = shift
80             || new Algorithm::Evolutionary::Op::Tournament_Selection 2;
81             bless $self, $class;
82             return $self;
83             }
84              
85             =head2 apply( $population[, $how_many || $population_size] )
86              
87             Applies the algorithm to the population, which should have
88             been evaluated first; checks that it receives a
89             ref-to-array as input, croaks if it does not. Returns a sorted,
90             culled, evaluated population for next generation.
91              
92             =cut
93              
94             sub apply ($) {
95             my $self = shift;
96             my $pop = shift || croak "No population here";
97             my $output_size = shift || @$pop; # Defaults to pop size
98             my @ops = @{$self->{_ops}};
99              
100             #Select for breeding
101             my $selector = $self->{_selector};
102             my @genitors = $selector->apply( $pop );
103              
104             #Reproduce
105             my $totRate = 0;
106             my @rates;
107             for ( @ops ) {
108             push( @rates, $_->{rate});
109             }
110             my $opWheel = new Algorithm::Evolutionary::Wheel @rates;
111              
112             my @new_population;
113             for ( my $i = 0; $i < $output_size; $i++ ) {
114             my @offspring;
115             my $selectedOp = $ops[ $opWheel->spin()];
116             # print $selectedOp->asXML;
117             for ( my $j = 0; $j < $selectedOp->arity(); $j ++ ) {
118             my $chosen = $genitors[ rand( @genitors )];
119             # print "Elegido ", $chosen->asString(), "\n";
120             push( @offspring, $chosen->clone() );
121             }
122             my $mutante = $selectedOp->apply( @offspring );
123             push( @new_population, $mutante );
124             }
125            
126             return \@new_population;
127             }
128              
129             =head1 SEE ALSO
130              
131             More or less in the same ballpark, alternatives to this one
132              
133             =over 4
134              
135             =item *
136              
137             L
138              
139             =back
140              
141             =head1 Copyright
142            
143             This file is released under the GPL. See the LICENSE file included in this distribution,
144             or go to http://www.fsf.org/licenses/gpl.txt
145              
146             CVS Info: $Date: 2010/12/16 18:57:41 $
147             $Header: /cvsroot/opeal/Algorithm-Evolutionary/lib/Algorithm/Evolutionary/Op/Breeder.pm,v 1.2 2010/12/16 18:57:41 jmerelo Exp $
148             $Author: jmerelo $
149             $Revision: 1.2 $
150              
151             =cut
152              
153             "The truth is out there";