I continued working on my AI pong project with the help of the video above.
I first made some minor changes to the run_neat function, then added a function that creates the game to evaluate the AI’s fitness level. The function should look like this:
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-12')
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, 50)
Next, I created the train_ai function, which creates the two neural networks and begins to train them against each other. I also created the calculate_fitness function, but I did not have enough time to write the rest of it. The train_ai function look like this:
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)))
output2 = net2.activate((self.right_paddle.y, self.ball.y, abs(self.right_paddle.x - self.ball.x)))
game_info = self.game.loop()
self.game.draw()
pygame.display.update()
if game_info.left_score >= 1 or game_info.right_score >= 1:
self.calculate_fitness(genome1, genome2, game_info)
break
def calculate_fitness(self, genome1, genome2, game_info):
pass
This is great. Do you understand how it is working?