add: LLM Epidemic (SIR) model using Chain-of-Thought reasoning#372
add: LLM Epidemic (SIR) model using Chain-of-Thought reasoning#372abhinavk0220 wants to merge 15 commits intomesa:mainfrom
Conversation
…classic SIR epidemic simulation where agents use LLMChain-of-Thought reasoning to decide their behavior during anoutbreak, instead of fixed stochastic transition probabilities.Agents choose between:- isolate: stay home, reduce infection risk- move_freely: normal activity, higher transmission risk- seek_treatment: if infected, accelerate recoveryThe model demonstrates how LLM-driven behavioral reasoningproduces emergent epidemic curves that reflect nuanced humandecision-making during crises, contrasting with classicalfixed-rule SIR dynamics.Includes .env.example for API key configuration (supportsGemini, OpenAI, Anthropic, and Ollama).Visualization includes spatial grid (color-coded by healthstate) and real-time SIR plot tracking population counts.Reference: Kermack & McKendrick (1927)
for more information, see https://pre-commit.ci
…220/mesa-examples into add/llm-epidemic-model
for more information, see https://pre-commit.ci
|
Thanks for the PR, looks like a cool model. Could you:
|
Thanks @EwoutH! The test failure is Will add a screenshot and move to |
…220/mesa-examples into add/llm-epidemic-model
for more information, see https://pre-commit.ci
…220/mesa-examples into add/llm-epidemic-model
for more information, see https://pre-commit.ci
Hi @EwoutH! Updates done:
READY TO MERGE !!!!! |
|
Hi @abhinavk0220, Does it belong? Is it correct and current? Is it clean? Using LLM Chain-of-Thought reasoning to replace fixed SIR transition probabilities is a genuinely inventive idea, and the way it is framed shows real understanding of what makes this approach interesting. I also liked how the README clearly explains the motivation, draws a thoughtful distinction from classical models, and includes a proper academic reference. Once the few issues flagged above are resolved, this will definitely be a strong addition to the examples. |
- Add epidemic_initial.png (Step 0: seeding), epidemic_spreading.png (Step 3: curves crossing), epidemic_complete.png (Step 13: full SIR arc) - Rewrite README with visualization section, step-by-step table, and contrast with classical Kermack-McKendrick (1927) SIR model - Document key finding: full SIR bell curve from LLM reasoning alone, no hardcoded β or γ parameters Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
…model Replace global agent iteration in _update_health with spatial Moore neighborhood check (radius=1 via cell.connections). This makes the spatial grid meaningful — an isolated agent in one area cannot infect a susceptible agent across the map. - Infection now requires direct proximity (immediate Moore neighbors) - vision=2 intentionally wider than infection radius: agents observe a broader area to inform LLM reasoning, but transmission is local - Infection probability per neighbor raised from 0.1 to 0.3 to maintain realistic epidemic dynamics with the smaller spatial neighbor set - Handles edge case: agent without a cell set (no infection check) Addresses peer review feedback from @savirpatil (PR mesa#372) Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
|
Thanks @savirpatil for the thorough review both points are accurate and I appreciate you catching them. On global infection check vs. spatial proximity: You're right The infection probability per neighbor was raised from On This is intentional by design Both issues resolved in: b4bc7d2 |
Summary
A classic SIR (Susceptible-Infected-Recovered) epidemic simulation where agents use LLM Chain-of-Thought reasoning to decide their behavior during an outbreak — instead of following fixed stochastic transition probabilities.
Unlike traditional SIR models, agents reason about their situation — weighing personal health risk, observed neighbor states, and community responsibility — before deciding to isolate, move freely, or seek treatment.
The Disease Dynamics
Each agent is in one of three states:
What makes this different from classical SIR
Classical SIR uses fixed β (transmission rate) and γ (recovery rate). The epidemic curve is fully determined by two numbers.
Here, agents reason at each step:
This produces behavioral heterogeneity: some agents isolate immediately, others rationalize continued movement. The macro curve still follows SIR dynamics, but its shape reflects individual decision-making.
Visualization
Step 0 — Initial seeding (3 infected, 17 susceptible):
Step 3 — Epidemic accelerating (curves crossing):
Step 13 — Full SIR arc complete (all recovered):
Why this matters: This is the Kermack-McKendrick (1927) SIR curve reproduced with zero hardcoded β or γ parameters. The epidemic arc — seed → spread → peak → recovery — emerges entirely from LLM reasoning about individual health decisions. Classical SIR cannot represent individual behavioral variation at all.
How to Run
cp .env.example .env # fill in your API key pip install -r requirements.txt solara run app.pySupported LLM Providers
Gemini, OpenAI, Anthropic, Ollama (local) — configured via
.env.Reference
Kermack, W. O., & McKendrick, A. G. (1927). A contribution to the mathematical theory of epidemics. Proceedings of the Royal Society of London. Series A, 115(772), 700–721.
GSoC contributor checklist
Context & motivation
The SIR epidemic model is a foundational model in mathematical epidemiology. Adding LLM agents replaces fixed β/γ parameters with genuine behavioral reasoning — agents decide whether to isolate based on their health state, neighbor states, and sense of community responsibility. This produces a more realistic behavioral epidemic model than stochastic rule-based SIR.
What I learned
LLM agents reproduce the full Kermack-McKendrick SIR bell curve through reasoning alone — no hardcoded transmission or recovery probabilities. The curve emerges from 20 agents individually deciding how to behave at each step. The behavioral heterogeneity (some isolate, some don't) shapes the curve in ways that purely stochastic models can only approximate with population-level parameters.
Learning repo
🔗 My learning repo: https://github.com/abhinavk0220/GSoC-learning-space
🔗 Relevant model(s): https://github.com/abhinavk0220/GSoC-learning-space/tree/main/models/llm_epidemic
Readiness checks
ruff check . --fix)AI Assistance Disclosure
This PR was developed with AI assistance (Claude) for code generation and debugging. All code has been reviewed, tested, and understood by the contributor.
Mesa Examples Review Checklist (#390)
Does it belong?
Is it correct?
rngseed — LLM outputs are non-deterministic by naturellm/directoryIs it clean?