-
Notifications
You must be signed in to change notification settings - Fork 2
/
test.py
61 lines (45 loc) · 1.83 KB
/
test.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
"""
2-input XOR example -- this is most likely the simplest possible example.
"""
from common import load_df, df_to_ML_data
from myneat import LoggingReporter
from sklearn.metrics import accuracy_score
import os
import pickle
import neat
import neat_visualization as visualize
import logging
import sys
logging.basicConfig(format='%(asctime)s | %(levelname)s : %(message)s',level=logging.INFO,
filename='genetic_neat.log', filemode='w+')
log = logging.getLogger(__name__)
X_train, X_test, y_train, y_test = df_to_ML_data(load_df())
def eval_genomes(genomes, config):
for _, genome in genomes:
net = neat.nn.FeedForwardNetwork.create(genome, config)
y_pred = [net.activate(Xi) for Xi in X_train]
genome.fitness = accuracy_score(y_train, y_pred)
def run(config_file):
# Load configuration.
config = neat.Config(neat.DefaultGenome, neat.DefaultReproduction,
neat.DefaultSpeciesSet, neat.DefaultStagnation,
config_file)
# Create the population, which is the top-level object for a NEAT run.
p = neat.Population(config)
# Add a stdout reporter to show progress in the terminal.
p.add_reporter(LoggingReporter(True))
stats = neat.StatisticsReporter()
p.add_reporter(stats)
p.add_reporter(neat.Checkpointer(5))
# Run for up to 300 generations.
winner = p.run(eval_genomes, 300)
# Display the winning genome.
log.info('\nBest genome:\n%s', winner)
node_names = {-1:'A', -2: 'B', 0:'A XOR B'}
visualize.draw_net(config, winner, True, node_names=node_names)
visualize.plot_stats(stats, ylog=False, view=True)
visualize.plot_species(stats, view=True)
p = neat.Checkpointer.restore_checkpoint('neat-checkpoint-4')
p.run(eval_genomes, 10)
if __name__ == '__main__':
run('genetic_neat.ini')