lib/Algorithm/Evolutionary/Op/Uniform_Crossover_Diff.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/Uniform_Crossover.pm ↑ Summary lib/Algorithm/Evolutionary/Run.pm »
1 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 =head1 NAME
5  
6 Algorithm::Evolutionary::Op::Uniform_Crossover_Diff - Uniform crossover, but interchanges only those atoms that are different
7  
8
9  
10 =head1 SYNOPSIS
11  
12 my $xmlStr3=<<EOC;
13 <op name='Uniform_Crossover_Diff' 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::Uniform_Crossover_Diff 1; #Uniform_Crossover_Diff 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 acting only on those bits that are different.
36  
37 =head1 METHODS
38  
39 =cut
40  
41 package Algorithm::Evolutionary::Op::Uniform_Crossover_Diff;
42  
43 1 ▶use lib qw( ../../.. );
Subroutine BEGIN: called CC=1 SCAR=0.0
44  
45 our $VERSION = sprintf "%d.1%02d", q$Revision: 3.6 $ =~ /(\d+)\.(\d+)/g; # Hack for avoiding version mismatch
46  
47 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
48  
49 1 ▶use base 'Algorithm::Evolutionary::Op::Crossover';
Subroutine BEGIN: called CC=1 SCAR=0.0
50  
51 #Class-wide constants
52 our $APPLIESTO = 'Algorithm::Evolutionary::Individual::String';
53 our $ARITY = 2;
54  
55 =head2 new( [$options_hash] [, $operation_priority] )
56  
57 Creates a new n-point crossover operator, with 2 as the default number
58 of points, that is, the default would be
59 my $options_hash = { crossover_rate => 0.5 };
60 my $priority = 1;
61  
62 =cut
63  
64 sub new {
65 my $class = shift;
66 my $hash = { numPoints => shift || 1 };
67 croak "Less than 1 points to cross"
68 if $hash->{'numPoints'} < 1;
69 my $priority = shift || 1;
70 my $self = Algorithm::Evolutionary::Op::Base::new( $class, $priority, $hash );
71 return $self;
72 }
73  
74 =head2 apply( $parent_1, $parent_2 )
75  
76 Same as L<Algorithm::Evolutionary::Op::Uniform_Crossover>, but making
77 sure that what is interchanged is different.
78  
79 =cut
80  
81 sub apply ($$){
82 my $self = shift;
83 my $arg = shift || croak "No victim here!";
84 my $arg2 = shift || croak "No victim here!";
85 my $victim2 = $arg2->clone();
86 my $victim = $arg->clone();
87 my $min_length = ( length( $victim->{_str} ) > length( $victim2->{_str} ) )?
88 length( $victim2->{_str} ): length( $victim->{_str} );
89  
90 my @diffs;
91 for ( my $i = 0; $i < $min_length; $i ++ ) {
92 if ( substr( $victim2->{_str}, $i, 1 ) ne substr( $victim->{_str}, $i, 1 ) ) {
93 push @diffs, $i;
94 }
95 }
96  
97 for ( my $i = 0; $i < $self->{'_numPoints'}; $i ++ ) {
98 if ( $#diffs > 0 ) {
99 my $diff = splice( @diffs, rand(@diffs), 1 );
100 substr( $victim->{'_str'}, $diff, 1 ) = substr( $victim2->{'_str'}, $diff, 1 );
101 } else {
102 last;
103 }
104 }
105 $victim->Fitness( undef );
106 return $victim;
107 }
108  
109 =head1 Copyright
110
111 This file is released under the GPL. See the LICENSE file included in this distribution,
112 or go to http://www.fsf.org/licenses/gpl.txt
113  
114 CVS Info: $Date: 2012/07/08 10:38:52 $
115 $Header: /media/Backup/Repos/opeal/opeal/Algorithm-Evolutionary/lib/Algorithm/Evolutionary/Op/Uniform_Crossover_Diff.pm,v 3.6 2012/07/08 10:38:52 jmerelo Exp $
116 $Author: jmerelo $
117 $Revision: 3.6 $
118 $Name $
119  
120 =cut
« lib/Algorithm/Evolutionary/Op/Uniform_Crossover.pm ↑ Summary lib/Algorithm/Evolutionary/Run.pm »