Skip to main content

Gymnasium-compatible trolley-problem environments for studying reinforcement-learning agents under moral constraints.

Project description

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/ and omnisafe/: 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:

  1. create an environment and morality chain,
  2. wrap the environment so info["cost"] is emitted at each step,
  3. 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.

Project details


Download files

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

Source Distribution

morality_gym-0.1.0.tar.gz (67.0 kB view details)

Uploaded Source

Built Distribution

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

morality_gym-0.1.0-py3-none-any.whl (86.6 kB view details)

Uploaded Python 3

File details

Details for the file morality_gym-0.1.0.tar.gz.

File metadata

  • Download URL: morality_gym-0.1.0.tar.gz
  • Upload date:
  • Size: 67.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.2

File hashes

Hashes for morality_gym-0.1.0.tar.gz
Algorithm Hash digest
SHA256 7fa2f4659dd5c82ba77028976b6f086c9093953976d9df8ff2571421e0fde650
MD5 00c364e09d16f949962720c57da46f86
BLAKE2b-256 3a23af08941f64f93ae5c967414cf92b1f1c232769a683638612428726515496

See more details on using hashes here.

File details

Details for the file morality_gym-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: morality_gym-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 86.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.2

File hashes

Hashes for morality_gym-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a87db4a54418ab33251c9c86a94f8bb83bba773a9a0716cef70ca37f122ecde6
MD5 e0c7532ad767299c8ceed8a2a475a26c
BLAKE2b-256 2b3f5c02f5bdff1fb9f481de7a6bb05805ca85a31d3e4099aaf14faa391b2f0b

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page