Morality Gym Tabular
Morality Gym Tabular provides Gymnasium-compatible trolley-problem environments for studying how reinforcement-learning agents behave under moral constraints.
The repository has two layers:
morality_gym/: the environment package, morality chains, cost function utilities, and lightweight wrappers.baselines/andomnisafe/: experiment code used for the paper benchmarks. These are intended for repository users, not as the minimal pip-facing API.
Installation
For development from this repository:
git clone https://github.com/SimonRosen173/morality-gym-tabular.git
cd morality-gym-tabular
pip install -e .
The lightweight environment path depends on numpy, matplotlib, and gymnasium. The experiment stack also uses additional packages such as torch, stable-baselines3, omnisafe, pandas, tqdm, and cluster tooling where relevant.
Basic Usage
from morality_gym import make
env, morality_chain = make(
env_id="SwitchStandard-HumanA-v1",
morality_chain_id="Utility",
)
obs, info = env.reset(seed=42)
done = False
while not done:
action = env.action_space.sample()
obs, reward, terminated, truncated, info = env.step(action)
done = terminated or truncated
env.close()
Environment IDs use:
{scenario_id}-{variant_id}-v1
Examples include SwitchStandard-HumanA-v1, PushStandard-HumanA-v1, Switch5-Human-v1, and PushOrSwitch-Human-v1.
Safe-RL Example
The standalone example in examples/train_safe_rl_agent.py shows how to:
- create an environment and morality chain,
- wrap the environment so
info["cost"]is emitted at each step, - train a small constrained Q-learning agent with a Lagrangian cost penalty.
Run a short smoke test:
python examples/train_safe_rl_agent.py \
--episodes 10 \
--eval-episodes 3 \
--max-episode-steps 25 \
--log-every 5
Run a longer example:
python examples/train_safe_rl_agent.py --episodes 500 --eval-episodes 50
This script is a readable demonstration of the cost-wrapper pattern. It is not the paper benchmark implementation.
Experiments
Paper-style experiments are configured and run through the benchmarker under baselines/. In short:
python baselines/cli.py --create exp_p9xe.json
python baselines/cli.py --exec-run p9xe/run_0.json
See baselines/README.md and baselines/benchmarker/README.md for the experiment configuration format, local execution, SLURM execution, logs, and result consolidation notes.
Documentation
| Resource | Description |
|---|---|
| morality_gym/README.md | Supported environment ID syntax and scenarios |
| examples/ | Small scripts for interacting with and evaluating environments |
| baselines/README.md | Baseline learner and benchmarker overview |
| baselines/benchmarker/README.md | Experiment configuration and execution details |
Citation
If you use this framework in your research, please cite:
@misc{rosen2024moralitygym,
author = {Rosen, Simon and Singh, Siddarth and Robertson, Helen Sarah and Gelo, Ebenezer and
Suder, Ibrahim and Nangue Tasse, Geraud and Williams, Victoria and
James, Steven and Rosman, Benjamin},
title = {Morality Gym Tabular: A Framework for Moral Dilemmas in Reinforcement Learning},
year = {2024},
publisher = {GitHub},
journal = {GitHub Repository},
howpublished = {\url{https://github.com/SimonRosen173/morality-gym-tabular}}
}
Contributing
Contributions are welcome. See CONTRIBUTING.md.
License
This project is licensed under the terms of LICENSE.
Metadata
Release files for morality-gym 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| morality_gym-0.1.0.tar.gz | 67.0 kB | Without provenance | Details for morality_gym-0.1.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| morality_gym-0.1.0-py3-none-any.whl | Python 3 | none | any | Details for morality_gym-0.1.0-py3-none-any.whl |
Total release size: 153.6 kB
Release files / morality_gym-0.1.0.tar.gz
| Download URL | morality_gym-0.1.0.tar.gz |
|---|---|
| Size | 67.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
7fa2f4659dd5c82ba77028976b6f086c9093953976d9df8ff2571421e0fde650
|
|
BLAKE2b-256 checksum How to use checksums |
3a23af08941f64f93ae5c967414cf92b1f1c232769a683638612428726515496
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.12.2
|
Release files / morality_gym-0.1.0-py3-none-any.whl
| Download URL | morality_gym-0.1.0-py3-none-any.whl |
|---|---|
| Size | 86.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
a87db4a54418ab33251c9c86a94f8bb83bba773a9a0716cef70ca37f122ecde6
|
|
BLAKE2b-256 checksum How to use checksums |
2b3f5c02f5bdff1fb9f481de7a6bb05805ca85a31d3e4099aaf14faa391b2f0b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.12.2
|
Security for morality-gym 0.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.