-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmutation.jl
60 lines (54 loc) · 1.87 KB
/
mutation.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
module Mutation
function perform_all_mutations!(population, mutation_func)
differences = []
for entity in population
mutated_genotypes = []
for genotype in entity.genotype
push!(mutated_genotypes, binary_mutation(genotype))
end
difference = hamming_distance(mutated_genotypes, entity.genotype)
push!(differences, difference)
reset_phenotypes!(entity, mutated_genotypes)
entity.genotype = mutated_genotypes
end
return differences
end
function binary_mutation(orig_genotype)
probability = (1 / length(orig_genotype)) * 0.000001
mutated_genotype = ""
for (index, bit) in enumerate(orig_genotype)
selector = rand(Float16, 1)[1]
if selector < probability
if bit == '0'
mutated_genotype *= '1'
else
mutated_genotype *= '0'
end
else
mutated_genotype *= bit
end
end
orig_genotype = mutated_genotype
end
function hamming_distance(mutated, original)
difference = 0
for (gene1, gene2) in zip(mutated, original)
for (ch1, ch2) in zip(gene1, gene2)
difference += ch1 != ch2
end
difference += abs(length(gene1) - length(gene2))
end
return difference
end
function reset_phenotypes!(entity, mutated_genotypes)
_range = entity.real_range
step = (_range[2] - _range[1]) / entity.n_genes
new_phenotypes = []
for genotype in mutated_genotypes
decimal = parse(Int, genotype, base = 2)
new_phen = _range[1] + (decimal * step)
push!(new_phenotypes, new_phen)
end
entity.phenotype = new_phenotypes
end
end