line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
|
2
|
|
|
|
|
|
|
package AI::Genetic::OpCrossover; |
3
|
|
|
|
|
|
|
|
4
|
1
|
|
|
1
|
|
4
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
405
|
|
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
1; |
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
# sub vectorSinglePoint(): |
9
|
|
|
|
|
|
|
# Single point crossover. |
10
|
|
|
|
|
|
|
# arguments are crossover prob, two |
11
|
|
|
|
|
|
|
# anon lists of genes (parents). |
12
|
|
|
|
|
|
|
# If crossover occurs, returns two anon lists |
13
|
|
|
|
|
|
|
# of children genes. If no crossover, returns 0. |
14
|
|
|
|
|
|
|
# both parents have to be of same length. |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
sub vectorSinglePoint { |
17
|
0
|
|
|
0
|
1
|
|
my ($prob, $mom, $dad) = @_; |
18
|
|
|
|
|
|
|
|
19
|
0
|
0
|
|
|
|
|
return 0 if rand > $prob; |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
# get single index from 1 to $#{$dad} |
22
|
0
|
|
|
|
|
|
my $ind = 1 + int rand $#{$dad}; |
|
0
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
|
24
|
0
|
|
|
|
|
|
my @c1 = (@$mom[0 .. $ind - 1], |
25
|
0
|
|
|
|
|
|
@$dad[$ind .. $#{$dad}]); |
26
|
0
|
|
|
|
|
|
my @c2 = (@$dad[0 .. $ind - 1], |
27
|
0
|
|
|
|
|
|
@$mom[$ind .. $#{$dad}]); |
28
|
|
|
|
|
|
|
|
29
|
0
|
|
|
|
|
|
return (\@c1, \@c2); |
30
|
|
|
|
|
|
|
} |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
# sub vectorTwoPoint(): |
33
|
|
|
|
|
|
|
# Two point crossover. |
34
|
|
|
|
|
|
|
# arguments are crossover prob, two |
35
|
|
|
|
|
|
|
# anon lists of genes (parents). |
36
|
|
|
|
|
|
|
# If crossover occurs, returns two anon lists |
37
|
|
|
|
|
|
|
# of children genes. If no crossover, returns 0. |
38
|
|
|
|
|
|
|
# both parents have to be of same length. |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
sub vectorTwoPoint { |
41
|
0
|
|
|
0
|
1
|
|
my ($prob, $mom, $dad) = @_; |
42
|
|
|
|
|
|
|
|
43
|
0
|
0
|
|
|
|
|
return 0 if rand > $prob; |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
# get first index from 1 to $#{$dad}-1 |
46
|
0
|
|
|
|
|
|
my $ind1 = 1 + int rand($#{$dad} - 1); |
|
0
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
# get second index from $ind1 to $#{$dad} |
49
|
0
|
|
|
|
|
|
my $ind2 = $ind1 + 1 + int rand($#{$dad} - $ind1); |
|
0
|
|
|
|
|
|
|
50
|
0
|
|
|
|
|
|
my @c1 = (@$mom[0 .. $ind1 - 1], |
51
|
|
|
|
|
|
|
@$dad[$ind1 .. $ind2 - 1], |
52
|
0
|
|
|
|
|
|
@$mom[$ind2 .. $#{$dad}]); |
53
|
|
|
|
|
|
|
|
54
|
0
|
|
|
|
|
|
my @c2 = (@$dad[0 .. $ind1 - 1], |
55
|
|
|
|
|
|
|
@$mom[$ind1 .. $ind2 - 1], |
56
|
0
|
|
|
|
|
|
@$dad[$ind2 .. $#{$dad}]); |
57
|
|
|
|
|
|
|
|
58
|
0
|
|
|
|
|
|
return (\@c1, \@c2); |
59
|
|
|
|
|
|
|
} |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
# sub vectorUniform(): |
62
|
|
|
|
|
|
|
# Uniform crossover. |
63
|
|
|
|
|
|
|
# arguments are crossover prob, two |
64
|
|
|
|
|
|
|
# anon lists of genes (parents). |
65
|
|
|
|
|
|
|
# If crossover occurs, returns two anon lists |
66
|
|
|
|
|
|
|
# of children genes. If no crossover, returns 0. |
67
|
|
|
|
|
|
|
# both parents have to be of same length. |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
sub vectorUniform { |
70
|
0
|
|
|
0
|
1
|
|
my ($prob, $mom, $dad) = @_; |
71
|
|
|
|
|
|
|
|
72
|
0
|
0
|
|
|
|
|
return 0 if rand > $prob; |
73
|
|
|
|
|
|
|
|
74
|
0
|
|
|
|
|
|
my (@c1, @c2); |
75
|
0
|
|
|
|
|
|
for my $i (0 .. $#{$dad}) { |
|
0
|
|
|
|
|
|
|
76
|
0
|
0
|
|
|
|
|
if (rand > 0.5) { |
77
|
0
|
|
|
|
|
|
push @c1 => $mom->[$i]; |
78
|
0
|
|
|
|
|
|
push @c2 => $dad->[$i]; |
79
|
|
|
|
|
|
|
} else { |
80
|
0
|
|
|
|
|
|
push @c2 => $mom->[$i]; |
81
|
0
|
|
|
|
|
|
push @c1 => $dad->[$i]; |
82
|
|
|
|
|
|
|
} |
83
|
|
|
|
|
|
|
} |
84
|
|
|
|
|
|
|
|
85
|
0
|
|
|
|
|
|
return (\@c1, \@c2); |
86
|
|
|
|
|
|
|
} |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
__END__ |