A lightweight, explainable, and constrained reinforcement learning toolkit.
Project description
🔐 SafeRL-Lite
A lightweight, explainable, and modular Python library for Constrained Reinforcement Learning (Safe RL) with real-time SHAP & saliency-based explainability, custom metrics, and Gym-compatible wrappers.
**New update with ease to setup, install and import coming soon.
By:
- Satyam Mishra, Vision Mentors Ltd., Hanoi, Vietnam
- Shivam Mishra, Phung Thao Vi, Vietnam National University, Hanoi, Vietnam
- Dr. Vishwanath Bijalwan, SR University, Warangal, India
- Dr. Vijay Bhaskar Semwal, MANIT, Bhopal, India
- Dr. Abdul Manan Khan, University of West London, London, UK
🌟 Overview
SafeRL-Lite empowers reinforcement learning agents to act under safety constraints, while remaining interpretable and modular for fast experimentation. It wraps standard Gym environments and DQN-based agents with:
- ✅ Safety constraint logic
- 🔍 Visual explainability (SHAP, saliency maps)
- 📊 Violation and reward tracking
- 🧪 Built-in testing and evaluations
✅ Problem We Solved
Modern Reinforcement Learning (RL) agents are powerful but unsafe and opaque:
-
🚫 They frequently violate safety constraints during learning or deployment (e.g., fall off a cliff in navigation tasks).
-
😕 Their decision-making is a black box: humans can’t understand why a certain action was chosen.
-
🔍 Standard RL libraries lack native support for:
-
Enforcing hard constraints during training.
-
Explaining decisions using methods like SHAP or saliency maps.
-
✅ Our Solution
SafeRL-Lite is a lightweight Python library that:
-
📏 Adds a SafetyWrapper around any Gym environment to enforce safety constraints (e.g., bounding actions, limiting states).
-
🧠 Integrates explainability methods:
-
SHAPExplainer (model-agnostic local explanations).
-
SaliencyExplainer (gradient-based sensitivity maps).
-
🔧 Wraps Constrained DQNs with ease, enabling safety-compliant Q-learning.
-
📊 Offers built-in metrics like violation count and safe episode tracking.
✅ Novelty
While Safe RL and Explainable RL are separately studied, no prior lightweight library:
-
Combines hard safety constraints with post-hoc interpretability.
-
Is designed to be minimal, pluggable, and easily installable (pip install saferl-lite) for education, experimentation, or safe deployment.
-
Enables real-time SHAP or saliency visualization for Gym-based agents out-of-the-box.
SafeRL-Lite is the first minimal library to unify constraint satisfaction and explainability in reinforcement learning — without heavy dependencies or overhead.
✅ Our Contribution
-
🔐 Constraint Wrapper API: Drop-in Gym wrapper for defining and enforcing logical constraints on observations, actions, and reward signals.
-
🧠 Explainability Modules: Plug-and-play SHAP and saliency explainer classes for deep Q-networks.
-
📦 PyPI-Ready Toolkit: Easily installed, documented, and CI/CD tested; built for research and reproducibility.
-
📈 Metrics for Constraint Violation: Tracks unsafe episodes, per-step violations, and integrates cleanly with WandB or TensorBoard.
✅ Technical Explanation
-
We define a custom SafeEnvWrapper(gym.Env) that:
-
Intercepts actions.
-
Applies logical rules or thresholding.
-
Optionally overrides rewards or terminations if constraints are violated.
-
-
A ConstrainedDQNAgent uses:
-
Safety-wrapped Gym envs.
-
Standard Q-learning with optional penalty_on_violation flag.
-
-
Post-training, the SHAPExplainer and SaliencyExplainer:
-
Generate local attributions using input perturbations or gradient norms.
-
Can visualize per-state or per-action explanations.
-
✅ Satyam's Explanation
Imagine you're teaching a robot to walk — but there’s lava on the floor!
You don’t just want it to learn fast, you want it to stay safe and explain why it stepped left, not right.
SafeRL-Lite is like a safety helmet and voicebox for robots:
-
The helmet makes sure they don’t do dangerous stuff.
-
The voicebox lets them say why they made that move.
🔧 Installation
📦 PyPI
pip install saferl-lite
🛠️ From source:
git clone https://github.com/satyamcser/saferl-lite.git
cd saferl-lite
pip install -e .
🚀 Quickstart
Train a constrained DQN agent with saliency-based explainability:
python train.py --env CartPole-v1 --constraint pole_angle --explain shap
🔹 This:
-
Adds a pole-angle constraint wrapper to the Gym env
-
Logs violations
-
Displays SHAP or saliency explanations for agent decisions
🧠 Features
✅ Constrained RL
-
Add custom constraints via wrapper or logic class
-
Violation logging and reward shaping
-
Safe vs unsafe episode tracking
🔍 Explainability
-
SaliencyExplainer — gradient-based visual heatmaps
-
SHAPExplainer — feature contribution values per decision
-
Compatible with any PyTorch-based agent
📊 Metrics
-
Constraint violation rate
-
Episode reward
-
Cumulative safe reward
-
Action entropy & temporal behavior stats
📚 Modularity
-
Swap out agents, constraints, evaluators, or explainers
-
Supports Gym environments
-
Configurable training pipeline
📜 Citation
Coming soon after arXiv/preprint release.
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 saferl_lite-0.1.3.tar.gz.
File metadata
- Download URL: saferl_lite-0.1.3.tar.gz
- Upload date:
- Size: 18.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.10.18
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
86ae5a932dc7d3d23e518dc4f796461b58d145bcd9d71b50a1574c83eb423ef3
|
|
| MD5 |
32eadd0c577cec33c8314cd19a630c15
|
|
| BLAKE2b-256 |
cc1182c654e3d3bea17324ee5d9d22c78631c9d9da29004d9d88c95af046ade7
|
File details
Details for the file saferl_lite-0.1.3-py3-none-any.whl.
File metadata
- Download URL: saferl_lite-0.1.3-py3-none-any.whl
- Upload date:
- Size: 9.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.10.18
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
bd7deb80b7571e8055773688b46249643c908f275bae71b7fafb593cbbe76782
|
|
| MD5 |
6f6167eeb9589546a0081e71047d6d58
|
|
| BLAKE2b-256 |
8e1ca0f21c585875db4ab68d09c1b2f1f21c8da90f222dbda811dfc906ea1d66
|