Skip to main content

Gymnasium Mars Lander

Build Python Package Python PyPI PyPI Downloads pre-commit Ruff ty

Gymnasium environment for the Mars Lander CodinGame puzzles:

Episode 2 Episode 3
Demo episode 2 Demo episode 3
Action Space Box(-1, 1, (2,), float32)
Observation Space Box(-1, 1, (13,), float32)
Import gymnasium.make("gymnasium_mars_lander:gymnasium_mars_lander/MarsLander-v1")

This package is inspired by the article of Antoine Broyelle: Learning to Land on Mars with Reinforcement Learning.

Installation

To install gymnasium-mars-lander with pip, execute:

pip install gymnasium_mars_lander

From source:

git clone https://github.com/Quentin18/gymnasium-mars-lander
cd gymnasium-mars-lander/
pip install -e .

For running on CPU with extras:

pip install -e .[training,testing,quality] --extra-index-url https://download.pytorch.org/whl/cpu

Environment

Action Space

The action is a ndarray with two continuous variables:

  • The rotation change between -15 and 15 degrees.
  • The thrust change between -1 and 1.

The values are normalized between -1 and 1.

Observation Space

The observation is a ndarray of 13 continuous variables:

  • The distances in six directions from the current position.
  • The rower horizontal and vertical speed, angle and thrust.
  • The horizontal and vertical distances to the middle of the landing area.
  • A boolean indicating whether the rover can see the landing area.

The values are normalized between -1 and 1.

The following figure shows the sensors used:

Sensors

Rewards

The rewards are described by the following table:

Condition Reward
The rover leaves the frame -150
The rover runs out of fuel -150
The rover crashes outside flat ground with incorrect angle and speed -100
The rover crashes with correct angle and speed -75
The rover crashes on flat ground -50
The rover approaches the landing area 0.01
The rover lands successfully 200 + Amount of remaining propellant

Starting State

The starting state is generated by choosing a random CodinGame test case. When the eval_env argument is False, some random augmentations are applied to the test case. For each test case, there are five starting positions in increasing order of difficulty. The starting position can be set with the start argument.

The following figure shows the starting positions:

Starts

Training a model on examples of increasing difficulty is called curriculum learning.

Episode End

The episode ends if either of the following happens:

  1. Termination: The rower lands on the landing area or runs out of fuel or crashes.
  2. Truncation: Episode length is greater than 2000.

Arguments

  • episode: episode number between 1 and 3. The default value is 2.
  • start: starting position between -1 and 4. The default value is -1.
  • eval_env: if True, the random augmentations are disabled. The default value is False.
  • sequential_maps: if True, the maps are generated sequentially. The default value is False.
import gymnasium as gym

gym.make(
    "gymnasium_mars_lander:gymnasium_mars_lander/MarsLander-v1",
    episode=2,
    start=-1,
    eval_env=False,
    sequential_maps=False,
)

Trained agents

There is one trained agent for each episode:

Path Episode
rl-trained-agents/ppo/gymnasium_mars_lander-MarsLander-v1_1/best_model.zip 1
rl-trained-agents/ppo/gymnasium_mars_lander-MarsLander-v1_2/best_model.zip 2
rl-trained-agents/ppo/gymnasium_mars_lander-MarsLander-v1_3/best_model.zip 3

Note: the agents can only solve the episode for which they were trained.

Usage

You can use RL Baselines3 Zoo to train and evaluate agents:

pip install rl_zoo3

Train an Agent

The hyperparameters are defined in hyperparams/ppo.yml.

To train a PPO agent for the Mars Lander game, execute:

python -m rl_zoo3.train \
  --algo ppo \
  --env gymnasium_mars_lander/MarsLander-v1 \
  --tensorboard-log logs \
  --trained-agent rl-trained-agents/ppo/gymnasium_mars_lander-MarsLander-v1_2/best_model.zip \
  --n-timesteps 10000000 \
  --log-interval 100 \
  --eval-freq 100000 \
  --eval-episodes 20 \
  --seed 42 \
  --gym-packages gymnasium_mars_lander \
  --conf-file hyperparams/ppo.yml \
  --progress \
  --env-kwargs "episode:int(2)" "start:int(-1)" "sequential_maps:True"

To train an agent for an episode (exemple: 2) with curriculum learning, execute:

./scripts/train.sh 2

Enjoy a Trained Agent

To see a trained agent in action on random test cases, execute:

python -m rl_zoo3.enjoy \
  --algo ppo \
  --env gymnasium_mars_lander/MarsLander-v1 \
  --n-timesteps 1000 \
  --exp-id 2 \
  --deterministic \
  --seed 42 \
  --gym-packages gymnasium_mars_lander \
  --load-best \
  --progress \
  --env-kwargs "episode:int(2)" "start:int(-1)" "sequential_maps:True"

Note: add --exp-id argument to choose the model corresponding to the episode.

To see a trained agent in action on CodinGame test cases, execute:

python -m scripts.enjoy \
  --path rl-trained-agents/ppo/gymnasium_mars_lander-MarsLander-v1_2/best_model.zip \
  --episode 2

To record videos of a trained agent in action on CodinGame test cases, execute:

python -m scripts.enjoy \
  --path rl-trained-agents/ppo/gymnasium_mars_lander-MarsLander-v1_2/best_model.zip \
  --episode 2 \
  --record-video

Tests

To run tests, execute:

pytest

Citing

To cite the repository in publications:

@misc{gymnasium-mars-lander,
  author = {Quentin Deschamps},
  title = {Gymnasium Mars Lander},
  year = {2026},
  publisher = {GitHub},
  journal = {GitHub repository},
  howpublished = {\url{https://github.com/Quentin18/gymnasium-mars-lander}},
}

References

Author

Quentin Deschamps

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

gymnasium_mars_lander-2.0.0.tar.gz (6.7 MB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

gymnasium_mars_lander-2.0.0-py3-none-any.whl (57.6 kB view details)

Uploaded Python 3

File details

Details for the file gymnasium_mars_lander-2.0.0.tar.gz.

File metadata

  • Download URL: gymnasium_mars_lander-2.0.0.tar.gz
  • Upload date:
  • Size: 6.7 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for gymnasium_mars_lander-2.0.0.tar.gz
Algorithm Hash digest
SHA256 16500ed110898e8bbf0b48a1560598006afaaf2723f38b0d684f4da43c9eb4cc
MD5 4ffc731a37db79d9bf22382d43beaeb8
BLAKE2b-256 b6dad60ae1390c7c809aaa7c429b0cd660264e2e39e6cdec0911cb640125c3ae

See more details on using hashes here.

File details

Details for the file gymnasium_mars_lander-2.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for gymnasium_mars_lander-2.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 0df1dd7b04d810cc7dea4fd4d8e927a5f7f96f6da8c8e9d82760cbd5b5757bed
MD5 a7f519e8a5357ac8c392cce3141d9981
BLAKE2b-256 d9eec361ecfe0ee19272f84c5a38a21bb88d4c77354fd195de5f1580c489b638

See more details on using hashes here.

Release history Release notifications | RSS feed

2.1.0

2 files

This release

2.0.0 This release

2 files

1.0.0

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page