-
Notifications
You must be signed in to change notification settings - Fork 0
/
tutorial.py
142 lines (110 loc) · 4.44 KB
/
tutorial.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
138
139
140
141
142
import pygame
from pong import Game
import neat
import os
import pickle
class PongGame:
def __init__(self, window, width, height):
self.game = Game(window, width, height)
self.left_paddle = self.game.left_paddle
self.right_paddle = self.game.right_paddle
self.ball = self.game.ball
def test_ai(self, genome, config):
net = neat.nn.FeedForwardNetwork.create(genome, config)
run = True
clock = pygame.time.Clock()
while run:
clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
break
keys = pygame.key.get_pressed()
if keys[pygame.K_w]:
self.game.move_paddle(left=True, up=True)
if keys[pygame.K_s]:
self.game.move_paddle(left=True, up=False)
output = net.activate(
(self.right_paddle.y, self.ball.y, abs(self.right_paddle.x - self.ball.x)))
decision = output.index(max(output))
if decision == 0:
pass
elif decision == 1:
self.game.move_paddle(left=False, up=True)
else:
self.game.move_paddle(left=False, up=False)
game_info = self.game.loop()
self.game.draw(True, False)
pygame.display.update()
pygame.quit()
def train_ai(self, genome1, genome2, config):
net1 = neat.nn.FeedForwardNetwork.create(genome1, config)
net2 = neat.nn.FeedForwardNetwork.create(genome2, config)
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
quit()
output1 = net1.activate(
(self.left_paddle.y, self.ball.y, abs(self.left_paddle.x - self.ball.x)))
decision1 = output1.index(max(output1))
if decision1 == 0:
pass
elif decision1 == 1:
self.game.move_paddle(left=True, up=True)
else:
self.game.move_paddle(left=True, up=False)
output2 = net2.activate(
(self.right_paddle.y, self.ball.y, abs(self.right_paddle.x - self.ball.x)))
decision2 = output2.index(max(output2))
if decision2 == 0:
pass
elif decision2 == 1:
self.game.move_paddle(left=False, up=True)
else:
self.game.move_paddle(left=False, up=False)
game_info = game.loop()
game.draw(draw_score=False, draw_hits=True)
pygame.display.update()
if game_info.left_score >= 1 or game_info.right_score >= 1 or game_info.left_hits > 50:
self.calculate_fitness(genome1, genome2, game_info)
break
def calculate_fitness(self, genome1, genome2, game_info):
genome1.fitness += game_info.left_hits
genome2.fitness += game_info.right_hits
def eval_genomes(genomes, config):
width, height = 700, 500
window = pygame.display.set_mode((width, height))
for i, (genome_id1, genome1) in enumerate(genomes):
if i == len(genomes) - 1:
break
genome1.fitness = 0
for genome_id2, genome2 in genomes[i+1:]:
genome2.fitness = 0 if genome2.fitness == None else genome2.fitness
game = PongGame(window, width, height)
game.train_ai(genome1, genome2, config)
def run_neat(config):
p = neat.Checkpointer.restore_checkpoint('neat-checkpoint-7')
#p = neat.Population(config)
p.add_reporter(neat.StdOutReporter(True))
stats = neat.StatisticsReporter()
p.add_reporter(stats)
p.add_reporter(neat.Checkpointer(1))
winner = p.run(eval_genomes, 1)
with open("best.pickle", "wb") as f:
pickle.dump(winner, f)
def test_ai(config):
width, height = 700, 500
window = pygame.display.set_mode((width, height))
with open("best.pickle", "rb") as f:
winner = pickle.load(f)
game = PongGame(window, width, height)
game.test_ai(winner, config)
if __name__ == "__main__":
local_dir = os.path.dirname(__file__)
config_path = os.path.join(local_dir, "config.txt")
config = neat.Config(neat.DefaultGenome, neat.DefaultReproduction,
neat.DefaultSpeciesSet, neat.DefaultStagnation,
config_path)
# run_neat(config)
test_ai(config)