Pro/Selection/RouletteDistribution.pm untested

filter: Statementstmt 0.0% 0 / 70 Branchbran 0.0% 0 / 36 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 9 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 118 CC 19 SCAR 52.7
CC
19
Cov
0%
run
306.0
roulette
12.0
new
2.0
CRAP 194.5
1 package AI::Genetic::Pro::Selection::RouletteDistribution;
2  
3 use warnings;
4 use strict;
5 use List::Util qw(min);
6 use List::MoreUtils qw(first_index);
7 #use Data::Dumper; $Data::Dumper::Sortkeys = 1;
8 #use AI::Genetic::Pro::Array::PackTemplate;
9 use Math::Random qw(
10 random_uniform
11 random_normal
12 random_beta
13 random_binomial
14 random_chi_square
15 random_exponential
16 random_poisson
17 );
18 use Carp 'croak';
19 #=======================================================================
20 sub new {
21 my ($class, $type, @params) = @_;
22 bless {
23 type => $type,
24 params => \@params,
25 }, $class;
26 }
27 #=======================================================================
28 sub roulette {
29 my ($total, $wheel) = @_;
30 my $rand = rand($total);
31 my $idx = first_index { $_->[1] > $rand } @$wheel;
32 if($idx == 0){ $idx = 1 }
33 elsif($idx == -1 ) { $idx = scalar @$wheel; }
34 return $wheel->[$idx-1]->[0];
35 }
36 #=======================================================================
37 sub run {
38 my ($self, $ga) = @_;
39
40 my ($fitness, $chromosomes) = ($ga->_fitness, $ga->chromosomes);
41 croak "You must set a number of parents for the RouletteDistribution strategy"
42 unless defined($ga->parents);
43 my $parents = $ga->parents;
44 my $high = scalar @$chromosomes;
45 my (@parents, @wheel);
46 my $const = min values %$fitness;
47 $const = $const < 0 ? abs($const) : 0;
48 my $total = 0;
49 #-------------------------------------------------------------------
50 foreach my $key (keys %$fitness){
51 $total += $fitness->{$key} + $const;
52 push @wheel, [ $key, $total ];
53 }
54 #-------------------------------------------------------------------
55 if($self->{type} eq q/uniform/){
56 push @parents,
57 pack 'I*',
58 map { roulette($total, \@wheel) }
59 random_uniform($parents, 0, $total)
60 for 0..$#$chromosomes;
61 }elsif($self->{type} eq q/normal/){
62 my $av = defined $self->{params}->[0] ? $self->{params}->[0] : $#$chromosomes/2;
63 my $sd = defined $self->{params}->[1] ? $self->{params}->[1] : $#$chromosomes;
64 push @parents,
65 pack 'I*',
66 map { roulette($total, \@wheel) }
67 map { int $_ % $high } random_normal($parents, $av, $sd)
68 for 0..$#$chromosomes;
69 }elsif($self->{type} eq q/beta/){
70 my $aa = defined $self->{params}->[0] ? $self->{params}->[0] : $parents;
71 my $bb = defined $self->{params}->[1] ? $self->{params}->[1] : $parents;
72 push @parents,
73 pack 'I*',
74 map { roulette($total, \@wheel) }
75 map { int($_ * $high) } random_beta($parents, $aa, $bb)
76 for 0..$#$chromosomes;
77 }elsif($self->{type} eq q/binomial/){
78 push @parents,
79 pack 'I*',
80 map { roulette($total, \@wheel) }
81 random_binomial($parents, $#$chromosomes, rand)
82 for 0..$#$chromosomes;
83 }elsif($self->{type} eq q/chi_square/){
84 my $df = defined $self->{params}->[0] ? $self->{params}->[0] : $#$chromosomes;
85 push @parents,
86 pack 'I*',
87 map { roulette($total, \@wheel) }
88 map { int $_ % $high } random_chi_square($parents, $df)
89 for 0..$#$chromosomes;
90 }elsif($self->{type} eq q/exponential/){
91 my $av = defined $self->{params}->[0] ? $self->{params}->[0] : $#$chromosomes/2;
92 push @parents,
93 pack 'I*',
94 map { roulette($total, \@wheel) }
95 map { int $_ % $high } random_exponential($parents, $av)
96 for 0..$#$chromosomes;
97 }elsif($self->{type} eq q/poisson/){
98 my $mu = defined $self->{params}->[0] ? $self->{params}->[0] : $#$chromosomes/2;
99 push @parents,
100 pack 'I*',
101 map { roulette($total, \@wheel) }
102 map { int $_ % $high } random_poisson($parents, $mu)
103 for 0..$#$chromosomes;
104 }else{
105 die qq/Unknown distribution "$self->{type}" in "selection"!\n/;
106 }
107
108 #-------------------------------------------------------------------
109 return \@parents;
110 }
111 #=======================================================================
112  
113 1;