line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
|
2
|
|
|
|
|
|
|
package AI::Genetic::OpMutation; |
3
|
|
|
|
|
|
|
|
4
|
1
|
|
|
1
|
|
4
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
293
|
|
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
1; |
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
# This package implements various mutation |
9
|
|
|
|
|
|
|
# algorithms. To be used as static functions. |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
# sub bitVector(): |
12
|
|
|
|
|
|
|
# each gene is a bit: 0 or 1. arguments are mutation |
13
|
|
|
|
|
|
|
# prob. and anon list of genes. |
14
|
|
|
|
|
|
|
# returns anon list of mutated genes. |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
sub bitVector { |
17
|
0
|
|
|
0
|
1
|
|
my ($prob, $genes) = @_; |
18
|
|
|
|
|
|
|
|
19
|
0
|
|
|
|
|
|
for my $g (@$genes) { |
20
|
0
|
0
|
|
|
|
|
next if rand > $prob; |
21
|
|
|
|
|
|
|
|
22
|
0
|
0
|
|
|
|
|
$g = $g ? 0 : 1; |
23
|
|
|
|
|
|
|
} |
24
|
|
|
|
|
|
|
|
25
|
0
|
|
|
|
|
|
return $genes; |
26
|
|
|
|
|
|
|
} |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
# sub rangeVector(): |
29
|
|
|
|
|
|
|
# each gene is a floating number, and can be anything |
30
|
|
|
|
|
|
|
# within a range of two numbers. |
31
|
|
|
|
|
|
|
# arguments are mutation prob., anon list of genes, |
32
|
|
|
|
|
|
|
# and anon list of ranges. Each element in $ranges is |
33
|
|
|
|
|
|
|
# an anon list of two numbers, min and max value of |
34
|
|
|
|
|
|
|
# the corresponding gene. |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
sub rangeVector { |
37
|
0
|
|
|
0
|
1
|
|
my ($prob, $ranges, $genes) = @_; |
38
|
|
|
|
|
|
|
|
39
|
0
|
|
|
|
|
|
my $i = -1; |
40
|
0
|
|
|
|
|
|
for my $g (@$genes) { |
41
|
0
|
|
|
|
|
|
$i++; |
42
|
0
|
0
|
|
|
|
|
next if rand > $prob; |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
# now randomly choose another value from the range. |
45
|
0
|
|
|
|
|
|
my $abs = $ranges->[$i][1] - $ranges->[$i][0] + 1; |
46
|
0
|
|
|
|
|
|
$g = $ranges->[$i][0] + int rand($abs); |
47
|
|
|
|
|
|
|
} |
48
|
|
|
|
|
|
|
|
49
|
0
|
|
|
|
|
|
return $genes; |
50
|
|
|
|
|
|
|
} |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
# sub listVector(): |
53
|
|
|
|
|
|
|
# each gene is a string, and can be anything |
54
|
|
|
|
|
|
|
# from a list of possible values supplied by user. |
55
|
|
|
|
|
|
|
# arguments are mutation prob., anon list of genes, |
56
|
|
|
|
|
|
|
# and anon list of value lists. Each element in $lists |
57
|
|
|
|
|
|
|
# is an anon list of the possible values of |
58
|
|
|
|
|
|
|
# the corresponding gene. |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
sub listVector { |
61
|
0
|
|
|
0
|
1
|
|
my ($prob, $lists, $genes) = @_; |
62
|
|
|
|
|
|
|
|
63
|
0
|
|
|
|
|
|
my $i = -1; |
64
|
0
|
|
|
|
|
|
for my $g (@$genes) { |
65
|
0
|
|
|
|
|
|
$i++; |
66
|
0
|
0
|
|
|
|
|
next if rand > $prob; |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
# now randomly choose another value from the lists. |
69
|
0
|
|
|
|
|
|
my $new; |
70
|
|
|
|
|
|
|
|
71
|
0
|
0
|
|
|
|
|
if (@{$lists->[$i]} == 1) { |
|
0
|
|
|
|
|
|
|
72
|
0
|
|
|
|
|
|
$new = $lists->[$i][0]; |
73
|
|
|
|
|
|
|
} else { |
74
|
0
|
|
|
|
|
|
do { |
75
|
0
|
|
|
|
|
|
$new = $lists->[$i][rand @{$lists->[$i]}]; |
|
0
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
} while $new eq $g; |
77
|
|
|
|
|
|
|
} |
78
|
|
|
|
|
|
|
|
79
|
0
|
|
|
|
|
|
$g = $new; |
80
|
|
|
|
|
|
|
} |
81
|
|
|
|
|
|
|
|
82
|
0
|
|
|
|
|
|
return $genes; |
83
|
|
|
|
|
|
|
} |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
__END__ |