| 1 | package AI::Genetic::Pro::Crossover::PMX; | ||||||||||
| 2 | |||||||||||
| 3 | 1 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 1 | ▶ | use List::MoreUtils qw(indexes); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | #use Data::Dumper; $Data::Dumper::Sortkeys = 1; | ||||||||||
| 7 | #======================================================================= | ||||||||||
| 8 | 1 | ▶ | sub new { bless \$_[0], $_[0]; } | ||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 9 | #======================================================================= | ||||||||||
| 10 | sub dup { | ||||||||||
| 11 | 3076 | ▶ | my ($ar) = @_; | ||||||||
|
Subroutine
dup: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||
| 12 | |||||||||||
| 13 | 3076 | my %seen; | |||||||||
| 14 | 3076 | ▶ | my @dup = grep { if($seen{$_}){ 1 }else{ $seen{$_} = 1; 0} } @$ar; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 15 | 3076 | ▶ | return \@dup if @dup; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 16 | 2006 | return; | |||||||||
| 17 | } | ||||||||||
| 18 | #======================================================================= | ||||||||||
| 19 | sub run { | ||||||||||
| 20 | 1 | ▶ | my ($self, $ga) = @_; | ||||||||
|
Subroutine
run: called CC=9 SCAR=22.0
Pod
undocumented
| |||||||||||
| 21 | |||||||||||
| 22 | 1 | my ($chromosomes, $parents, $crossover) = ($ga->chromosomes, $ga->_parents, $ga->crossover); | |||||||||
| 23 | 1 | my ($fitness, $_fitness) = ($ga->fitness, $ga->_fitness); | |||||||||
| 24 | 1 | my @children; | |||||||||
| 25 | #------------------------------------------------------------------- | ||||||||||
| 26 | 1 | ▶ | while(my $elders = shift @$parents){ | ||||||||
|
Branch: 1 branch
| |||||||||||
| 27 | 1124 | my @elders = unpack 'I*', $elders; | |||||||||
| 28 | |||||||||||
| 29 | 1124 | ▶ | unless(scalar @elders){ | ||||||||
|
Branch: 1 branch
| |||||||||||
| 30 | 0 | push @children, $chromosomes->[$elders[0]]; | |||||||||
| 31 | 0 | next; | |||||||||
| 32 | } | ||||||||||
| 33 | |||||||||||
| 34 | 1124 | my @points = sort { $a <=> $b } map { 1 + int(rand $#{$chromosomes->[0]}) } 0..1; | |||||||||
| 35 | |||||||||||
| 36 | @elders = sort { | ||||||||||
| 37 | 1003 | my @av = @{$a}[$points[0]..$points[1]-1]; | |||||||||
| 38 | 1003 | my @bv = splice @$b, $points[0], $points[1] - $points[0], @av; | |||||||||
| 39 | 1003 | splice @$a, $points[0], $points[1] - $points[0], @bv; | |||||||||
| 40 | |||||||||||
| 41 | 1003 | my %av; @av{@av} = @bv; | |||||||||
| 42 | 1003 | my %bv; @bv{@bv} = @av; | |||||||||
| 43 | |||||||||||
| 44 | 1003 | ▶ | while(my $dup = dup($a)){ | ||||||||
|
Branch: 1 branch
| |||||||||||
| 45 | 535 | foreach my $val (@$dup){ | |||||||||
| 46 | 654 | ▶ | my ($ind) = grep { $_ < $points[0] or $_ >= $points[1] } indexes { $_ == $val } @$a; | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
| |||||||||||
| 47 | 654 | $a->[$ind] = $bv{$val}; | |||||||||
| 48 | } | ||||||||||
| 49 | } | ||||||||||
| 50 | |||||||||||
| 51 | 1003 | ▶ | while(my $dup = dup($b)){ | ||||||||
|
Branch: 1 branch
| |||||||||||
| 52 | 535 | foreach my $val (@$dup){ | |||||||||
| 53 | 654 | ▶ | my ($ind) = grep { $_ < $points[0] or $_ >= $points[1] } indexes { $_ == $val } @$b; | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
| |||||||||||
| 54 | 654 | $b->[$ind] = $av{$val}; | |||||||||
| 55 | } | ||||||||||
| 56 | } | ||||||||||
| 57 | |||||||||||
| 58 | 0; | ||||||||||
| 59 | } map { | ||||||||||
| 60 | 1124 | $chromosomes->[$_]->clone | |||||||||
| 61 | } @elders; | ||||||||||
| 62 | |||||||||||
| 63 | |||||||||||
| 64 | 1124 | my %elders = map { $_ => $fitness->($ga, $elders[$_]) } 0..$#elders; | |||||||||
| 65 | 1124 | my $max = (sort { $elders{$a} <=> $elders{$b} } keys %elders)[-1]; | |||||||||
| 66 | 1124 | $_fitness->{scalar(@children)} = $elders{$max}; | |||||||||
| 67 | |||||||||||
| 68 | 1124 | push @children, $elders[$max]; | |||||||||
| 69 | } | ||||||||||
| 70 | #------------------------------------------------------------------- | ||||||||||
| 71 | |||||||||||
| 72 | 1 | return \@children; | |||||||||
| 73 | } | ||||||||||
| 74 | #======================================================================= | ||||||||||
| 75 | 1 | 1; | |||||||||