Machine Learning With NEAT: February 23, 2024

Today is the day I finally finished the Pong AI project! I used the video above and have now watched it in its entirety.

Firstly, we added a few lines of code to the test_ai method in the PongGame class to allow the player to play pong against the AI. That code should look like this:

        net = neat.nn.FeedForwardNetwork.create(genome, config)
                  ...
            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)

Next, we use the pickle module to store our best AI to play against later. This is added to the run_neat function.

    winner = p.run(eval_genomes, 10)
    with open("best.pickle", "wb") as f:
        pickle.dump(winner, f)

Finally, we create the test_ai function which will use the test_ai method to allow the player to play against the AI with the AI we have saved using pickle.

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)

After realizing the AI would get stuck at the top of the screen during training, I am currently in the process of testing new ways the fitness of the AI is increased, such as adding more fitness for scoring and hitting the ball to go at steep angles.

From this project, I learned the basics of neural networks and how they train. I learned not only how to write a game that a neural network can play, but also how to tweak the settings of said network to train to become better at the game and to use cheating tactics.

One comment

Leave a Reply

Your email address will not be published. Required fields are marked *