-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy paththisisneat.py
78 lines (51 loc) · 1.74 KB
/
thisisneat.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
# pip3 install gym
# pip3 install neat-python
# for gym stuff:
# apt install xvfb ffmpeg xorg-dev libsdl2-dev swig cmake
# pip3 install gym[box2d]
import multiprocessing
import os
import pickle
import neat
import numpy as np
#import cart_pole
import gym
runs_per_net = 2
# Use the NN network phenotype and the discrete actuator force function.
def eval_genome(genome, config):
net = neat.nn.FeedForwardNetwork.create(genome, config)
fitnesses = []
for runs in range(runs_per_net):
env = gym.make("BipedalWalker-v3")
observation = env.reset()
fitness = 0.0
done = False
while not done:
action = net.activate(observation)
observation, reward, done, info = env.step(action)
fitness += reward
fitnesses.append(fitness)
return np.mean(fitnesses)
def eval_genomes(genomes, config):
for genome_id, genome in genomes:
genome.fitness = eval_genome(genome, config)
def run():
# Load the config file, which is assumed to live in
# the same directory as this script.
local_dir = os.path.dirname(__file__)
config_path = os.path.join(local_dir, 'config')
config = neat.Config(neat.DefaultGenome, neat.DefaultReproduction,
neat.DefaultSpeciesSet, neat.DefaultStagnation,
config_path)
pop = neat.Population(config)
stats = neat.StatisticsReporter()
pop.add_reporter(stats)
pop.add_reporter(neat.StdOutReporter(True))
pe = neat.ParallelEvaluator(multiprocessing.cpu_count(), eval_genome)
winner = pop.run(pe.evaluate)
# Save the winner.
with open('winner', 'wb') as f:
pickle.dump(winner, f)
print(winner)
if __name__ == '__main__':
run()