« lib/Algorithm/Evolutionary/Individual/Any.pm
↑ Summary
blib/lib/Algorithm/Evolutionary/Op/Eval/General.pm »
| 1 | use strict; #-*-cperl-*- | ||
| 2 | use warnings; | ||
| 3 | |||
| 4 | =head1 NAME | ||
| 5 | |||
| 6 | Algorithm::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 | |||
| 20 | L<Algorithm::Evolutionary::Op::Base|Algorithm::Evolutionary::Op::Base> | ||
| 21 | |||
| 22 | =head1 DESCRIPTION | ||
| 23 | |||
| 24 | Applies an operator and stores the result in a hash (can be a tied | ||
| 25 | database), so that the whole population is stored. It creates an | ||
| 26 | operator whose results are cached, which could be useful for expensive | ||
| 27 | operators. | ||
| 28 | |||
| 29 | =head1 METHODS | ||
| 30 | |||
| 31 | =cut | ||
| 32 | |||
| 33 | package Algorithm::Evolutionary::Op::Storing; | ||
| 34 | |||
| 35 | use lib qw(../../..); | ||
| 36 | |||
| 37 | our $VERSION = sprintf "%d.%03d", q$Revision: 3.1 $ =~ /(\d+)\.(\d+)/g; # Hack for avoiding version mismatch | ||
| 38 | |||
| 39 | use Carp; | ||
| 40 | |||
| 41 | use base 'Algorithm::Evolutionary::Op::Base'; | ||
| 42 | |||
| 43 | =head2 new( $operator, $population_hashref ) | ||
| 44 | |||
| 45 | Wraps around the operator, and stores the reference to the population | ||
| 46 | hash that will be used | ||
| 47 | |||
| 48 | =cut | ||
| 49 | |||
| 50 | sub 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 | |||
| 64 | Applies internal operator, and keeps result | ||
| 65 | |||
| 66 | =cut | ||
| 67 | |||
| 68 | sub 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 |