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