# Quick start

A single-server queue, from nothing to numbers.

## Install

```console
pip install nagasim
```

Python 3.11 or later. This gives you the `nagasim` package and the `naga` command, and
`naga --version` prints the version you have:

```console
naga --version
```

A misspelt command or bundled model name answers with the nearest match, so `naga rnu` suggests
`naga run`.

## A model file

Customers arrive one a minute on average, one server serves 1.25 a minute, and they leave. Save
this as `mm1.json`:

```json title="mm1.json"
{
  "$schema": "https://thematthub.github.io/naga/schema/model-v32.json",
  "blocks": [
    {"name": "arrive", "type": "CREATE",
     "params": {"distribution": "Exponential", "dist_params": {"rate": 1.0}}},
    {"name": "serve", "type": "PROCESS",
     "params": {"action": "Seize Delay Release", "resource_name": "Server",
                "distribution": "Exponential", "dist_params": {"rate": 1.25}}},
    {"name": "leave", "type": "DISPOSE", "params": {}}
  ],
  "links": [{"source": "arrive", "target": "serve"}, {"source": "serve", "target": "leave"}],
  "resources": [{"name": "Server", "capacity": 1}],
  "simulation": {"time_limit": 10000, "warmup": 1000, "replications": 5}
}
```

Three blocks, two links, one resource. Times are in minutes unless the file says otherwise.
Every parameter of every block type is in the [block reference](reference/blocks/index.md).
The `"$schema"` line is optional: it points your editor at the file format, so it completes
keys and parameters and flags a wrong one as you type (the engine ignores it).

## Run it

```console
naga validate mm1.json
naga run mm1.json
```

`validate` prints `mm1.json: OK`, or each problem with a stable code, all found in one pass. A
misspelt key is a warning that names the nearest valid key, and the file keeps it. A value that
cannot work, such as a negative rate or text where a number goes, is an error before the run.
A run error names its block on one line. `run` prints one row per
system KPI (entities created and completed, average time in the system, throughput, queue
lengths, utilisation) with its mean over the replications, the 95% half-width and the number of
replications behind it. Below the table come a `WARN` line for each law check the run did not
pass, such as an offered load of 1 or more, and the replications a half-width of 5% of the
mean would need. `--raw` prints the per-replication table instead, and `--no-checks` leaves the
`WARN` lines out. `--json` prints the same summary as JSON, with each row's `key` and `ci95`; the
older `metric` and `half_width` fields go in the next minor release.

Queueing theory gives this queue a mean time in the system of 1 / (1.25 - 1) = 4 minutes and a
utilisation of 80%. Your numbers should be close to both; the half-width in the table says how
close.

A report with charts, or the frames as a workbook:

```console
naga run mm1.json --replications 10 --report report.html
naga run mm1.json --no-logs --out results.xlsx
```

Without `--no-logs` the workbook also holds every event of every entity, which on a run this
long is most of its size and of the time it takes to write.

## From Python

```python
from nagasim.api import run_model

results = run_model("mm1.json", replications=10, seed=0)
kpis = results.kpis()                       # metric, mean, std, ci95 (the 95% half-width)
print(kpis[kpis["metric"].isin(["system/Avg Throughput Time", "system/Avg Resource Usage %"])])
print(results["resource_summary"].head())   # one row per resource and replication
```

`results` is a dictionary of pandas frames; the [KPI reference](reference/kpis.md) lists every
frame and column. The same seed gives the same frames.

The same file can be written from Python ([Build a model in Python](python-api.md#build-a-model-in-python)):

```python
from nagasim import build as nb

m = nb.Model(time_limit=10000, warmup=1000, replications=5)
m.add_resource(name="Server", capacity=1)
arrive = m.create("arrive", distribution=nb.Exponential(rate=1.0))
serve = m.process("serve", resource_name="Server", distribution=nb.Exponential(rate=1.25))
arrive >> serve >> m.dispose("leave")
m.save("mm1.json")
```

## Next

- Build one model step by step in Python, from a queue to scenarios and the event log:
  the [learning path](learn/index.md).
- Change the model and compare: `naga scenarios` (the system KPIs in a short table; `--kpi KEY`
  or `--all` for more), `naga optimize`, `naga doe`
  ([command line reference](reference/cli/index.md)).
- Larger models that run as they are: `naga examples` lists them, `naga run job_shop_jackson`
  runs one, and the [gallery](gallery/index.md) shows each with its numbers.
- The whole file format: [Model file format](model-file-format.md).
- The same model in raw SimPy, side by side: [Coming from SimPy](coming-from/simpy.md).
- Prefer a canvas to a JSON file? Naga Studio, a desktop editor built on Naga, opens and saves
  these model files.
