| 1 | 1 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 2 | 1 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 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<Algorithm::Evolutionary::Op::CanonicalGA>. It's similar to | ||||||||||||
| 18 | L<Algorithm::Evolutionary::Wheel>, 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 | ▶ | use Carp; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 27 | |||||||||||||
| 28 | 1 | 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 | 1 | ▶ | sub new { | ||||||||||
|
Subroutine
new: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||
| 39 | 1 | my $class = shift; | |||||||||||
| 40 | 1 | ▶ | my $probs_hashref = shift || die "No probabilities hash"; | ||||||||||
|
Condition50%1/2
shift() || die('No probabilities hash')
MC/DC0%0/1
shift() || die('No probabilities hash')
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||
| 41 | |||||||||||||
| 42 | 1 | my %probs = %$probs_hashref; | |||||||||||
| 43 | 1 | my $self = { _accProbs => [] }; | |||||||||||
| 44 | |||||||||||||
| 45 | 1 | my $acc = 0; | |||||||||||
| 46 | 1 | for ( sort keys %probs ) { $acc += $probs{$_};} | |||||||||||
| 47 | 1 | for ( sort keys %probs ) { $probs{$_} /= $acc;} #Normalizes array | |||||||||||
| 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 | my $aux = 0; | |||||||||||
| 53 | 1 | for ( sort keys %probs ) { | |||||||||||
| 54 | 5 | push @{$self->{_accProbs}}, [$probs{$_} + $aux,$_ ]; | |||||||||||
| 55 | 5 | $aux += $probs{$_}; | |||||||||||
| 56 | } | ||||||||||||
| 57 | |||||||||||||
| 58 | 1 | bless $self, $class; | |||||||||||
| 59 | 1 | 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 | 10 | ▶ | sub spin { | ||||||||||
|
Subroutine
spin: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||
| 70 | 10 | my $self = shift; | |||||||||||
| 71 | 10 | my $i = 0; | |||||||||||
| 72 | 10 | my $rand = rand(); | |||||||||||
| 73 | 10 | ▶ | while ( $self->{_accProbs}[$i]->[0] < $rand ) { $i++ }; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 74 | 10 | 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: /media/Backup/Repos/opeal/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 | 1 | "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 | #} | ||||||||||||