| 1 | package AI::Genetic::Pro::Mutation::Bitvector; | ||||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||||
| 3 | 394 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||
| 4 | 394 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||
| 5 | #use Data::Dumper; $Data::Dumper::Sortkeys = 1; | ||||||||||||||||||||||||||||||||||||||||||||
| 6 | #======================================================================= | ||||||||||||||||||||||||||||||||||||||||||||
| 7 | 394 | ▶ | sub new { bless \$_[0], $_[0]; } | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||
| 8 | #======================================================================= | ||||||||||||||||||||||||||||||||||||||||||||
| 9 | sub run { | ||||||||||||||||||||||||||||||||||||||||||||
| 10 | 404 | ▶ | my ($self, $ga) = @_; | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
run: called CC=17 SCAR=28.4
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||||||||||
| 12 | # this is declared here just for speed | ||||||||||||||||||||||||||||||||||||||||||||
| 13 | 404 | my $mutation = $ga->mutation; | |||||||||||||||||||||||||||||||||||||||||||
| 14 | 404 | my $chromosomes = $ga->chromosomes; | |||||||||||||||||||||||||||||||||||||||||||
| 15 | 404 | my $_translations = $ga->_translations; | |||||||||||||||||||||||||||||||||||||||||||
| 16 | 404 | my ($fitness, $_fitness) = ($ga->fitness, $ga->_fitness); | |||||||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||||||||
| 18 | # main loop | ||||||||||||||||||||||||||||||||||||||||||||
| 19 | 404 | for my $idx (0..$#$chromosomes){ | |||||||||||||||||||||||||||||||||||||||||||
| 20 | 4568 | ▶ | next if rand() >= $mutation; | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||||||||
| 22 | 233 | ▶ | if($ga->variable_length){ | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 23 | 29 | my $rand = rand(); | |||||||||||||||||||||||||||||||||||||||||||
| 24 | 29 | ▶ | if($rand < 0.16 and $#{$chromosomes->[$idx]} > 1){ | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 5 branches
Condition66%2/3
$rand < "0.16" and $#{$$chromosomes[$idx];} > 1
MC/DC50%1/2
$rand < "0.16" and $#{$$chromosomes[$idx];} > 1
$rand < "0.16"
$#{$$chromosomes[$idx];} > 1
Truth table66%2/3
A and B
A= $rand < "0.16"B= $#{$$chromosomes[$idx];} > 1
| |||||||||||||||||||||||||||||||||||||||||||||
| 25 | 3 | pop @{$chromosomes->[$idx]}; | |||||||||||||||||||||||||||||||||||||||||||
| 26 | 9 | ▶ | }elsif($rand < 0.32 and $#{$chromosomes->[$idx]} > 1){ | ||||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$rand < "0.32" and $#{$$chromosomes[$idx];} > 1
MC/DC50%1/2
$rand < "0.32" and $#{$$chromosomes[$idx];} > 1
$rand < "0.32"
$#{$$chromosomes[$idx];} > 1
Truth table66%2/3
A and B
A= $rand < "0.32"B= $#{$$chromosomes[$idx];} > 1
| |||||||||||||||||||||||||||||||||||||||||||||
| 27 | 9 | shift @{$chromosomes->[$idx]}; | |||||||||||||||||||||||||||||||||||||||||||
| 28 | 3 | ▶ | }elsif($rand < 0.48 and $#{$chromosomes->[$idx]} < $#$_translations){ | ||||||||||||||||||||||||||||||||||||||||||
|
Condition100%3/3
$rand < "0.48" and $#{$$chromosomes[$idx];} < $_translations->$#*
MC/DC100%2/2
$rand < "0.48" and $#{$$chromosomes[$idx];} < $_translations->$#*
$rand < "0.48"
$#{$$chromosomes[$idx];} < $_translations->$#*
Truth table100%3/3
A and B
A= $rand < "0.48"B= $#{$$chromosomes[$idx];} < $_translations->$#*
| |||||||||||||||||||||||||||||||||||||||||||||
| 29 | 1 | ▶ | push @{$chromosomes->[$idx]}, rand > 0.5 ? 0 : 1; | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 30 | 6 | ▶ | }elsif($rand < 0.64 and $#{$chromosomes->[$idx]} < $#$_translations){ | ||||||||||||||||||||||||||||||||||||||||||
|
Condition100%3/3
$rand < "0.64" and $#{$$chromosomes[$idx];} < $_translations->$#*
MC/DC100%2/2
$rand < "0.64" and $#{$$chromosomes[$idx];} < $_translations->$#*
$rand < "0.64"
$#{$$chromosomes[$idx];} < $_translations->$#*
Truth table100%3/3
A and B
A= $rand < "0.64"B= $#{$$chromosomes[$idx];} < $_translations->$#*
| |||||||||||||||||||||||||||||||||||||||||||||
| 31 | 1 | ▶ | unshift @{$chromosomes->[$idx]}, rand > 0.5 ? 0 : 1; | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 32 | }elsif($rand < 0.8){ | ||||||||||||||||||||||||||||||||||||||||||||
| 33 | ##tied(@{$chromosomes->[$idx]})->reverse; | ||||||||||||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||||||||||||
| 35 | #if( my $obj = tied( @{ $chromosomes->[ $idx ] } ) ){ | ||||||||||||||||||||||||||||||||||||||||||||
| 36 | # $obj->reverse; | ||||||||||||||||||||||||||||||||||||||||||||
| 37 | #}else{ | ||||||||||||||||||||||||||||||||||||||||||||
| 38 | # @{ $chromosomes->[ $idx ] } = reverse @{ $chromosomes->[ $idx ] }; | ||||||||||||||||||||||||||||||||||||||||||||
| 39 | #} | ||||||||||||||||||||||||||||||||||||||||||||
| 40 | #@{ $chromosomes->[ $idx ] } = reverse @{ $chromosomes->[ $idx ] }; | ||||||||||||||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||||||||||||
| 42 | }else{ | ||||||||||||||||||||||||||||||||||||||||||||
| 43 | 5 | my $id = int rand @{$chromosomes->[$idx]}; | |||||||||||||||||||||||||||||||||||||||||||
| 44 | 5 | ▶ | $chromosomes->[$idx]->[$id] = $chromosomes->[$idx]->[$id] ? 0 : 1; | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 46 | }else{ | ||||||||||||||||||||||||||||||||||||||||||||
| 47 | 204 | my $id = int rand @{$chromosomes->[$idx]}; | |||||||||||||||||||||||||||||||||||||||||||
| 48 | 204 | ▶ | $chromosomes->[$idx]->[$id] = $chromosomes->[$idx]->[$id] ? 0 : 1; | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 50 | # we need to change fitness | ||||||||||||||||||||||||||||||||||||||||||||
| 51 | 233 | $_fitness->{$idx} = $fitness->($ga, $chromosomes->[$idx]); | |||||||||||||||||||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||||||||||||
| 54 | 404 | return 1; | |||||||||||||||||||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 56 | #======================================================================= | ||||||||||||||||||||||||||||||||||||||||||||
| 57 | # too slow; mutation is too dangerous in this solution | ||||||||||||||||||||||||||||||||||||||||||||
| 58 | sub run0 { | ||||||||||||||||||||||||||||||||||||||||||||
| 59 | 0 | ▶ | my ($self, $ga) = @_; | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
run0: not called CC=6 SCAR=37.4
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||||||||||||||
| 61 | 0 | my $mutation = $ga->mutation; # this is declared here just for speed | |||||||||||||||||||||||||||||||||||||||||||
| 62 | 0 | foreach my $chromosome (@{$ga->{chromosomes}}){ | |||||||||||||||||||||||||||||||||||||||||||
| 63 | 0 | ▶ | if(rand() < $mutation){ tied(@$chromosome)->reverse; } | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 64 | else{ | ||||||||||||||||||||||||||||||||||||||||||||
| 65 | 0 | for(0..$#$chromosome){ | |||||||||||||||||||||||||||||||||||||||||||
| 66 | 0 | ▶ | next if rand > $mutation; | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 67 | 0 | ▶ | $chromosome->[$_] = $chromosome->[$_] ? 0 : 1; | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||||||||||||
| 72 | 0 | return 1; | |||||||||||||||||||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 74 | #======================================================================= | ||||||||||||||||||||||||||||||||||||||||||||
| 75 | 394 | 1; | |||||||||||||||||||||||||||||||||||||||||||