# AnestheSim

**Gymnasium environments for reinforcement learning in automated anesthesia drug dosing**

![Python 3.10+](https://img.shields.io/badge/python-3.10%2B-blue.svg)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
![Tests](https://img.shields.io/badge/tests-109%20passing-brightgreen.svg)
[![PyPI version](https://img.shields.io/pypi/v/anesthesim.svg)](https://pypi.org/project/anesthesim/)

---

Three Gymnasium-compatible RL environments for automated anesthesia drug dosing: propofol infusion control via the Marsh three-compartment pharmacokinetic model with Hill pharmacodynamic BIS prediction, remifentanil effect-site concentration targeting via the Minto model, and combined propofol-remifentanil anesthesia management using the Greco synergistic interaction surface. Includes configurable difficulty tiers with patient variability and surgical stimulation events, heuristic TCI (Target-Controlled Infusion) clinical baselines, PPO RL agents, and a benchmark suite across three difficulty levels. 109 tests, MIT licensed.

## Installation

```bash
pip install anesthesim              # Core (numpy, scipy, gymnasium)
pip install anesthesim[train]       # + SB3, PyTorch for RL training
pip install anesthesim[all]         # Everything
```

Development install:

```bash
git clone https://github.com/HassDhia/anesthesim.git
cd anesthesim
pip install -e ".[all]"
```

## Quick Start

```python
import gymnasium as gym
import anesthesim

env = gym.make("anesthesim/PropofolControl-v0")
obs, info = env.reset(seed=42)
for _ in range(100):
    action = env.action_space.sample()
    obs, reward, terminated, truncated, info = env.step(action)
    if terminated or truncated:
        obs, info = env.reset()
env.close()
```

## Environments

| Environment | Task | Observation | Action | Key Challenge |
|---|---|---|---|---|
| `PropofolControl-v0` | BIS-guided propofol infusion | Box(6): Cp, Ce, BIS, error, rate, time | Box(1): rate [0,200] mcg/kg/min | PK/PD delay, surgical stimulation |
| `RemifentanilDosing-v0` | Effect-site concentration targeting | Box(6): Cp, Ce, target, error, rate, time | Box(1): rate [0,0.5] mcg/kg/min | Rapid onset/offset, changing targets |
| `CombinedAnesthesia-v0` | Multi-drug BIS control | Box(8): 2x Cp, 2x Ce, BIS, error, 2x rate | Box(2): propofol + remifentanil rates | Drug synergy exploitation |

## Architecture

AnestheSim implements established pharmacokinetic/pharmacodynamic models:

- **Marsh PK Model** (Marsh et al., 1991): Three-compartment model for propofol distribution
- **Minto PK Model** (Minto et al., 1997): Three-compartment model for remifentanil with effect-site
- **Hill PD Model** (Schnider et al., 1999): Sigmoid Emax model mapping propofol Ce to BIS
- **Greco Interaction Model** (Bouillon et al., 2004): Response surface for propofol-remifentanil synergy

## Paper

The accompanying paper is available at:
- [PDF (GitHub)](https://github.com/HassDhia/anesthesim/blob/main/paper/anesthesim.pdf)

## Citation

If you use anesthesim in your research, please cite:

```bibtex
@software{dhia2026anesthesim,
  author = {Dhia, Hass},
  title = {AnestheSim: Gymnasium Environments for Reinforcement Learning in Automated Anesthesia Drug Dosing},
  year = {2026},
  publisher = {Smart Technology Investments Research Institute},
  url = {https://github.com/HassDhia/anesthesim}
}
```

## License

MIT License. See [LICENSE](LICENSE) for details.

## Contact

Hass Dhia - Smart Technology Investments Research Institute
- Email: partners@smarttechinvest.com
- Web: [smarttechinvest.com/research](https://smarttechinvest.com/research)
