ANFIS and NARX Surrogate Models for Nonlinear MPC-Based Artificial Pancreas Control
Abstract
Nonlinear Model Predictive Control (NMPC) for artificial pancreas systems traditionally relies on low-order analytical models, such as the Bergman minimal model, which trade physiological fidelity for tractability. Learned, black-box dynamics models (e.g., neural networks) promise higher fidelity to real physiology by fitting richer simulators, but their reliability as internal models inside a closed-loop optimizer is rarely audited beyond aggregate open-loop accuracy. We investigate whether a lightweight, differentiable NARX network can safely replace an analytical model inside an NMPC loop for blood glucose regulation in Type 1 Diabetes, and surface a failure mode that pure prediction metrics fail to reveal. Approach. We generate training data from the FDA-accepted simglucose simulator (Dalla Man model, 19 states), predicting only measurable, controller-relevant quantities: glucose G(k+1) from a sliding window of glucose, insulin infusion, and announced carbohydrates, [G,u,D]k−W+1:k. A NARX network (W=3, 2 hidden layers, 2,753 parameters) and an ANFIS model (W=2, 448 parameters) are trained and compared. To counter the well-known bias of clinical controllers toward basal-dominated trajectories, we design a persistent-excitation datacollection controller that stochastically injects insulin boluses (with hypoglycemia safety cutoffs), increasing coverage of the insulin saturation region (u>4.0) by 7.5×. The trained NARX is exported to CasADi and embedded as the internal transition model of a new NMPC (NMPC-NARX), verified against the PyTorch model to a discrepancy of 2×10−6 mg/dL, and benchmarked in closed loop against the analytical NMPC-Bergman and a classical anti-windup PID. Results. In open loop, the NARX model achieves R2=0.994 (MAE = 1.37 mg/dL) on held-out data, and in closed loop it stabilizes a meal-disturbance scenario with lower tracking error (ISE) than both baselines. However, a targeted safety audit of a hypoglycemia-recovery scenario (G0=60 mg/dL, no meal) revealed that NMPC-NARX infused 266mU of insulin – over 200× more than the Bergman-based and PID controllers – while the patient was hypoglycemic, a clinically dangerous action. Probing the learned model directly showed it had inverted the sign of the true physiological relationship in this region: at G=100.5 mg/dL, it predicted higher future glucose under maximal insulin (u=5) than under zero insulin. We trace this to a covariate-shift artifact: because every safety-aware data-collection policy (clinical and excitation-based) suppresses insulin near hypoglycemia by design, the training set contained zero samples in the low-glucose/active-insulin quadrant, and the solver exploited this extrapolation gap to minimize its cost function. Takeaway. Aggregate accuracy metrics (R2, closed-loop tracking error) can mask safety-critical failures that only appear when a learned model is queried outside the support of its training distribution – precisely the regions a feedback optimizer is incentivized to explore. We argue that deploying differentiable surrogate models inside safetycritical control loops requires explicit monotonicity constraints or barrier-based cost penalties, not just higher predictive accuracy, and we release our excitation-based data collection protocol and safety-auditing methodology as a template for evaluating other learned-model-in-the-loop control systems.