lib/Algorithm/Evolutionary/Op/QuadXOver.pm

filter: Statementstmt 100% 15 / 15 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 5 / 5 Podpod n/a totaltotal 100% 20 / 20 CC 1 SCAR 0.0
CC
1
Cov
100%
BEGIN
1.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« lib/Algorithm/Evolutionary/Op/Population_Output.pm ↑ Summary lib/Algorithm/Evolutionary/Op/Quad_Crossover_Diff.pm »
1 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 =head1 NAME
5  
6 Algorithm::Evolutionary::Op::QuadXOver - N-point crossover operator that changes operands
7  
8
9  
10 =head1 SYNOPSIS
11  
12 my $xmlStr3=<<EOC;
13 <op name='QuadXOver' type='binary' rate='1'>
14 <param name='numPoints' value='2' /> #Max is 2, anyways
15 </op>
16 EOC
17 my $ref3 = XMLin($xmlStr3);
18  
19 my $op3 = Algorithm::Evolutionary::Op::Base->fromXML( $ref3 );
20 print $op3->asXML(), "\n";
21  
22 my $indi = new Algorithm::Evolutionary::Individual::BitString 10;
23 my $indi2 = $indi->clone();
24 my $indi3 = $indi->clone(); #Operands are modified, so better to clone them
25 $op3->apply( $indi2, $indi3 );
26  
27 my $op4 = new Algorithm::Evolutionary::Op::QuadXOver 1; #QuadXOver with 1 crossover points
28  
29 =head1 Base Class
30  
31 L<Algorithm::Evolutionary::Op::Base|Algorithm::Evolutionary::Op::Base>
32  
33 =head1 DESCRIPTION
34  
35 Crossover operator for a GA, takes args by reference and issues two
36 children from two parents
37  
38 =head1 METHODS
39  
40 =cut
41  
42 package Algorithm::Evolutionary::Op::QuadXOver;
43  
44 2 ▶use lib qw( ../../.. );
Subroutine BEGIN: called CC=1 SCAR=0.0
45  
46 our $VERSION = sprintf "%d.1%02d", q$Revision: 3.4 $ =~ /(\d+)\.(\d+)/g; # Hack for avoiding version mismatch
47  
48 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
49  
50 2 ▶use base 'Algorithm::Evolutionary::Op::Crossover';
Subroutine BEGIN: called CC=1 SCAR=0.0
51  
52 #Class-wide constants
53 our $APPLIESTO = 'Algorithm::Evolutionary::Individual::String';
54 our $ARITY = 2;
55  
56 =head2 apply( $parent_1, $parent_2 )
57  
58 Same as L<Algorithm::Evolutionary::Op::Crossover>, but changes
59 parents, does not return anything; that is, $parent_1 and $parent_2
60 interchange genetic material.
61  
62 =cut
63  
64 sub apply ($$){
65 my $self = shift;
66 my $victim = shift || croak "No victim here!";
67 my $victim2 = shift || croak "No victim here!";
68 # croak "Incorrect type ".(ref $victim) if !$self->check($victim);
69 # croak "Incorrect type ".(ref $victim2) if !$self->check($victim2);
70 my $minlen = ( length( $victim->{_str} ) > length( $victim2->{_str} ) )?
71 length( $victim2->{_str} ): length( $victim->{_str} );
72 my $pt1 = 1+int( rand( $minlen - 1 ) ); # first crossover point shouldn't be 0
73 my $range;
74 if ( $self->{_numPoints} > 1 ) {
75 $range= 1 + int( rand( $minlen - $pt1 ) );
76 } else {
77 $range = $minlen - $pt1;
78 }
79 # print "Puntos: $pt1, $range \n";
80 my $str = $victim->{_str};
81 substr( $victim->{_str}, $pt1, $range ) = substr( $victim2->{_str}, $pt1, $range );
82 substr( $victim2->{_str}, $pt1, $range ) = substr( $str, $pt1, $range );
83 $victim->Fitness( undef );
84 $victim2->Fitness( undef );
85 return undef; #As a warning that you should not expect anything
86 }
87  
88 =head1 Copyright
89
90 This file is released under the GPL. See the LICENSE file included in this distribution,
91 or go to http://www.fsf.org/licenses/gpl.txt
92  
93 CVS Info: $Date: 2010/12/08 17:34:22 $
94 $Header: /media/Backup/Repos/opeal/opeal/Algorithm-Evolutionary/lib/Algorithm/Evolutionary/Op/QuadXOver.pm,v 3.4 2010/12/08 17:34:22 jmerelo Exp $
95 $Author: jmerelo $
96 $Revision: 3.4 $
97 $Name $
98  
99 =cut
« lib/Algorithm/Evolutionary/Op/Population_Output.pm ↑ Summary lib/Algorithm/Evolutionary/Op/Quad_Crossover_Diff.pm »