« blib/lib/Algorithm/Evolutionary/Fitness/wP_Peaks.pm
↑ Summary
blib/lib/Algorithm/Evolutionary/Fitness/Rastrigin.pm »
| 1 | use strict; # -*- cperl -*- | ||
| 2 | use warnings; | ||
| 3 | |||
| 4 | use lib qw( ../../../../lib ); | ||
| 5 | |||
| 6 | =head1 NAME | ||
| 7 | |||
| 8 | Algorithm::Evolutionary::Fitness::ONEMAX - Fitness function for the ONEMAX or count-ones problem | ||
| 9 | |||
| 10 | =head1 SYNOPSIS | ||
| 11 | |||
| 12 | my $onemax = new Algorithm::Evolutionary::Fitness::Knapsack; | ||
| 13 | my $string = "1010101010101010"; | ||
| 14 | print $onemax->apply($string); | ||
| 15 | |||
| 16 | =head1 DESCRIPTION | ||
| 17 | |||
| 18 | ONEMAX is the classical count-ones optimization function. Fast to implement, and good for early prototyping of new evolutionary algorithms. | ||
| 19 | |||
| 20 | =head1 METHODS | ||
| 21 | |||
| 22 | =cut | ||
| 23 | |||
| 24 | package Algorithm::Evolutionary::Fitness::ONEMAX; | ||
| 25 | |||
| 26 | our ($VERSION) = ( '$Revision: 3.0 $ ' =~ / (\d+\.\d+)/ ) ; | ||
| 27 | |||
| 28 | use Carp qw( croak ); | ||
| 29 | use base qw(Algorithm::Evolutionary::Fitness::String); | ||
| 30 | |||
| 31 | |||
| 32 | sub _really_apply { | ||
| 33 | my $self = shift; | ||
| 34 | return $self->onemax( @_ ); | ||
| 35 | } | ||
| 36 | |||
| 37 | =head2 onemax | ||
| 38 | |||
| 39 | Computes the number of ones, using base class cache | ||
| 40 | |||
| 41 | =cut | ||
| 42 | |||
| 43 | sub onemax { | ||
| 44 | my $self = shift; | ||
| 45 | my $string = shift; | ||
| 46 | |||
| 47 | my $cache = $self->{'_cache'}; | ||
| 48 | if ( defined $cache->{$string} ) { | ||
| 49 | return $cache->{$string}; | ||
| 50 | } | ||
| 51 | my $num_ones = ($string =~ tr/1/1/); | ||
| 52 | # my $num_ones = grep( $_, split(//, $string )); | ||
| 53 | # while ( $string ) { | ||
| 54 | # $num_ones += chop( $string ); | ||
| 55 | # } | ||
| 56 | $cache->{$string} = $num_ones; | ||
| 57 | return $num_ones; | ||
| 58 | } | ||
| 59 | |||
| 60 | =head1 Copyright | ||
| 61 | |||
| 62 | This file is released under the GPL. See the LICENSE file included in this distribution, | ||
| 63 | or go to http://www.fsf.org/licenses/gpl.txt | ||
| 64 | |||
| 65 | =cut | ||
| 66 | |||
| 67 | "Nothing to do here, proceed"; |