| 1 | package AI::Genetic::Pro::Mutation::Rangevector; | ||||||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||||||
| 3 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 4 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 5 | 3 | ▶ | use List::MoreUtils qw(first_index); | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 6 | 3 | ▶ | use Math::Random qw(random_uniform_integer); | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 7 | #use Data::Dumper; $Data::Dumper::Sortkeys = 1; | ||||||||||||||||||||||||||||||||||||||||||||||
| 8 | #======================================================================= | ||||||||||||||||||||||||||||||||||||||||||||||
| 9 | 3 | ▶ | sub new { bless \$_[0], $_[0]; } | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 10 | #======================================================================= | ||||||||||||||||||||||||||||||||||||||||||||||
| 11 | sub run { | ||||||||||||||||||||||||||||||||||||||||||||||
| 12 | 5 | ▶ | my ($self, $ga) = @_; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
run: called CC=14 SCAR=26.7
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||||||
| 14 | # this is declared here just for speed | ||||||||||||||||||||||||||||||||||||||||||||||
| 15 | 5 | my $mutation = $ga->mutation; | |||||||||||||||||||||||||||||||||||||||||||||
| 16 | 5 | my $chromosomes = $ga->chromosomes; | |||||||||||||||||||||||||||||||||||||||||||||
| 17 | 5 | my $_translations = $ga->_translations; | |||||||||||||||||||||||||||||||||||||||||||||
| 18 | 5 | my ($fitness, $_fitness) = ($ga->fitness, $ga->_fitness); | |||||||||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||||||||
| 20 | # main loop | ||||||||||||||||||||||||||||||||||||||||||||||
| 21 | 5 | for my $idx (0..$#$chromosomes){ | |||||||||||||||||||||||||||||||||||||||||||||
| 22 | 6900 | ▶ | next if rand() >= $mutation; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||||||||
| 24 | 356 | ▶ | if($ga->variable_length){ | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 25 | 290 | my $rand = rand(); | |||||||||||||||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||||||||||||||||
| 27 | 290 | ▶ | my $min = first_index { $_ } @{$chromosomes->[$idx]}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 28 | 290 | my $range = $#{$chromosomes->[$idx]} - $min + 1; | |||||||||||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||||||||||||
| 30 | 290 | ▶ | if($rand < 0.4 and $range > 2){ | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition66%2/3
$rand < "0.4" and $range > 2
MC/DC50%1/2
$rand < "0.4" and $range > 2
$rand < "0.4"
$range > 2
Truth table66%2/3
A and B
A= $rand < "0.4"B= $range > 2
| |||||||||||||||||||||||||||||||||||||||||||||||
| 31 | 126 | ▶ | if($rand < 0.2 and $ga->variable_length > 1){ $chromosomes->[$idx]->[$min] = 0; } | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$rand < "0.2" and $ga->variable_length > 1
MC/DC100%2/2
$rand < "0.2" and $ga->variable_length > 1
$rand < "0.2"
$ga->variable_length > 1
Truth table100%3/3
A and B
A= $rand < "0.2"B= $ga->variable_length > 1
| |||||||||||||||||||||||||||||||||||||||||||||||
| 32 | 96 | else{ pop @{$chromosomes->[$idx]}; } | |||||||||||||||||||||||||||||||||||||||||||||
| 33 | 109 | ▶ | }elsif($rand < 0.8 and $range < scalar @{$_translations}){ | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%3/3
$rand < "0.8" and $range < scalar @{$_translations;}
MC/DC100%2/2
$rand < "0.8" and $range < scalar @{$_translations;}
$rand < "0.8"
$range < scalar @{$_translations;}
Truth table100%3/3
A and B
A= $rand < "0.8"B= $range < scalar @{$_translations;}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 34 | 1 | ▶ | if($rand < 0.6 and $ga->variable_length > 1 and not $chromosomes->[$idx]->[0]){ | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition33%2/6
$rand < "0.6" and $ga->variable_length > 1 and not $chromosomes->[$idx][0]
$rand < "0.6" and $ga->variable_length > 1
MC/DC0%0/3
$rand < "0.6" and $ga->variable_length > 1 and not $chromosomes->[$idx][0]
$rand < "0.6"
$ga->variable_length > 1
not $chromosomes->[$idx][0]
Truth table25%1/4
A and B and not(C)
A= $rand < "0.6"B= $ga->variable_length > 1C= not $chromosomes->[$idx][0]
| |||||||||||||||||||||||||||||||||||||||||||||||
| 35 | 0 | $chromosomes->[$idx]->[ $min - 1 ] = random_uniform_integer(1, @{$_translations->[ $min - 1 ]}[1..2]); | |||||||||||||||||||||||||||||||||||||||||||||
| 36 | 1 | }elsif(exists $_translations->[scalar @{$chromosomes->[$idx]}]){ | |||||||||||||||||||||||||||||||||||||||||||||
| 37 | 1 | push @{$chromosomes->[$idx]}, random_uniform_integer(1, @{$_translations->[scalar @{$chromosomes->[$idx]}]}[1..2]); | |||||||||||||||||||||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 39 | }else{ | ||||||||||||||||||||||||||||||||||||||||||||||
| 40 | 163 | my $id = $min + int rand($range - 1); | |||||||||||||||||||||||||||||||||||||||||||||
| 41 | 163 | $chromosomes->[$idx]->[$id] = random_uniform_integer(1, @{$_translations->[$id]}[1..2]); | |||||||||||||||||||||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 43 | }else{ | ||||||||||||||||||||||||||||||||||||||||||||||
| 44 | 66 | my $id = int rand @{$chromosomes->[$idx]}; | |||||||||||||||||||||||||||||||||||||||||||||
| 45 | 66 | $chromosomes->[$idx]->[$id] = random_uniform_integer(1, @{$_translations->[$id]}[1..2]); | |||||||||||||||||||||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||||||||||||||||
| 48 | # we need to change fitness | ||||||||||||||||||||||||||||||||||||||||||||||
| 49 | 356 | $_fitness->{$idx} = $fitness->($ga, $chromosomes->[$idx]); | |||||||||||||||||||||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||||||||||||||||
| 52 | 5 | return 1; | |||||||||||||||||||||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 54 | #======================================================================= | ||||||||||||||||||||||||||||||||||||||||||||||
| 55 | sub run0 { | ||||||||||||||||||||||||||||||||||||||||||||||
| 56 | 0 | ▶ | my ($self, $ga) = @_; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
run0: not called CC=8 SCAR=42.8
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||||||||||||||||
| 58 | # this is declared here just for speed | ||||||||||||||||||||||||||||||||||||||||||||||
| 59 | 0 | my $mutation = $ga->mutation; | |||||||||||||||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||||||||||||||||
| 61 | # main loop | ||||||||||||||||||||||||||||||||||||||||||||||
| 62 | 0 | foreach my $chromosome (@{$ga->{chromosomes}}){ | |||||||||||||||||||||||||||||||||||||||||||||
| 63 | 0 | ▶ | next if rand() <= $mutation; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||||||||||||||||
| 65 | 0 | ▶ | if($ga->variable_length){ | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 66 | 0 | my $rand = rand(); | |||||||||||||||||||||||||||||||||||||||||||||
| 67 | 0 | ▶ | if($rand < 0.33 and $#$chromosome > 1){ | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition0%0/3
$rand < "0.33" and $chromosome->$#* > 1
MC/DC0%0/2
$rand < "0.33" and $chromosome->$#* > 1
$rand < "0.33"
$chromosome->$#* > 1
Truth table0%0/3
A and B
A= $rand < "0.33"B= $chromosome->$#* > 1
| |||||||||||||||||||||||||||||||||||||||||||||||
| 68 | 0 | pop @$chromosome; | |||||||||||||||||||||||||||||||||||||||||||||
| 69 | 0 | ▶ | }elsif($rand < 0.66 and $#$chromosome < $#{$ga->_translations}){ | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
$rand < "0.66" and $chromosome->$#* < $#{$ga->_translations;}
MC/DC0%0/2
$rand < "0.66" and $chromosome->$#* < $#{$ga->_translations;}
$rand < "0.66"
$chromosome->$#* < $#{$ga->_translations;}
Truth table0%0/3
A and B
A= $rand < "0.66"B= $chromosome->$#* < $#{$ga->_translations;}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 70 | 0 | push @$chromosome, random_uniform_integer(1, @{$ga->_translations->[scalar @$chromosome]}); | |||||||||||||||||||||||||||||||||||||||||||||
| 71 | }else{ | ||||||||||||||||||||||||||||||||||||||||||||||
| 72 | 0 | my $idx = int rand @$chromosome; | |||||||||||||||||||||||||||||||||||||||||||||
| 73 | 0 | $chromosome->[$idx] = random_uniform_integer(1, @{$ga->_translations->[$idx]}); | |||||||||||||||||||||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 75 | }else{ | ||||||||||||||||||||||||||||||||||||||||||||||
| 76 | 0 | my $idx = int rand @$chromosome; | |||||||||||||||||||||||||||||||||||||||||||||
| 77 | 0 | $chromosome->[$idx] = random_uniform_integer(1, @{$ga->_translations->[$idx]}); | |||||||||||||||||||||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||||||||||||||
| 81 | 0 | return 1; | |||||||||||||||||||||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 83 | #======================================================================= | ||||||||||||||||||||||||||||||||||||||||||||||
| 84 | 3 | 1; | |||||||||||||||||||||||||||||||||||||||||||||