A Research-Grade Artificial Pancreas System (APS) Simulator
ProjectβEndocrine is a complete, modular, research-grade Artificial Pancreas System (APS) simulator combining:
- Physiological glucoseβinsulin modeling
- Adaptive PID-based insulin control
- Safety-critical logic (IOB, suspension, max-rate limits)
- Reinforcement Learning (RL) environment
- Cohort-level virtual patient testing (10β200 subjects)
- Publication-level visualization and evaluation tools
This simulator aims to provide an open, interpretable, and experiment-friendly APS research platformβideal for:
- Biomedical engineering students
- Control systems researchers
- RL researchers
- Medical device prototyping
- Teaching and simulation environments
Type-1 Diabetes (T1D) results in absence of endogenous insulin, requiring lifelong external insulin delivery. Maintaining glucose in the target range (70β180 mg/dL) is challenging due to:
- Meal disturbances
- Sensor noise, delays, drift
- Large inter-patient variability
- Non-linear insulin action
- Uncertainty in digestion and absorption
- Noisy CGM readings
An Artificial Pancreas System (APS) automates insulin delivery using:
- A continuous glucose sensor (CGM)
- An insulin pump
- A control algorithm
ProjectβEndocrine models this entire loop end-to-end.
βββββββββββββββββββββββββββββββββ
β Meal Input β
ββββββββββββββββ¬βββββββββββββββββ
β
ββββββββββββββββββββββ
β Gut Absorption β
ββββββββββββ¬ββββββββββ
β
ββββββββββββββββββββββββ
β Glucose Dynamics βββββΊ CGM Sensor (Noise)
ββββββββββββ¬ββββββββββββ
β
ββββββββββββββββββββββββββββββββββββββββ
β Insulin Action (X) β
β PK/PD + Delays + Nonlinear Effects β
ββββββββββββββββββββ¬ββββββββββββββββββββ
β
βββββββββββββββββ
β Controller β
βPID / RL / APIDβ
ββββββββ¬βββββββββ
β
βββββββββββββββββββββββββ
β Safety Layer (IOB, β
β Suspend, Rate Limits) β
ββββββββββββ¬βββββββββββββ
β
Insulin Delivery
Fully modular β each block can be replaced, extended, or studied independently.
The simulator includes a nonlinear glucoseβinsulin model:
Glucose Model
- Plasma glucose compartment
- Endogenous glucose production (EGP)
- Meal appearance via two-stage gut absorption pools
- Nonlinear insulin-dependent & insulin-independent clearance
Insulin Model
- Subcutaneous insulin absorption
- Plasma insulin with clearance
- Insulin effect (X) compartment
- Full PK/PD chain
Sensor Model (CGM)
- Gaussian noise
- Filtering
- Measurement bias (optional)
Example subject:
- Proportional: responds to immediate deviation
- Integral: corrects long-term bias
- Derivative: anticipates rate-of-change
- Uses filtered CGM input
- Includes anti-windup and clamp logic
Learns patient-specific basal patterns:
- Predictive low-glucose suspend
- Negative-trend veto
- Max insulin rate limiter
- Insulin-On-Board (IOB) constraints
- Hard safety clamps
ProjectβEndocrine exposes a Gym-style RL environment:
reset(),step()API- Full state vector extraction
- Reward functions (TIR-weighted, risk index, smoothness)
- Multi-patient training
- Deterministic and stochastic modes
Supports algorithms such as:
- PPO
- SAC
- TD3
- Model-Based RL
Each virtual subject varies in:
- insulin sensitivity
- carb absorption rate
- glucose clearance
- insulin clearance
- basal metabolic drift
- noise profile
Cohort Mean Β± STD
Sample traces (first 6 subjects)
Full cohort (200 subjects)
TIR distribution (200 subjects)
Subject-wise TIR
Glycemic Variability
Mean Glucose Distribution
Mean Glucose vs CV%
TIR / TAR / TBR Boxplot
download.mp4
git clone https://github.com/HeroicKrishna160905/Project-Endocrine.git
cd Project-Endocrine
pip install -r requirements.txt
Or [launch in Google Colab](https://colab.research.google.com/) (recommended).
## π§ͺ Quickstart
### Single Patient Simulation
```python
from endocrine.simulator import APS_Simulator
sim = APS_Simulator()
results = sim.run()
sim.plot()from endocrine.evaluation import run_cohort
stats, df = run_cohort(n_subjects=200)
print(stats)
df.head()Project-Endocrine/
β
βββ endocrine/
β βββ physiology/ # PK/PD model, glucose, insulin
β βββ controller/ # PID, adaptive basal, safety
β βββ rl_env/ # Gym-style RL environment
β βββ evaluation/ # Cohort simulation + metrics
β βββ utils/
β
βββ notebooks/
β βββ v6_3_realism.ipynb
β βββ v6_3_RL.ipynb
β
βββ assets/ # Figures, demo video
βββ results/ # Saved outputs
βββ README.md
βββ LICENSE
ProjectβEndocrine provides:
- β A fully custom glucoseβinsulin PK/PD model (Not dependent on UVA/Padova or simglucose)
- β A modular adaptive PID controller Designed for interpretability & robustness.
- β A complete safety layer implementation (LOWS, IOB, rate limits, rapid-drop veto)
- β A Gym-compatible RL environment Allowing direct integration with PPO/SAC/TD3.
- β Realistic cohort simulation 200-subject evaluation with variability.
- β Publication-grade visualizations All included as reproducible notebooks.
- β Academic grounding Supported by nonlinear control literature β e.g., observer-based LMI designs from our professor's paper (2019).
- UVA/Padova T1D Simulator
- GluCoEnv: High-performance APS RL environment
- Control-IQ / DiAs APS systems
- PID/MPC control for APS
- Reinforcement Learning for glucose control
Observer-based nonlinear control for glycemic regulation, using LMI-stabilized designs:
- 2019 β "Observer based nonlinear control: An LMI approach." (Full text provided internally)
MIT License Open for academic, research, and personal use.
Pull requests, ideas, and discussions are welcome.
- Inspired by global diabetes research.
- Built with guidance from professors and real control theory literature.
- Developed with attention to openness, reproducibility, and scientific rigor.











