-
Notifications
You must be signed in to change notification settings - Fork 4
AI Service
The AI Service is a standalone microservice responsible for providing an intelligent opponent in the "Player vs AI" game mode. It uses Deep Reinforcement Learning to predict the optimal paddle movement based on the current state of the game.
| Property | Value |
|---|---|
| Port | 3005 |
| Language | Python 3.10+ |
| Framework | FastAPI |
| Model | PPO (Proximal Policy Optimization) |
| Library | Stable Baselines3 |
The service operates as a stateless API. The Game Service (Node.js) acts as the client, sending the game state to the AI Service every frame (or tick) and receiving a move in response.
sequenceDiagram
participant Game as ๐ Game Service
participant AI as ๐ค AI Service
loop Every Game Tick
Game->>AI: POST /predict { ball_x, ball_y, paddle_y, ... }
AI->>AI: Normalize State & Run Model
AI-->>Game: JSON { move: "UP" | "DOWN" | "STOP" }
end
- FastAPI: High-performance web server for serving the model.
- Stable Baselines3: Reliable implementation of the PPO algorithm.
-
Gymnasium: Used to create the
Pongenvironment for training. - NumPy: Efficient array manipulation for state normalization.
The agent uses PPO (Proximal Policy Optimization) with a Multi-Layer Perceptron (MLP) policy. This was chosen over CNN (Convolutional Neural Networks) for performance reasons, as we pass coordinate data rather than raw pixels.
Training Hyperparameters:
model = PPO(
'MlpPolicy', # Neural Network type (Dense layers)
env, # Pong Environment
learning_rate=0.001, # Learning speed
n_steps=2048, # Steps per update
batch_size=64, # Minibatch size
n_epochs=10, # Epochs per update
verbose=1
)The model is pre-trained and saved as a .zip file (e.g., ppo_pong_final.zip). To retrain the model, you can run the training script inside the container.
- Enter the container:
docker exec -it pong-ai
- Run training:
python3 train.py
This will launch the Gymnasium environment and train for ~2 million timesteps.
The service is part of the main docker-compose.yml stack.
pong-ai:
build: ./srcs/pong-ai
ports:
- "3005:3005"
volumes:
- ./srcs/ai:/app
networks:
- transcendence-network
To start it individually:
docker compose up -d pong-ai --build
See also:
- Game Service - The consumer of this API.
- Reinforcement Learning - Theory behind PPO.
Introduction to Proximal Policy Optimization algorithm (PPO)
- Gateway Service - API Gateway & JWT validation
- Auth Service - Authentication & 2FA/TOTP
- AI Service - AI opponent
- API Documentation - OpenAPI/Swagger
- DB Schema - Databases
- Fastify - Web framework
- Prisma - ORM
- WebSockets - Real-time communication
- Restful API - API standards
- React - UI library
- CSS - Styling
- Tailwind - CSS framework
- Accessibility - WCAG compliance
- TypeScript - Language
- Zod - Schema validation
- Nginx - Reverse proxy
- Logging and Error management - Observability
- OAuth 2.0 - Authentication flows
- Two-factor authentication - 2FA/TOTP
- Avalanche - Blockchain network
- Hardhat - Development framework
- Solidity - Smart contracts language
- Open Zeppelin - Security standards
- ESLint - Linting
- Vitest - Testing
- GitHub Actions - CI/CD
- Husky, Commit lints and git hooks - Git hooks
- ELK - Logging stack
๐ Page model