1use strict; #-*-cperl-*-
2use warnings;
3
4=head1 NAME
5
6Algorithm::Evolutionary::Op::Storing - Applies the op and keeps the result
7
8=head1 SYNOPSIS
9
10  my %visited_population_hash;
11
12  #Create from scratch with default operator rate
13  my $op = new Algorithm::Evolutionary::Op::Bitflip 2;
14
15  my $stored_op = new Algorithm::Evolutionary::Op::Storing ( $op, \%visited_population_hash );
16
17
18=head1 Base Class
19
20L<Algorithm::Evolutionary::Op::Base|Algorithm::Evolutionary::Op::Base>
21
22=head1 DESCRIPTION
23
24Applies an operator and stores the result in a hash (can be a tied
25database), so that the whole population is stored. It creates an
26operator whose results are cached, which could be useful for expensive
27operators.
28
29=head1 METHODS
30
31=cut
32
33package Algorithm::Evolutionary::Op::Storing;
34
35use lib qw(../../..);
36
37our $VERSION =   sprintf "%d.%03d", q$Revision: 3.1 $ =~ /(\d+)\.(\d+)/g; # Hack for avoiding version mismatch
38
39use Carp;
40
41use base 'Algorithm::Evolutionary::Op::Base';
42
43=head2 new( $operator, $population_hashref )
44
45Wraps around the operator, and stores the reference to the population
46hash that will be used
47
48=cut
49
50sub new {
51  my $class = shift;
52  my $op = shift || croak "No operator to wrap";
53  my $pop_hash = shift || croak "No population hash";
54
55  my $rate = $op->rate();
56  my $self = Algorithm::Evolutionary::Op::Base::new( 'Algorithm::Evolutionary::Op::Storing', $rate);
57  $self->{'_op'} = $op;
58  $self->{'_pop_hash'} = $pop_hash;
59  return $self;
60}
61
62=head2 apply( @victims )
63
64Applies internal operator, and keeps result
65
66=cut
67
68sub apply ($;$){
69  my $self = shift;
70  my $result = $self->{'_op'}->apply( @_ );
71  my $key = $result->as_string();
72  if ( !$self->{'_pop_hash'}->{$key} ) {
73    $self->{'_pop_hash'}->{$key} = $result;
74  }
75  return $self->{'_pop_hash'}->{$key};
76}
77
78=head1 Copyright
79
80  This file is released under the GPL. See the LICENSE file included in this distribution,
81  or go to http://www.fsf.org/licenses/gpl.txt
82
83  CVS Info: $Date: 2011/02/14 06:55:36 $
84  $Header: /media/Backup/Repos/opeal/opeal/Algorithm-Evolutionary/lib/Algorithm/Evolutionary/Op/Storing.pm,v 3.1 2011/02/14 06:55:36 jmerelo Exp $
85  $Author: jmerelo $
86  $Revision: 3.1 $
87  $Name $
88
89=cut
90
91