Skip to content

Naga

Naga is a simulation engine for Python, built on SimPy: you describe a system as a model file, and it runs the model and gives back an answer with its confidence interval. Python 3.11 or later; the naga command comes with it.

pip install nagasim

Ten lines build the textbook single-server queue, run it twenty times and set the result beside the exact answer:

from nagasim import build as nb

lam, mu = 1.0, 1.25                                  # customers arrive, and are served, per minute
m = nb.Model(time_limit=5500, warmup=500, replications=20)
m.add_resource(name="Server")
arrive = m.create(distribution=nb.Exponential(rate=lam))
serve = m.process(resource_name="Server", distribution=nb.Exponential(rate=mu))
arrive >> serve >> m.dispose()

row = m.run(seed=0).kpis().set_index("metric").loc["resource/Server/Time in Queue Avg"]
print(f"simulated mean wait {row['mean']:.2f} +/- {row['ci95']:.2f} minutes (95% interval)")
print(f"exact, M/M/1        {lam / mu / (mu - lam):.2f} minutes")
simulated mean wait 3.30 +/- 0.29 minutes (95% interval)
exact, M/M/1        3.20 minutes

The simulated interval brackets the exact value, which is what a trustworthy simulator does and what the reference models page checks against known answers. It also shows the point of Naga: the same clock runs discrete-event processes, system dynamics, agents and continuous levels with exact crossing times, so one model can hold all four.

What goes into the engine, what it does, and what comes out

The engine works in four stages: it checks the model file, runs it on one clock, proves results with replications and scenarios, and follows a live plant. What comes back is results with their law-check warnings, web pages to share (a replay and a viewer) and forecasts.

What you can model

  • Processes and queues: blocks (create, process, seize and release, batch, match, decide, route, transport, ...) joined by links, with resources, sets, schedules, failures, costs, expressions and submodels.
  • Material handling: path networks, vehicle fleets with batteries and chargers, conveyors, motion cycles, machines tended by mobile robots.
  • Stocks and flows (system dynamics: auxiliaries, delays, subscripts, units, XMILE and Vensim import) and agents (statecharts, messages, space, networks), in the same model as the processes.
  • Rates: tanks, batteries and other quantities that fill and drain continuously, with exact crossing times.

What it does around a run

  • Statistics you can defend: independent replications, Student-t intervals, warm-up handling, reproducible random streams, checked against models with a known answer.
  • Experiments: named scenarios with paired comparisons, design of experiments, ranking-and-selection, optimisation, calibration and an investment case.
  • A twin of a plant: readers for files, SQL, REST, MQTT, OPC UA and Modbus TCP, a run that starts from the plant's state, forecasts scored against what then happened, and actions compared on the forecast. The twin reads and proposes; the one writer is naga plc-write, which runs only on a person's command and writes to a soft PLC on the same machine (loopback only).
  • Outputs: pandas frames, Excel, CSV, JSON, a self-contained HTML report, a recorded run to replay, and a viewer in which the engine runs in a reader's browser.

Three ways in

  • The command line: naga run single_server --report report.html runs a bundled model and writes its report; naga --help lists every command.
  • Python: nagasim.api.run_model runs a file and returns pandas frames, and nagasim.build writes a model in code, with your editor completing every parameter. See the Quick start and the Python API.
  • An agent: an MCP server offers the same operations as tools, so an assistant can build, validate, run and explain a model. See Use with an agent.

Whichever way you use, the model is the same JSON file, so what one person builds in Python an agent can check and a colleague can open.

What's new in 0.21

  • Answers that say yes or no. A study's write-up, control room and slides open with Yes, No or Not clear yet, judged from each goal's 95% interval, and explain the terms they use. A run whose law checks fail reads "not valid" with the reason; a run that may be biased low (people still inside at the end, a cold start) keeps its numbers with that caveat.
  • Studies a person checks. naga study confirm records a person's own check that the model matches reality, and each option's difference from the Baseline comes with its paired 95% interval.
  • The whole model is validated. A misspelt key at any level is named with the nearest valid one, scenario override paths are checked, and a zero, negative or empty distribution value is refused before the run.
  • Lean results. run_model and run_scenarios return only the result frames that have rows, as in the snippet below; keep_empty=True gives every frame. uncertainty.input_uncertainty_frame gives input uncertainty as a table, and naga uncertainty --out writes it.
  • Before you upgrade. results["store_summary"] on a frame with no rows now raises a KeyError that names the change: use results.get(...), in, or keep_empty=True. MCP clients read the structured answer instead of the text. Models with a zero or negative distribution value are refused. Bundled models give the same results as 0.20.1 (62 of 62, on two seed sets).
from nagasim import api

results = api.run_model("single_server", replications=2, seed=0)
print(sorted(results))                # only the frames that have rows
print(results.get("store_summary"))   # None: this model has no store

All releases: What's new for the highlights, Release notes for every change.

What it is not

  • Not an editor. There is no window, no canvas and no animation here. naga run --player writes one HTML file that replays a run in a browser, and naga viewer one in which a reader re-runs the model; those are the only pictures. Naga Studio, a desktop editor built on Naga, adds the canvas, 2D and 3D views, videos of a run and a package for a client, and opens and saves the same model files.
  • Not SimPy with helpers. You do not write generator functions. A model is data, even when Python code builds it, and what a block can do is fixed by its type and its expressions. When the logic you need is not a block, raw SimPy is the better tool: see Coming from SimPy.
  • Not stable yet. Before 1.0 the Python API may change between minor versions. Model files always migrate forward, and agent tool names and problem codes are permanent.

Where to go

You want to Read
run something in a minute Quick start
see what a model looks like and what it gives Gallery
check the engine against models with a known answer Reference models
map what you know onto this Coming from SimPy, Arena, AnyLogic, Vensim, NetLogo
write a model file Model file format, Blocks
know what a part of a model does when it runs Modelling guide
compare, search and check models Experiments, Model assertions, Semantic diff
call it from a script or a notebook Python API
let an agent build and run models Use with an agent
give someone a model they can run in a browser Sharing a model
follow a plant and forecast from its state Digital twin
know what a model file can and cannot do to your machine Security

An LLM agent reading this site can start from llms.txt.

Apache License 2.0. Source and issues: github.com/TheMattHub/naga.