A sailing environment for OpenAI Gym / Gymnasium
Project description
gym-sailing: A sailing environment for OpenAI Gym / Gymnasium
This is a Gymnasium (OpenAI Gym) environment designed to train reinforcement learning (RL) agents to control a sailboat. The environment simulates the dynamics of a sailboat and allows the agent to learn tacking behavior to reach a target point.
Environments
| Environment | Description |
|---|---|
| Sailboat-v0 | The main environment with a continuous action space. |
| SailboatDiscrete-v0 | A variation of the environment with a discrete action space. |
| Motorboat-v0 | An easy test environment with a motorboat instead of a sailboat. |
Installation
You can install the latest release using pip:
pip install gym-sailing
Alternatively, if you prefer, you can clone the repository and install it locally.
Usage
Basic Usage
Bare minimum code to run the environment:
import gymnasium as gym
import gym_sailing
env = gym.make("Sailboat-v0", render_mode="human")
observation, info = env.reset(seed=42)
for _ in range(1000):
action = env.action_space.sample() # this is where you would insert your policy
observation, reward, terminated, truncated, info = env.step(action)
if terminated or truncated:
observation, info = env.reset()
env.close()
Training an RL Agent
To train an RL agent using stable-baselines3:
from stable_baselines3 import PPO
import gymnasium as gym
import gym_sailing
env = gym.make("Sailboat-v0")
model = PPO('MlpPolicy', env, verbose=1)
# Train the agent
model.learn(total_timesteps=1_000_000)
# Test the trained model
observation, info = env.reset()
for _ in range(1000):
action, _ = model.predict(observation)
observation, reward, terminated, truncated, info = env.step(action)
env.close()
Environment Details
Observation Space
The observation space includes:
- Boat Speed: The current speed of the boat.
- Boat Heading: The angle of the boat relative to the wind, ranging from -$\pi$ to $\pi$.
- Heading Rate: The rate of change of the boat's heading.
- Course to Target: The angle between the boat's heading and the target, ranging from -$\pi$ to $\pi$.
- Distance to Target: The normalized distance between the boat and the target.
Action Space
The action space consists of:
- Rudder Angle: The angle of the rudder, ranging from -1 to 1 for Sailboat-v0 and Motorboat-v0, and {-1, 0, 1} for SailboatDiscrete-v0.
Reward
The default reward function includes:
- Alive Penalty: A penalty for each time step to encourage the agent to reach the target quickly.
- Target Reward: A reward for reaching the target.
- Course Penalty: A penalty for leaving the course area.
- Progress Reward: A reward for making progress towards the target, using the L8 norm, to encourage the agent to move upwind.
Episode End
- The environment is terminated if the boat reaches the target or leaves the course area.
- The environment is truncated after 3000 steps.
Benchmarks
Benchmarks using stable-baselines3 with default hyperparameters. Good policies that tack only once tend to achieve ~390 total reward for the sailboat environment. PPO seems to perform better, but SAC is also a good option, that even converging faster.
Contributing
Contributions are welcome. Please fork the repository and submit a pull request with your changes. For any questions or suggestions, feel free to open an issue.
Future Work
Here are some features I'd like to add in the future:
- Add currents of different intensities and directions.
- Add wind shifts.
- Add wind gusts and lulls.
- Make the polar diagram more accurate, using the data from this paper: R. Binns, F. W. Bethwaite, and N. R. Saunders, “Development of A More Realistic Sailing Simulator,” High Performance Yacht Design Conference. RINA, pp. 243–250, Dec. 04, 2002. doi: 10.3940/rina.ya.2002.29.
Inspiration
This project was inspired by this fork: https://github.com/openai/gym/compare/master...JonAsbury:gym:Sailing-Simulator-Env
License
This project is licensed under the MIT License - see the LICENSE file for details.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file gym_sailing-0.2.1.tar.gz.
File metadata
- Download URL: gym_sailing-0.2.1.tar.gz
- Upload date:
- Size: 923.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.11.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
67938c3e3df52580351b79fcf82c6131f2b078c6d14d50bfaa63e8ccd58fe568
|
|
| MD5 |
77cf66e4c4bab2e06c021fd13092a91b
|
|
| BLAKE2b-256 |
c7572527aac22527e0d0823ac9a5c4be22d9a52f5ded5671fb3ef50ba2565c79
|
File details
Details for the file gym_sailing-0.2.1-py3-none-any.whl.
File metadata
- Download URL: gym_sailing-0.2.1-py3-none-any.whl
- Upload date:
- Size: 23.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.11.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4f49dc4180c54c7fc92837ae6fd5e1e15b32867a0cee3f0a47054891dc5cb397
|
|
| MD5 |
603649faed28319a298484c67b932360
|
|
| BLAKE2b-256 |
deb6b50efc6670694a41580f1bf04416d22f86ae71b4c8e103492659c4e544e9
|