-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathequalityga.jl
68 lines (49 loc) · 1.5 KB
/
equalityga.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
61
62
63
64
65
66
67
68
module equalityga
import Base.isless
using GeneticAlgorithms
mutable struct EqualityMonster <: Entity
abcde::Array
fitness
EqualityMonster() = new(Array{Int}(5), nothing)
EqualityMonster(abcde) = new(abcde, nothing)
end
function isless(lhs::EqualityMonster, rhs::EqualityMonster)
abs(lhs.fitness) > abs(rhs.fitness)
end
function create_entity(num)
# for simplicity sake, let's limit the values for abcde to be integers in [-42, 42]
EqualityMonster(rand(Int, 5) % 43)
end
function fitness(ent)
# we want the expression `a + 2b + 3c + 4d + 5e - 42` to be as close to 0 as possible
score = ent.abcde[1] + 2 * ent.abcde[2] + 3 * ent.abcde[3] + 4 * ent.abcde[4] + 5 * ent.abcde[5]
println(score - 42)
abs(score - 42)
end
function group_entities(grouped::Channel, pop)
println("BEST: ", pop[1])
if pop[1].fitness == 0
return
end
# simple naive groupings that pair the best entitiy with every other
for i in 1:length(pop)
put!(grouped, [1, i])
end
end
function crossover(group)
child = EqualityMonster()
# grab each element from a random parent
num_parents = length(group)
for i in 1:length(group[1].abcde)
parent = (rand(UInt) % num_parents) + 1
child.abcde[i] = group[parent].abcde[i]
end
child
end
function mutate(ent)
# let's go crazy and mutate 20% of the time
rand(Float64) < 0.8 && return
rand_element = rand(UInt) % 5 + 1
ent.abcde[rand_element] = rand(Int) % 43
end
end