| line | stmt | bran | cond | sub | pod | time | code | 
| 1 | 1 |  |  | 1 |  | 4032 | use strict; | 
|  | 1 |  |  |  |  | 3 |  | 
|  | 1 |  |  |  |  | 32 |  | 
| 2 | 1 |  |  | 1 |  | 34 | use warnings; | 
|  | 1 |  |  |  |  | 3 |  | 
|  | 1 |  |  |  |  | 41 |  | 
| 3 |  |  |  |  |  |  |  | 
| 4 |  |  |  |  |  |  | =head1 NAME | 
| 5 |  |  |  |  |  |  |  | 
| 6 |  |  |  |  |  |  | Algorithm::Evolutionary::Hash_Wheel - Random selector of things depending on probabilities | 
| 7 |  |  |  |  |  |  |  | 
| 8 |  |  |  |  |  |  | =head1 SYNOPSIS | 
| 9 |  |  |  |  |  |  |  | 
| 10 |  |  |  |  |  |  | my $wheel = new Algorithm::Evolutionary::Hash_Wheel( \%probs ); | 
| 11 |  |  |  |  |  |  | print $wheel->spin(); #Returns an element according to probabilities; | 
| 12 |  |  |  |  |  |  |  | 
| 13 |  |  |  |  |  |  | =head1 DESCRIPTION | 
| 14 |  |  |  |  |  |  |  | 
| 15 |  |  |  |  |  |  | Creates a "roulette wheel" for spinning and selecting stuff. It will | 
| 16 |  |  |  |  |  |  | be used in several places; mainly in the | 
| 17 |  |  |  |  |  |  | L. It's similar to | 
| 18 |  |  |  |  |  |  | L, but with a hash instead of an | 
| 19 |  |  |  |  |  |  | array. Probably should unify both.. | 
| 20 |  |  |  |  |  |  |  | 
| 21 |  |  |  |  |  |  | =head1 METHODS | 
| 22 |  |  |  |  |  |  |  | 
| 23 |  |  |  |  |  |  | =cut | 
| 24 |  |  |  |  |  |  |  | 
| 25 |  |  |  |  |  |  | package Algorithm::Evolutionary::Hash_Wheel; | 
| 26 | 1 |  |  | 1 |  | 6 | use Carp; | 
|  | 1 |  |  |  |  | 1 |  | 
|  | 1 |  |  |  |  | 455 |  | 
| 27 |  |  |  |  |  |  |  | 
| 28 |  |  |  |  |  |  | our ($VERSION) = ( '$Revision: 1.2 $ ' =~ / (\d+\.\d+)/ ) ; | 
| 29 |  |  |  |  |  |  |  | 
| 30 |  |  |  |  |  |  | =head2 new( $probabilities_hashref ) | 
| 31 |  |  |  |  |  |  |  | 
| 32 |  |  |  |  |  |  | Creates a new roulette wheel. Takes a hashref, which uses as keys the | 
| 33 |  |  |  |  |  |  | objects to be returned by the roulette wheel, and as values the ones | 
| 34 |  |  |  |  |  |  | that are going to be used | 
| 35 |  |  |  |  |  |  |  | 
| 36 |  |  |  |  |  |  | =cut | 
| 37 |  |  |  |  |  |  |  | 
| 38 |  |  |  |  |  |  | sub new { | 
| 39 | 1 |  |  | 1 | 1 | 709 | my $class = shift; | 
| 40 | 1 |  | 50 |  |  | 5 | my $probs_hashref = shift || die "No probabilities hash"; | 
| 41 |  |  |  |  |  |  |  | 
| 42 | 1 |  |  |  |  | 7 | my %probs = %$probs_hashref; | 
| 43 | 1 |  |  |  |  | 4 | my $self = { _accProbs => [] }; | 
| 44 |  |  |  |  |  |  |  | 
| 45 | 1 |  |  |  |  | 2 | my $acc = 0; | 
| 46 | 1 |  |  |  |  | 7 | for ( sort keys %probs ) { $acc += $probs{$_};} | 
|  | 5 |  |  |  |  | 9 |  | 
| 47 | 1 |  |  |  |  | 5 | for ( sort keys %probs ) { $probs{$_} /= $acc;} #Normalizes array | 
|  | 5 |  |  |  |  | 10 |  | 
| 48 |  |  |  |  |  |  |  | 
| 49 |  |  |  |  |  |  | #Now creates the accumulated array, putting the accumulated | 
| 50 |  |  |  |  |  |  | #probability in the first element arrayref element, and the object | 
| 51 |  |  |  |  |  |  | #in the second | 
| 52 | 1 |  |  |  |  | 2 | my $aux = 0; | 
| 53 | 1 |  |  |  |  | 5 | for ( sort keys %probs ) { | 
| 54 | 5 |  |  |  |  | 6 | push @{$self->{_accProbs}}, [$probs{$_} + $aux,$_ ]; | 
|  | 5 |  |  |  |  | 16 |  | 
| 55 | 5 |  |  |  |  | 10 | $aux += $probs{$_}; | 
| 56 |  |  |  |  |  |  | } | 
| 57 |  |  |  |  |  |  |  | 
| 58 | 1 |  |  |  |  | 4 | bless $self, $class; | 
| 59 | 1 |  |  |  |  | 5 | return $self; | 
| 60 |  |  |  |  |  |  | } | 
| 61 |  |  |  |  |  |  |  | 
| 62 |  |  |  |  |  |  | =head2 spin() | 
| 63 |  |  |  |  |  |  |  | 
| 64 |  |  |  |  |  |  | Returns a single individual whose probability is related to its fitness | 
| 65 |  |  |  |  |  |  | TODO: should return many, probably | 
| 66 |  |  |  |  |  |  |  | 
| 67 |  |  |  |  |  |  | =cut | 
| 68 |  |  |  |  |  |  |  | 
| 69 |  |  |  |  |  |  | sub spin { | 
| 70 | 10 |  |  | 10 | 1 | 5221 | my $self = shift; | 
| 71 | 10 |  |  |  |  | 14 | my $i = 0; | 
| 72 | 10 |  |  |  |  | 17 | my $rand = rand(); | 
| 73 | 10 |  |  |  |  | 39 | while ( $self->{_accProbs}[$i]->[0] < $rand ) { $i++ }; | 
|  | 21 |  |  |  |  | 43 |  | 
| 74 | 10 |  |  |  |  | 27 | return $self->{_accProbs}[$i]->[1]; | 
| 75 |  |  |  |  |  |  |  | 
| 76 |  |  |  |  |  |  | } | 
| 77 |  |  |  |  |  |  | =head1 Copyright | 
| 78 |  |  |  |  |  |  |  | 
| 79 |  |  |  |  |  |  | This file is released under the GPL. See the LICENSE file included in this distribution, | 
| 80 |  |  |  |  |  |  | or go to http://www.fsf.org/licenses/gpl.txt | 
| 81 |  |  |  |  |  |  |  | 
| 82 |  |  |  |  |  |  | CVS Info: $Date: 2010/03/16 18:39:40 $ | 
| 83 |  |  |  |  |  |  | $Header: /cvsroot/opeal/Algorithm-Evolutionary/lib/Algorithm/Evolutionary/Hash_Wheel.pm,v 1.2 2010/03/16 18:39:40 jmerelo Exp $ | 
| 84 |  |  |  |  |  |  | $Author: jmerelo $ | 
| 85 |  |  |  |  |  |  |  | 
| 86 |  |  |  |  |  |  | =cut | 
| 87 |  |  |  |  |  |  |  | 
| 88 |  |  |  |  |  |  | "The truth is by here"; | 
| 89 |  |  |  |  |  |  |  | 
| 90 |  |  |  |  |  |  | #Test code | 
| 91 |  |  |  |  |  |  | #my @array = qw( 5 4 3 2 1 ); | 
| 92 |  |  |  |  |  |  | #my $wheel = new Wheel @array; | 
| 93 |  |  |  |  |  |  |  | 
| 94 |  |  |  |  |  |  | #my @histo; | 
| 95 |  |  |  |  |  |  | #for ( 0..100 ){ | 
| 96 |  |  |  |  |  |  | #  my $s = $wheel->spin(); | 
| 97 |  |  |  |  |  |  | #  print "$s\n"; | 
| 98 |  |  |  |  |  |  | #  $histo[$s]++; | 
| 99 |  |  |  |  |  |  | #} | 
| 100 |  |  |  |  |  |  |  | 
| 101 |  |  |  |  |  |  | #for ( 0..(@histo - 1)){ | 
| 102 |  |  |  |  |  |  | #  print $_, " => $histo[$_] \n"; | 
| 103 |  |  |  |  |  |  | #} | 
| 104 |  |  |  |  |  |  |  | 
| 105 |  |  |  |  |  |  | #my @array2 = qw( 1 3 7 4 2 1 ); | 
| 106 |  |  |  |  |  |  | #my $wheel2 = new Wheel @array2; | 
| 107 |  |  |  |  |  |  |  | 
| 108 |  |  |  |  |  |  | #my @histo2; | 
| 109 |  |  |  |  |  |  | #for ( 0..100 ){ | 
| 110 |  |  |  |  |  |  | #  my $s = $wheel2->spin(); | 
| 111 |  |  |  |  |  |  | #  print "$s\n"; | 
| 112 |  |  |  |  |  |  | #  $histo2[$s]++; | 
| 113 |  |  |  |  |  |  | #} | 
| 114 |  |  |  |  |  |  |  | 
| 115 |  |  |  |  |  |  | #for ( 0..(@histo2 - 1)){ | 
| 116 |  |  |  |  |  |  | #  print $_, " => $histo2[$_] \n"; | 
| 117 |  |  |  |  |  |  | #} |