Algorithm-Evolutionary-Utils

 view release on metacpan or  search on metacpan

lib/Algorithm/Evolutionary/Utils.pm  view on Meta::CPAN

use strict; #-*-CPerl-*-
use warnings;

use lib qw( ../../../lib );

=encoding utf8

=head1 NAME

Algorithm::Evolutionary::Utils - Collection of functions used in all kind of evolutionary algorithms..

=head1 SYNOPSIS
  
   use Algorithm::Evolutionary::Utils qw(entropy genotypic_entropy hamming consensus average random_bitstring random_number_array decode_string vector_compare );

  my $this_entropy = entropy( $population );

  #Computes consensus sequence (for binary chromosomes
  my $this_consensus = consensus( $population); 

=head1 DESCRIPTION

Miscellaneous class that contains functions that might be useful
    somewhere else, especially when computing EA statistics.  

=cut


=head1 METHODS

=cut

package Algorithm::Evolutionary::Utils;

use Exporter;
our @ISA = qw(Exporter);

use version; our $VERSION =  qv("3.403");

our @EXPORT_OK = qw( entropy genotypic_entropy consensus hamming 
		     random_bitstring random_number_array average 
		     parse_xml decode_string vector_compare);

use Carp;
use String::Random;
use XML::Parser;
use Statistics::Basic qw(mean);

=head2 entropy( $population)

Computes the entropy using the well known Shannon's formula: L<http://en.wikipedia.org/wiki/Information_entropy>
'to avoid botching highlighting

=cut

sub entropy {
  my $population = shift;
  my %frequencies;
  map( (defined $_->{'_fitness'})?$frequencies{$_->{'_fitness'}}++:1, @$population );
  my $entropy = 0;
  my $gente = scalar(@$population); # Population size
  for my $f ( keys %frequencies ) {
    my $this_freq = $frequencies{$f}/$gente;
    $entropy -= $this_freq*log( $this_freq );
  }
  return $entropy;



( run in 1.417 second using v1.01-cache-2.11-cpan-5a3173703d6 )