| line | stmt | bran | cond | sub | pod | time | code | 
| 1 | 9 |  |  | 9 |  | 58 | use strict; # -*- cperl -*- | 
|  | 9 |  |  |  |  | 20 |  | 
|  | 9 |  |  |  |  | 438 |  | 
| 2 | 9 |  |  | 9 |  | 53 | use warnings; | 
|  | 9 |  |  |  |  | 17 |  | 
|  | 9 |  |  |  |  | 330 |  | 
| 3 |  |  |  |  |  |  |  | 
| 4 | 9 |  |  | 9 |  | 44 | use lib qw( ../../../../lib ); | 
|  | 9 |  |  |  |  | 14 |  | 
|  | 9 |  |  |  |  | 66 |  | 
| 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 | 9 |  |  | 9 |  | 2070 | use Carp qw( croak ); | 
|  | 9 |  |  |  |  | 27 |  | 
|  | 9 |  |  |  |  | 526 |  | 
| 29 | 9 |  |  | 9 |  | 50 | use base qw(Algorithm::Evolutionary::Fitness::String); | 
|  | 9 |  |  |  |  | 16 |  | 
|  | 9 |  |  |  |  | 6167 |  | 
| 30 |  |  |  |  |  |  |  | 
| 31 |  |  |  |  |  |  |  | 
| 32 |  |  |  |  |  |  | sub _really_apply { | 
| 33 | 0 |  |  | 0 |  |  | my $self = shift; | 
| 34 | 0 |  |  |  |  |  | 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 | 0 |  |  | 0 | 1 |  | my $self = shift; | 
| 45 | 0 |  |  |  |  |  | my $string = shift; | 
| 46 |  |  |  |  |  |  |  | 
| 47 | 0 |  |  |  |  |  | my $cache = $self->{'_cache'}; | 
| 48 | 0 | 0 |  |  |  |  | if ( defined $cache->{$string} ) { | 
| 49 | 0 |  |  |  |  |  | return $cache->{$string}; | 
| 50 |  |  |  |  |  |  | } | 
| 51 | 0 |  |  |  |  |  | my $num_ones = ($string =~ tr/1/0/); | 
| 52 |  |  |  |  |  |  | #    my $num_ones = grep( $_, split(//, $string )); | 
| 53 |  |  |  |  |  |  | #     while ( $string ) { | 
| 54 |  |  |  |  |  |  | #       $num_ones += chop( $string ); | 
| 55 |  |  |  |  |  |  | #     } | 
| 56 | 0 |  |  |  |  |  | $cache->{$string} = $num_ones; | 
| 57 | 0 |  |  |  |  |  | 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 |  |  |  |  |  |  | CVS Info: $Date: 2009/07/24 08:46:59 $ | 
| 66 |  |  |  |  |  |  | $Header: /cvsroot/opeal/Algorithm-Evolutionary/lib/Algorithm/Evolutionary/Fitness/ONEMAX.pm,v 3.0 2009/07/24 08:46:59 jmerelo Exp $ | 
| 67 |  |  |  |  |  |  | $Author: jmerelo $ | 
| 68 |  |  |  |  |  |  | $Revision: 3.0 $ | 
| 69 |  |  |  |  |  |  |  | 
| 70 |  |  |  |  |  |  | =cut | 
| 71 |  |  |  |  |  |  |  | 
| 72 |  |  |  |  |  |  | "What???"; |