-
Notifications
You must be signed in to change notification settings - Fork 0
/
pong_ai.py
167 lines (132 loc) · 5.6 KB
/
pong_ai.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
"""
Dilyana Koleva, August 2022
Pong AI with NEAT
NEAT (NeuroEvolution of Augmenting Topologies) is an
evolutionary algorithm that creates artificial neural networks.
Link: https://neat-python.readthedocs.io/en/latest/neat_overview.html
"""
import pygame
import neat
import os
import pickle
from pong import Game
# width, height = 700, 500
# window = pygame.display.set_mode((width, height))
# game = Game(window, width, height)
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()
# Allows left paddle to move
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))
# Allows right paddle to move
if decision == 0:
pass
elif decision == 1:
self.game.move_paddle(left=False, up=True)
else:
self.game.move_paddle(left=False, up=False)
self.game.loop()
# Shows the combined number of hits of both paddles
self.game.draw(True, False)
pygame.display.update()
pygame.quit()
def train_ai(self, genome1, genome2, config):
# Set neural networks
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 = self.game.loop()
self.game.draw(draw_score=False, draw_hits=True)
pygame.display.update()
# If either paddle misses the ball, end game
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
# Calculates the fitness of the genome
def calculate_fitness(self, genome1, genome2, game_info):
genome1.fitness += game_info.left_hits
genome2.fitness += game_info.right_hits
def evaluate_genomes(genomes, config):
# Set up a pygame window
width, height = 700, 500
window = pygame.display.set_mode((width, height))
# Runs each genome against other genomes exactly once
for i, (genome_id1, genome1) in enumerate(genomes):
if i == len(genomes) - 1:
break
genome1.fitness = 0
# Ensures the same genomes don't play against each other multiple times
for genome_id2, genome2 in genomes[i + 1:]:
genome2.fitness = 0 if genome2.fitness is None else genome2.fitness
game = PongGame(window, width, height)
game.train_ai(genome1, genome2, config)
def run_NEAT(config):
# population = neat.Checkpointer.restore_checkpoint('neat-checkpoint-27')
# Create population
population = neat.Population(config)
# Report data to standard output
population.add_reporter(neat.StdOutReporter(True))
stats = neat.StatisticsReporter()
population.add_reporter(stats)
# Saves checkpoint after every generation (restarts algorithm)
population.add_reporter(neat.Checkpointer(1))
# Gives best
best = population.run(evaluate_genomes, 1)
# Allows us to save a whole python object
with open("best.pickle", "wb") as f:
pickle.dump(best, f)
def test_ai(config):
width, height = 700, 500
window = pygame.display.set_mode((width, height))
with open("best.pickle", "rb") as f:
best = pickle.load(f)
game = PongGame(window, width, height)
game.test_ai(best, 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)