# Naga

Naga is a simulation engine for Python, built on [SimPy](https://simpy.readthedocs.io/): 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.

```console
pip install nagasim
```

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

```python
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")

```

```text
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](validation/reference-models.md) 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](img/overview.svg)

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.

<div class="grid cards" markdown>

-   **Python modeller**

    ---

    Build a model in code, run replications, read the results.

    [Learning path](learn/index.md), [Quick start](quickstart.md) and the [Python API](python-api.md)

-   **Planner or analyst**

    ---

    See what a finished study looks like, then compare scenarios of your own.

    [Worked examples](worked-examples/airport-ground-support.md), [Experiments](experiments.md) and [reading results](learn/6-results.md)

-   **Integrator**

    ---

    Run it from a pipeline, check files in CI, build on the formats.

    [Command line](reference/cli/index.md), [model file keys](reference/schema.md) and [MCP](mcp.md)

-   **AI agent**

    ---

    Read the site as plain text, validate against the schema, call the tools.

    [llms.txt](llms.txt), [llms-full.txt](https://thematthub.github.io/naga/llms-full.txt) (the whole site as one file), a Markdown copy of every page (its address with `.md`), the [JSON Schema](schema/model-v32.json) and the [agent tools](reference/tools.md)

</div>

## 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](quickstart.md) and the [Python API](python-api.md).
- **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](mcp.md).

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).

```python
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](whats-new.md) for the highlights, [Release notes](changelog.md) 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](coming-from/simpy.md).
- **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](quickstart.md) |
| see what a model looks like and what it gives | [Gallery](gallery/index.md) |
| check the engine against models with a known answer | [Reference models](validation/reference-models.md) |
| map what you know onto this | Coming from [SimPy](coming-from/simpy.md), [Arena](coming-from/arena.md), [AnyLogic](coming-from/anylogic.md), [Vensim](coming-from/vensim.md), [NetLogo](coming-from/netlogo.md) |
| write a model file | [Model file format](model-file-format.md), [Blocks](reference/blocks/index.md) |
| know what a part of a model does when it runs | [Modelling guide](modelling-guide/index.md) |
| compare, search and check models | [Experiments](experiments.md), [Model assertions](model-assertions.md), [Semantic diff](semantic-diff.md) |
| call it from a script or a notebook | [Python API](python-api.md) |
| let an agent build and run models | [Use with an agent](mcp.md) |
| give someone a model they can run in a browser | [Sharing a model](sharing-a-model.md) |
| follow a plant and forecast from its state | [Digital twin](digital-twin.md) |
| know what a model file can and cannot do to your machine | [Security](security.md) |

An LLM agent reading this site can start from [`llms.txt`](llms.txt).

Apache License 2.0. Source and issues: [github.com/TheMattHub/naga](https://github.com/TheMattHub/naga).
