How wathsara02/pusl3190 works
Purpose and Entry Points
This repository builds, trains, evaluates, and deploys a multi-agent reinforcement-learning player for the four-player Omi card game. It has two major subsystems: an offline MARL project in finalwm/ and a live React/FastAPI game application in last/.
Primary execution entry points:
• Training/evaluation: finalwm/scripts/train.py, finalwm/scripts/eval.py, finalwm/scripts/export.py
• Backend service: last/backend/app/main.py
• Frontend client: last/frontend/src/main.tsx, last/frontend/src/App.tsx
• Standalone demonstrations: demo/01_weights_check.py through demo/07_ai_card_decision.py
Omi Rules and Environment Boundary
The authoritative gameplay model is a Gymnasium/PettingZoo-compatible environment. It manages dealing, trump declaration, action legality, trick resolution, team scoring, termination at five tricks, and action masks for the 36-action space.
Core training-game paths:
• finalwm/omi_env/env.py
• finalwm/omi_env/rules.py
• finalwm/omi_env/encoding.py
encoding.py turns game state into the 195-dimensional policy observation. The web backend contains a separately packaged copy of this game implementation, allowing deployed play to run independently of the training project:
• last/backend/app/game/omi_env/env.py
• last/backend/app/game/omi_env/rules.py
• last/backend/app/game/omi_env/encoding.py
MARL Training Pipeline
Training uses MAPPO: multiple agents interact with vectorized Omi environments, collect rollout trajectories, and update shared policy/value networks with masked legal actions. Training configuration controls parallelism, device, schedules, and episode limits.
Architecture-defining stages:
1. Vector environment orchestration: finalwm/marl/vector_env.py
2. Rollout/buffer storage: finalwm/buffer.py
3. MAPPO/PPO optimization and checkpoint lifecycle: finalwm/marl/r_mappo.py
4. Actor policy and action distribution: finalwm/models/policy.py
5. Centralized value estimation: finalwm/models/critic.py
finalwm/scripts/train.py composes these layers from YAML configs such as finalwm/configs/default.yaml. Baseline opponents provide comparative evaluation rather than core serving behavior:
• finalwm/baselines/random_agent.py
• finalwm/baselines/rule_based_agent.py
Model Export and Runtime Inference
A trained policy checkpoint is transformed into a deployment artifact by finalwm/scripts/export.py. The backend ships its own model wrapper and serialized weights:
• last/backend/rl_model/omi_agent.py
• last/backend/rl_model/weights.pt
At runtime, bot management obtains a legal game-state representation, invokes the PyTorch policy wrapper, and applies the selected card or trump action through the room/game manager. This is the key boundary between ML inference and server-authoritative game state:
• last/backend/app/ai/bot_manager.py
• last/backend/app/game/room_manager.py
Backend Service, State, and Real-Time Flow
FastAPI exposes REST endpoints for authentication, rooms, and game state, while WebSockets push interactive updates. The room manager owns live game-session lifecycle; it should remain the coordination point for human moves, bot turns, state transitions, and broadcasts.
Key paths:
• HTTP app/bootstrap and CORS: last/backend/app/main.py
• REST and JWT auth: last/backend/app/api/routes.py, last/backend/app/api/auth.py
• WebSocket handlers and connection registry: last/backend/app/ws/sockets.py, last/backend/app/ws/connection.py
• SQLAlchemy persistence: last/backend/app/db/database.py, last/backend/app/db/models.py
Infrastructure includes Uvicorn, SQLAlchemy, and SQLite or PostgreSQL selected through DB_URL. JWT secrets and allowed browser origins are environment-driven.
Frontend Client and External Boundaries
The frontend is a Vite React/TypeScript application. Pages handle auth, lobby/room navigation, gameplay, profiles, and history; a shared game-state hook/store bridges REST initialization and WebSocket-driven updates.
Core client paths:
• last/frontend/src/pages/Game.tsx
• last/frontend/src/hooks/useGameState.ts
• last/frontend/src/store/gameStore.ts
• last/frontend/src/lib/api.ts
The client connects to backend HTTP and WebSocket URLs via environment variables. It also includes a Supabase integration boundary at last/frontend/src/lib/supabase.ts; authentication behavior should be traced alongside the backend JWT endpoints before modifying identity flows.
Main entry points: room_manager.py Room manager · routes.py REST and JWT API · sockets.py WebSocket gateway · r_mappo.py MAPPO optimizer
Components 20
Offline MARL
Training, evaluation, and policy export.
- Train / eval entrypoints
finalwm/scripts/train.py - Omi training environment · Authoritative rules, state encoding, and legal-action masks.
finalwm/omi_env/env.py - Vector environments
finalwm/marl/vector_env.py - Rollout buffer
finalwm/buffer.py - MAPPO optimizer · Updates shared policy/value networks and manages checkpoints.
finalwm/marl/r_mappo.py - Actor policy · Produces masked actions over the 36-action space.
finalwm/models/policy.py - Centralized critic
finalwm/models/critic.py - Model export
finalwm/scripts/export.py
Live Backend
Authoritative game sessions and bot inference.
- FastAPI application · Configures REST, WebSocket, CORS, and runtime service.
last/backend/app/main.py - REST and JWT API
last/backend/app/api/routes.py - WebSocket gateway
last/backend/app/ws/sockets.py - Room manager · Owns turns, state transitions, bot turns, and broadcasts.
last/backend/app/game/room_manager.py - Deployed Omi environment · Separate packaged game implementation for live play.
last/backend/app/game/omi_env/env.py - Bot manager
last/backend/app/ai/bot_manager.py - Runtime policy and weights
last/backend/rl_model/omi_agent.py - Game persistence · SQLite or PostgreSQL via DB_URL.
last/backend/app/db/database.py
React Client
Browser gameplay and identity integration.
- React application
last/frontend/src/App.tsx - Game page and state · Bridges REST initialization and WebSocket updates.
last/frontend/src/hooks/useGameState.ts - Backend API client
last/frontend/src/lib/api.ts - Supabase integration · Trace alongside backend JWT flows before changing authentication.
last/frontend/src/lib/supabase.ts
Connections 23
- Train / eval entrypoints → Vector environments: creates
- Vector environments → Omi training environment: runs parallel games
- Omi training environment → Rollout buffer: observations and rollouts
- Rollout buffer → MAPPO optimizer: training batches
- MAPPO optimizer → Actor policy: optimizes
- MAPPO optimizer → Centralized critic: optimizes
- Actor policy → Model export: checkpoint
- Model export → Runtime policy and weights: deploys weights
- FastAPI application → REST and JWT API: serves
- FastAPI application → WebSocket gateway: serves
- REST and JWT API → Room manager: room and game commands
- REST and JWT API → Game persistence: persists users and games
- WebSocket gateway → Room manager: live move events
- Room manager → WebSocket gateway: broadcasts state
- Room manager → Deployed Omi environment: applies game actions
- Room manager → Bot manager: requests bot turn
- Bot manager → Runtime policy and weights: legal-state inference
- Bot manager → Room manager: selected action
- React application → Game page and state: renders
- Game page and state → Backend API client: fetches and subscribes
- Backend API client → REST and JWT API: HTTP
- Backend API client → WebSocket gateway: WebSocket
- React application → Supabase integration: identity integration