-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNEAT_Atari.py
More file actions
53 lines (48 loc) · 1.27 KB
/
Copy pathNEAT_Atari.py
File metadata and controls
53 lines (48 loc) · 1.27 KB
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
import os
import neat
from ale_python_interface import ALEInterface
import cv2
import numpy as np
import random
ale=ALEInterface()
ale.setInt("frame_skip", 4)
ale.setInt("random_seed", 123)
#ale.setBool('sound', True)
#ale.setBool('display_screen', True)
ale.loadROM("roms/pong.bin")
acts = ale.getLegalActionSet()
def get_screen():
screen = ale.getScreenGrayscale()
resized = cv2.resize(screen, (1,7056))
return resized
def gen_pop():
config_path='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))
pop.add_reporter(neat.Checkpointer(25, 900))
print "reached"
gen_best = pop.run(e, 25)
def e(gs, config):
actionset = ale.getLegalActionSet()
nets=[]
for gid, g in gs:
nets.append((g, neat.nn.FeedForwardNetwork.create(g, config)))
for g, net in nets:
ale.reset_game()
r=0.0
while not ale.game_over():
o=get_screen()
if random.random()<0.2:
action = actionset[np.random.randint(actionset.size)]
else:
output = net.activate(o)
action = np.argmax(output)
r+=ale.act(action)
print(r)
g.fitness = r
if __name__ == '__main__':
gen_pop()