This repository provides a reproducible experimental framework for evaluating the use of a large language model (LLM) as a supervisory element in closed-loop process control. The benchmark system is a nonlinear water-tank model implemented in MATLAB/Simulink, with online interaction between Simulink, Python, and a locally hosted LLM via Ollama.
The LLM does not replace classical feedback control. Instead, it periodically suggests PID gains that are applied within a conventional PID controller, enabling controlled comparison between:
- classical PID control,
- autotuned PID control,
- hybrid LLM-supervised PID control.
The repository corresponds to the experiments reported in the manuscript
“An Experiment of Using a Large Language Model to Control a Water Tank System.”
This repository contains three experiment folders:
-
Comparison of LLM LLM-PID PID PIDautotuned/
Main comparison experiment used in the paper.
Includes:watertank_LLM_PID_PIDautotuned_compare.slx- MATLAB helper functions
- Python LLM interface scripts
- shared data files
-
Step Fault/
Step-disturbance robustness test. -
Ramp Fault/
Ramp-disturbance robustness test.
Each folder contains a self-contained Simulink model corresponding to the named experiment.
watertank_LLM_PID_PIDautotuned_compare.slx
Main comparison model (hybrid LLM-PID vs PID vs autotuned PID)
LLM_Controller.m
Applies LLM-generated PID gains and enforces safety bounds.getLLMGains.m
MATLAB wrapper that calls Python to query the LLM.TrimAndLinearizeSimulinkModelsExample.m
Used for linearization and autotuned PID baseline.watertankdat.mat
Water-tank model parameters.
llm_interface.py
Handles prompt construction, Ollama calls, output parsing, and logging.llm_controller.py
Direct LLM controller (illustrative baseline; not used for stable control).
-
Supervisory Control
The LLM is queried at a fixed supervisory update period (e.g., 0.5 s). Between updates, PID gains remain constant. -
Latency-Aware Execution
The supervisory update period is selected to exceed worst-case observed LLM inference latency. -
Safety Constraints
All LLM-generated gains are clamped to predefined bounds before being applied. -
Reproducibility First
Raw LLM outputs and parsed values are logged for analysis and debugging.
- MATLAB + Simulink 2025b
- Python 3.12.x
- Ollama (local LLM server)
- LLM model: LLaMA 3.2 (3B) via Ollama
- Simulink
- Control System Toolbox
- Simulink Control Design
- Install MATLAB/Simulink and Python.
- Install Ollama and pull the model:
ollama pull llama3.2
- Start the Ollama server
ollama serve
- Open MATLAB and set the current folder to the repository root.
- Open one of the experiment folders and run the corresponding
.slxmodel.
Detailed build and run instructions are provided in MANUAL.md.
- LLM outputs are stochastic by nature.
- For reproducibility:
- Keep model versions fixed.
- Log raw LLM responses.
- Use conservative gain bounds.
- Avoid interpreting single runs as optimal performance.
If you use this repository in academic work, please cite:
Roberts, J., Wen, H., Zaidi, A., McLeod, A. An Experiment of Using a Large Language Model to Control a Water Tank System. (Manuscript under revision)
This project is intended for academic and educational use.