A reinforcement learning project where an agent learns to navigate a 2D maze to reach a goal using Q-learning.
- nn.py: Defines the neural network architecture. The
Agentclass is a feed-forward neural network that takes the current state (position, goal, and surrounding view) as input and predicts the Q-values for four possible actions (Up, Down, Left, Right). - main.py: A FastAPI server that acts as the environment and training coordinator. It generates the maze, manages the agent's current position, implements the Q-learning update logic, and provides endpoints for the frontend to request actions and save the model.
- main.html: The basic HTML structure that provides a container for the 2D maze visualization.
- main.js: Handles the frontend logic. It fetches the maze layout and start/goal positions from the server, renders the grid on the canvas, and continuously calls the server to move the agent and update the visualization until the goal is reached.
- Python 3.x
- PyTorch
- FastAPI
- Uvicorn (for running the FastAPI server)
- NumPy
-
Install the required Python packages:
pip install torch fastapi uvicorn numpy
-
Note on Hardware Acceleration: The current code uses
device = 'mps'for Apple Silicon (Metal Performance Shaders). If you are using a CUDA-enabled GPU or a CPU, change thedevicevariable innn.pyandmain.pyto'cuda'or'cpu'.
- Start the backend server:
uvicorn main:app --reload
- Open
main.htmlin any modern web browser.
- The project generates a 10x10 grid with randomly placed obstacles.
- A start position is chosen from the bottom row, and a goal position is determined using Breadth-First Search (BFS) to find the farthest reachable cell from the start.
- State: The input to the network consists of the agent's coordinates, the goal's coordinates, and a local 4-direction view (indicating if there is a wall in each direction).
- Actions: The agent can move in four directions: Up, Down, Left, or Right.
- Training: The agent uses an epsilon-greedy strategy to explore the maze. It learns by updating its Q-values using the Mean Squared Error (MSE) between the current Q-prediction and the target (reward + discounted max Q-value of the next state).
- Rewards:
- Reaching the goal: +100
- Hitting a wall/obstacle: -1
- Moving to an empty cell: +1
- The frontend requests the maze layout from
/data. - The frontend enters a loop, calling
/agentto get the next action and new position. - The server performs a training step, updates the agent's weights, and returns the new position.
- Once the agent reaches the goal, the frontend calls
/saveto persist the model weights toagent.pt.