Brown CPFU Vers. 8.28.25
A Fork of https://github.com/BrownParticleAstro/Grav-Nav-RL.git but includes multiship orbital environment & multiplayer game using authoritative server-client setup.
This project simulates 2D orbital maneuvers via RL and provides a real-time multiplayer experience. Players can control their ships manually or deploy a trained AI model to compete for the longest survival time.
For deploying this application to Google Cloud Run with automated CI/CD, see the complete deployment guide:
- Complete one-time setup (GCP project, service account, GitHub secrets)
- Trigger deployment via GitHub Actions
- Access your game at the provided Cloud Run URL
There are two ways to run the game: with Docker (recommended for a clean, isolated setup) or locally on your machine.
This is the simplest way to get the game running.
- Docker and Docker Compose must be installed on your system.
-
Open a terminal and navigate to the root directory of the project.
-
Run the following command. It will build the Docker image and start the server.
docker-compose up --build
-
The server is now running. You will see log output in your terminal, ending with a line like:
INFO: Uvicorn running on http://0.0.0.0:5501.
- Open your web browser.
- Navigate to the following address:
http://localhost:5501 - The game client will load and connect to the server running inside the Docker container.
- Return to the terminal where the server is running.
- Press
Ctrl + Cto stop the container. - To remove the container and its network, run:
docker-compose down
Follow these steps to run the server and client directly on your machine.
- Python 3.8 or higher
- A modern web browser
- VS Code with the Live Server extension (recommended for hosting
index.html)
- Create a virtual environment:
python3 -m venv grav-nav-env
- Activate the virtual environment:
- On macOS/Linux:
source grav-nav-env/bin/activate - On Windows:
grav-nav-env\Scripts\activate
- On macOS/Linux:
- Install required Python packages:
pip install -r requirements.txt
- Verify installation:
python -c "import fastapi, uvicorn, numpy, torch, stable_baselines3; print('All dependencies installed successfully!')"
-
Start the server:
python server_multiship.py
*Note: To run multidevice version, please run this isntead:
uvicorn server_multiship:app --host 0.0.0.0 --port 5500
-
Verify server is running:
- You should see output like:
INFO: Uvicorn running on http://0.0.0.0:5500 - The server will be accessible at
http://localhost:5500 - For multidevice version, the server will be accessible at
http://10.37.117.236:5500/. This is based off the IP address of the Brown WiFi network.
- You should see output like:
- Open VS Code in the project directory.
- Right-click on the
index.htmlfile in the explorer panel. - Select "Open with Live Server".
- Your browser will open automatically to the game interface (e.g.,
http://127.0.0.1:5501/index.html).
- Choose your control mode:
- Click "Manual Control" to fly your ship with the keyboard.
- Click "RL Model Control" to upload a
.zipmodel file and let an AI control your ship.
- Manual Controls:
- β (Up Arrow): Apply thrust.
- β (Left Arrow): Turn left.
- β (Right Arrow): Turn right.
- Multiple Players:
- Open additional browser tabs to the same address. Each tab will be a new player.
The simulation runs on an authoritative server using a tick-based system for synchronization.
clientsdict:{client_id: {"ws": websocket, "type": "manual" | "model", "ship_id": "uuid"}}env.shipsdict:{ship_id: {x, y, vx, vy, init_r, done, ...}}leaderboarddict:{ship_id: {name, steps, alive}}
All messages follow a standard header/payload format.
{
"header": {
"version": "1.0",
"type": "message_type_string",
"tick": 12345,
"timestamp": 167...
},
"payload": { ... }
}Server β Client:
mode_confirmed: Confirms the client's control mode (observe,manual, ormodel).action_request: Sent each tick to request an action from manual-control clients.model_upload_response: Informs the client if their AI model was successfully loaded.state_update: Broadcasts the state of all ships, leaderboard, and trail history to all clients.
Client β Server:
join_mode: Client requests to join with a specific control mode (manualormodel).model_upload: Client sends a base64-encoded AI model file.manual_action: Client sends their turn and thrust inputs for the current tick.cancel_control: Client requests to leave the game and return to observer mode.
The original goal was to simulate spacecraft orbital control by training a PPO model to pilot a spacecraft, achieving stable orbits with minimal fuel cost.
Core features include:
- Training: Learn optimal control strategies with PPO.
- Testing: Evaluate model performance.
- Rendering: Visualize the simulation at various stages.
- Rendering Multiship: Run
python3 render_multiship_trained.pyto visualize multiple AI-controlled ships orbiting a central mass.
| File Name | Description |
|---|---|
environment.py |
Defines the OrbitalEnvironment (core physics) and MultiShipOrbitalEnvironment. Adjust physics and reward functions here. |
model.py |
Contains the neural network architecture for the PPO agent. |
render.py |
Handles matplotlib visualizations for single-ship test episodes. |
run_and_view_episode.py |
A primary script for training, testing, and visualizing a full single-ship episode from start to finish. |
train.py |
Trains the PPO model. Modify hyperparameters (learning rate, gamma, etc.) |
test.py |
Runs multiple test episodes to evaluate a trained model's performance. |
hohman_example.py |
Demonstrates a Hohmann transfer orbit, a classic two-burn maneuver to move between circular orbits efficiently. |
Run train.py to train a new AI model from scratch.
python train.pyThis script saves the trained model and episode data to a new timestamped folder in ./models/.
Run test.py to evaluate a pre-trained model.```bash
python test.py --model_path /path/to/your/model.zip
### π¬ Running and Visualizing Full Episodes
Use `run_and_view_episode.py` to train, test, and render in one command.
```bash
python run_and_view_episode.py
Run hohman_example.py to see a demonstration of a classical Hohmann transfer orbit.
python hohman_example.py