| 1 | package AI::Genetic::Pro::Selection::Roulette; | ||||||||||||
| 2 | |||||||||||||
| 3 | 366 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 4 | 366 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 5 | #use Data::Dumper; $Data::Dumper::Sortkeys = 1; | ||||||||||||
| 6 | 366 | ▶ | use List::Util qw(sum min); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 7 | 366 | ▶ | use List::MoreUtils qw(first_index); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 8 | 366 | ▶ | use Carp 'croak'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 9 | |||||||||||||
| 10 | #======================================================================= | ||||||||||||
| 11 | 375 | ▶ | sub new { bless \$_[0], $_[0]; } | ||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 12 | #======================================================================= | ||||||||||||
| 13 | sub run { | ||||||||||||
| 14 | 387 | ▶ | my ($self, $ga) = @_; | ||||||||||
|
Subroutine
run: called CC=13 SCAR=25.7
Pod
undocumented
| |||||||||||||
| 15 | |||||||||||||
| 16 | 387 | my ($fitness) = ($ga->_fitness); | |||||||||||
| 17 | 387 | my (@parents, @elders); | |||||||||||
| 18 | #------------------------------------------------------------------- | ||||||||||||
| 19 | 387 | my $count = $#{$ga->chromosomes}; | |||||||||||
| 20 | 387 | my $const = min values %$fitness; | |||||||||||
| 21 | 387 | ▶ | $const = $const < 0 ? abs($const) : 0; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 22 | 387 | ▶ | my $total = sum( map { $_ < 0 ? $_ + $const : $_ } values %$fitness); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 23 | 387 | ▶ | $total ||= 1; | ||||||||||
|
Condition50%1/2
$total ||= 1
MC/DC0%0/1
$total ||= 1
$total
Truth table50%1/2
A
A= $total
| |||||||||||||
| 24 | |||||||||||||
| 25 | # elders | ||||||||||||
| 26 | 387 | for my $idx (0..$count){ | |||||||||||
| 27 | 16532 | push @elders, $idx for 1..int((($fitness->{$idx} + $const) / $total) * $count); | |||||||||||
| 28 | } | ||||||||||||
| 29 | |||||||||||||
| 30 | 387 | ▶ | if((my $add = $count - scalar @elders) > 0){ | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 31 | 382 | my $idx = $elders[rand @elders]; | |||||||||||
| 32 | 382 | push @elders, int rand($count) for 0..$add; | |||||||||||
| 33 | } | ||||||||||||
| 34 | |||||||||||||
| 35 | 387 | ▶ | croak "You must set a crossover probability to use the Roulette strategy" | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 36 | unless defined($ga->crossover); | ||||||||||||
| 37 | 387 | ▶ | croak "You must set a number of parents to use the Roulette strategy" | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 38 | unless defined($ga->parents); | ||||||||||||
| 39 | |||||||||||||
| 40 | # parents | ||||||||||||
| 41 | 387 | for(0..$count){ | |||||||||||
| 42 | 16532 | ▶ | if(rand > $ga->crossover){ | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 43 | 1643 | push @parents, pack 'I*', $elders[ rand @elders ] | |||||||||||
| 44 | }else{ | ||||||||||||
| 45 | 14889 | my @group; | |||||||||||
| 46 | 14889 | push @group, $elders[ rand @elders ] for 1..$ga->parents; | |||||||||||
| 47 | 14889 | push @parents, pack 'I*', @group; | |||||||||||
| 48 | } | ||||||||||||
| 49 | } | ||||||||||||
| 50 | |||||||||||||
| 51 | #------------------------------------------------------------------- | ||||||||||||
| 52 | 387 | return \@parents; | |||||||||||
| 53 | } | ||||||||||||
| 54 | #======================================================================= | ||||||||||||
| 55 | |||||||||||||
| 56 | 366 | 1; | |||||||||||