10. Pacman with AI#
10.1. Practical Session 3: Pacman with AI#
10.1.1. Introduction#
In this practical session, we will implement a simple net that plays the game Pacman. The goal is to understand all the repository and how to use it for further development.
10.2. Repository Overview#
This repository contains an implementation of the classic Pacman game with an AI component that allows for different intelligent agents to play the game. The project is based on UC Berkeley’s Pacman AI projects, modified to incorporate neural networks for agent decision-making.
10.2.1. Key Components#
Game Engine (
game.py): Contains the core game mechanics, including:Agent classes
Directional movement logic
Game state management
Configuration handling for game entities
Pacman Engine (
pacman.py): The main game runner that:Implements Pacman-specific rules
Handles user input
Manages game flow
Provides game visualization
Includes command-line interfaces for running games
Neural Agent (
multiAgents.py): Contains various agent implementations including:Reflex agents
Minimax agents
Neural network-based agent (
NeuralAgent)Functions for evaluating game states
Neural Network (
net.py): Implements the neural network architecture:PacmanNetclass using PyTorchTraining functionality
Data loading and preprocessing
Model saving/loading capabilities
Ghost Agents (
ghostAgents.py): Different ghost behaviors:Random ghost movement
Directional ghosts that chase Pacman
Data Collection (
gamedata.py): Records game play for training:Captures state, actions, and results
Saves game data to CSV files
Formats game maps as numeric matrices
Replay Functionality (
playback.py): Allows replaying recorded games from CSV dataUtilities (
util.py): Helper functions and data structures:Queue, Stack, PriorityQueue implementations
Random number generation
Distance calculations
Various utility functions for game logic
10.3. How the Neural Agent Works#
The NeuralAgent class in multiAgents.py provides an AI-powered Pacman that:
Loads a pre-trained neural network model from a file
Converts game states into numeric matrices for neural network input
Makes decisions by combining neural network outputs with heuristic game knowledge
Balances exploration and exploitation with a decaying exploration rate
Evaluates potential moves by considering:
Network confidence scores
Proximity to food
Distance from ghosts (avoiding or chasing based on ghost state)
Game score
10.4. Data Collection and Training#
The system includes a complete pipeline for collecting game data and training models:
The
GameDataCollectoringamedata.pycaptures game states during playGame states are converted to numeric matrices (representing walls, food, capsules, ghosts, and Pacman)
The training pipeline in
net.pyloads recorded games, processes them, and trains a neural networkThe model is saved for later use by the
NeuralAgent
10.5. Getting Started#
To run the game with a keyboard-controlled Pacman:
python pacman.py
To run the game with the neural agent:
python pacman.py -p NeuralAgent
Other useful options:
-l [layout_name]: Choose a game layout-g [ghost_type]: Select ghost agent type-k [num_ghosts]: Set number of ghosts-n [num_games]: Run multiple games--csv [file_path]: Replay a recorded game
# Source Web and GitHub