File Coverage

lib/Algorithm/Evolutionary/Op/Novelty_Mutation.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   32964 use strict; #-*-cperl-*-
  1         2  
  1         39  
2 1     1   6 use warnings;
  1         1  
  1         28  
3              
4 1     1   5 use lib qw(../../..);
  1         2  
  1         13  
5              
6             =head1 NAME
7              
8             Algorithm::Evolutionary::Op::Novelty_Mutation - Mutation guaranteeing new individual is not in the population
9              
10             =head1 SYNOPSIS
11              
12             my $mmdp = new Algorithm::Evolutionary::Fitness::MMDP;
13             my $bits = 36;
14             my @population;
15             for ( 1..100 ) { #Create and evaluate a population
16             my $indi = new Algorithm::Evolutionary::Individual::BitString $bits;
17             $indi->evaluate( $mmdp );
18             push @population, $indi;
19             }
20             my $nm = new Algorithm::Evolutionary::Op::Novelty_Mutation $mmdp->{'_cache'}; #Initialize using cache
21             $nm->apply($population[$i]);
22            
23             =head1 Base Class
24              
25             L
26              
27             =head1 DESCRIPTION
28              
29             Attempts all possible mutations in order, until a "novelty" individual
30             is found. Generated individuals are checked against the population
31             hash, and discarded if they are already in the population.
32              
33             =head1 METHODS
34              
35             =cut
36              
37             package Algorithm::Evolutionary::Op::Novelty_Mutation;
38              
39             our $VERSION = sprintf "%d.%03d", q$Revision: 3.1 $ =~ /(\d+)\.(\d+)/g; # Hack for avoiding version mismatch
40              
41 1     1   192 use Carp;
  1         3  
  1         94  
42 1     1   841 use Clone qw(clone);
  1         4589  
  1         71  
43              
44 1     1   58 use base 'Algorithm::Evolutionary::Op::Base';
  1         2  
  1         683  
45              
46             #Class-wide constants
47             our $ARITY = 1;
48              
49             =head2 new( $ref_to_population_hash [,$priority] )
50              
51             Creates a new mutation operator with an operator application rate
52             (general for all ops), which defaults to 1, and stores the reference
53             to population hash.
54              
55             =cut
56              
57             sub new {
58             my $class = shift;
59             my $ref_to_population_hash = shift || croak "No pop hash here, fella!";
60             my $rate = shift || 1;
61              
62             my $hash = { population_hashref => $ref_to_population_hash };
63             my $self = Algorithm::Evolutionary::Op::Base::new( 'Algorithm::Evolutionary::Op::Novelty_Mutation', $rate, $hash );
64             return $self;
65             }
66              
67             =head2 apply( $chromosome )
68              
69             Applies mutation operator to a "Chromosome", a bitstring, really. Can be
70             applied only to I composed of [0,1] atoms, independently of representation; but
71             it checks before application that the operand is of type
72             L.
73              
74             =cut
75              
76             sub apply ($;$){
77             my $self = shift;
78             my $arg = shift || croak "No victim here!";
79             my $test_clone;
80             my $size = $arg->size();
81             for ( my $i = 0; $i < $size; $i++ ) {
82             if ( (ref $arg ) =~ /BitString/ ) {
83             $test_clone = clone( $arg );
84             } else {
85             $test_clone = $arg->clone();
86             }
87             $test_clone->Atom( $i, $test_clone->Atom( $i )?0:1 );
88             last if !$self->{'_population_hashref'}->{$test_clone->Chrom()}; #Exit if not found in the population
89             }
90             if ( $test_clone->Chrom() eq $arg->Chrom() ) { # Nothing done, zap
91             for ( my $i = 0; $i < $size; $i++ ) {
92             $test_clone->Atom( $i, (rand(100)>50)?0:1 );
93             }
94             }
95             $test_clone->{'_fitness'} = undef ;
96             return $test_clone;
97             }
98              
99             =head1 Copyright
100            
101             This file is released under the GPL. See the LICENSE file included in this distribution,
102             or go to http://www.fsf.org/licenses/gpl.txt
103              
104             CVS Info: $Date: 2011/02/14 06:55:36 $
105             $Header: /cvsroot/opeal/Algorithm-Evolutionary/lib/Algorithm/Evolutionary/Op/Novelty_Mutation.pm,v 3.1 2011/02/14 06:55:36 jmerelo Exp $
106             $Author: jmerelo $
107             $Revision: 3.1 $
108             $Name $
109              
110             =cut
111