File Coverage

lib/Algorithm/Evolutionary/Op/QuadXOver.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 2     2   16456 use strict;
  2         38  
  2         65  
2 2     2   7 use warnings;
  2         3  
  2         82  
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=<
13            
14             #Max is 2, anyways
15            
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
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     2   8 use lib qw( ../../.. );
  2         2  
  2         8  
45              
46             our $VERSION = sprintf "%d.1%02d", q$Revision: 3.4 $ =~ /(\d+)\.(\d+)/g; # Hack for avoiding version mismatch
47              
48 2     2   244 use Carp;
  2         2  
  2         119  
49              
50 2     2   7 use base 'Algorithm::Evolutionary::Op::Crossover';
  2         2  
  2         677  
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, 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