File Coverage

lib/Algorithm/Evolutionary/Op/Combined.pm
Criterion Covered Total %
statement 18 18 100.0
branch n/a
condition n/a
subroutine 6 6 100.0
pod n/a
total 24 24 100.0


line stmt bran cond sub pod time code
1 1     1   16887 use strict;
  1         2  
  1         44  
2 1     1   4 use warnings;
  1         1  
  1         43  
3              
4             =encoding utf8
5              
6             =head1 NAME
7              
8             Algorithm::Evolutionary::Op::Combined - Combinator of several operators of the same arity, unary or binary
9              
10             =head1 SYNOPSIS
11              
12              
13             #Initialize using OO interface
14             my $op = new Algorithm::Evolutionary::Op::Mutation 0.1 3
15             my $another_op = new Algorithm::Evolutionary::Op::Permutation 2
16             # Single operator with rate of application = 3
17             my $combined_op = new Algorithm::Evolutionary::Op::Combined [ $op, $another_op ], 3;
18              
19             =head1 Base Class
20              
21             L
22              
23             =head1 DESCRIPTION
24              
25             Some algorithms (such as
26             L) need a single
27             "mutation" and a single "crossover" operator. If you want to combine
28             several (like above, mutation and permutation), each one with its own
29             rate, you have to give them a façade like this one.
30              
31             =head1 METHODS
32              
33             =cut
34              
35             package Algorithm::Evolutionary::Op::Combined;
36              
37 1     1   3 use lib qw(../../..);
  1         1  
  1         6  
38              
39             our $VERSION = '1.2';
40              
41 1     1   407 use Algorithm::Evolutionary::Wheel;
  1         2  
  1         25  
42 1     1   4 use Carp;
  1         2  
  1         46  
43              
44 1     1   4 use base 'Algorithm::Evolutionary::Op::Base';
  1         7  
  1         335  
45              
46             #Class-wide constants
47             our $APPLIESTO = 'Algorithm::Evolutionary::Individual::String';
48             our $ARITY = 2;
49             our %parameters = ( numPoints => 2 );
50              
51             =head2 new( $ref_to_operator_array [, $operation_priority] )
52              
53             Priority defaults to one, operator array has no defaults.
54              
55             =cut
56              
57             sub new {
58             my $class = shift;
59             croak "Need operator array" if (!@_) ;
60             my $hash = { ops => shift };
61             my $rate = shift || 1;
62             my $self = Algorithm::Evolutionary::Op::Base::new( $class, $rate, $hash );
63             return $self;
64             }
65              
66             =head2 apply( @operands )
67              
68             Applies the operator to the set of operands. All are passed, as such,
69             to whatever operator is selected
70              
71             =cut
72              
73             sub apply ($$$){
74             my $self = shift;
75             my @victims = @_; # No need to clone, any operator will also clone.
76             my $op_wheel = new Algorithm::Evolutionary::Wheel map( $_->{'rate'}, @{$self->{'_ops'}} );
77             my $selected_op = $self->{'_ops'}->[ $op_wheel->spin()];
78            
79             return $selected_op->apply(@victims);
80             }
81              
82             =head1 SEE ALSO
83              
84             =over 4
85              
86             =item L a mutation operator.
87              
88             =item L another more mutation-like crossover. These two operators can be combined using this one, for instance.
89              
90             =back
91              
92             =head1 Copyright
93            
94             This file is released under the GPL. See the LICENSE file included in this distribution,
95             or go to http://www.fsf.org/licenses/gpl.txt
96              
97             =cut