-
Notifications
You must be signed in to change notification settings - Fork 0
/
evolve_xor.py
137 lines (114 loc) · 3.09 KB
/
evolve_xor.py
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
from deap import base
from deap import creator
from deap import tools
import random
import numpy as np
from encoding import encoding
import math
pop_size = 500
elitism = 5
survival_threshold = 0.4
mutate_options = [
encoding.mutate_add_seed,
encoding.mutate_drop_seed,
encoding.mutate_existing_seed
]
input_data = [
{
'input': (1., 1.),
'expected': 0.
},
{
'input': (1., 0.),
'expected': 1.
},
{
'input': (0., 1.),
'expected': 1.
},
{
'input': (0., 0.),
'expected': 0.
}
]
def relu(x):
return np.maximum(0, x)
def tanh(x):
return np.tanh(x)
def sigmoid(x):
return 1/(1 + np.exp(-x))
def eval(genome, layer_defs):
nn = encoding.initialize(genome, layer_defs)
fitness = 4.
for sample in input_data:
output = encoding.forward_pass(nn, np.array(sample['input']))
fitness -= (sample['expected'] - output) ** 2
return fitness
layer_defs = [
{
'name': 'input',
'nodes': 2
},
{
'name': 'encoder_hidden',
'nodes': 2,
'activation': relu
},
{
'name': 'output',
'nodes': 1,
'activation': relu
}
]
creator.create('fitness', base.Fitness, weights=(1.,))
creator.create('individual', list, fitness=creator.fitness)
toolbox = base.Toolbox()
rng = np.random.default_rng()
toolbox.register(
'individual',
tools.initRepeat,
creator.individual,
lambda: rng.integers(0, np.iinfo(np.uint64).max, dtype=np.uint64),
1
)
toolbox.register('population', tools.initRepeat, list, toolbox.individual, pop_size)
toolbox.register('evaluate', eval, layer_defs=layer_defs)
toolbox.register('mutate', encoding.mutate_genome, mutate_options=mutate_options)
pop = toolbox.population()
best_fitness = 0.
best_genome = None
fitnesses = list(map(toolbox.evaluate, pop))
for ind, fitness in zip(pop, fitnesses):
ind.fitness.values = [fitness]
gen = 0
while best_fitness < 3.95:
gen += 1
truncated = tools.selDoubleTournament(
individuals=pop,
k=math.floor(len(pop) * survival_threshold),
fitness_size=5,
parsimony_size=1.5,
fitness_first=True
)
next_pop = truncated[:elitism]
while len(next_pop) < pop_size:
parent = toolbox.clone(random.choice(truncated))
next_pop.append(toolbox.mutate(parent))
pop = next_pop
fitnesses = list(map(toolbox.evaluate, pop))
for ind, fitness in zip(pop, fitnesses):
if fitness > best_fitness:
best_fitness = fitness
best_genome = toolbox.clone(ind)
ind.fitness.values = [fitness]
genome_length = np.array(list(map(lambda genome: len(genome), pop)))
avg_length = np.mean(genome_length)
print(f'generation: {gen}, best_fitness: {best_fitness}')
print(f'best_genome: {best_genome}')
print(f'avg_length: {avg_length}')
print('-'*80)
nn = encoding.initialize(best_genome, layer_defs)
for input in input_data:
print(f'input: {input["input"]}')
output = encoding.forward_pass(nn, np.array(input['input']))
print(f'output: {output}')