# Using the example `treat-sim` model

For examples in this section, we'll use the `treat-sim` simulation model. This is an example model of a terminating system based on exercise 13 from page 170 of:

> Nelson. B.L. (2013). Foundations and methods of stochastic simulation. Springer.


# Installing the model

The model is available on [GitHub](https://github.com/pythonhealthdatascience/stars-treat-sim), and can be installed from [PyPI](https://pypi.org/project/treat-sim/):

``` bash
pip install treat-sim
```


``` python
from treat_sim.model import (
    Scenario,
    multiple_replications,
    single_run,
)
```


# Running a single replication

1.  **Create an instance of `Scenario`**. This contains all the parameters used in the model (e.g. resources and statistical distributions for arrivals and activities).
2.  **Run the model once with [single_run()](../../reference/output_analysis.ReplicationsAlgorithmModelAdapter.md#sim_tools.output_analysis.ReplicationsAlgorithmModelAdapter.single_run)**. This function is passed an instance of `Scenario` and an integer to set the random numbers used.

This returns a `pandas.DataFrame` with all performance measures for that replication, e.g. waiting times, utilisation, throughput.


``` python
scenario = Scenario()

# Run a single replication
results = single_run(scenario, random_no_set=1)
print(results.T)
```


    rep                                      1
    00_arrivals                     227.000000
    01a_triage_wait                  57.120114
    01b_triage_util                   0.621348
    02a_registration_wait            90.002385
    02b_registration_util             0.836685
    03a_examination_wait             14.688492
    03b_examination_util              0.847295
    04a_treatment_wait(non_trauma)  120.245474
    04b_treatment_util(non_trauma)    0.912127
    05_total_time(non-trauma)       233.882040
    06a_trauma_wait                 133.813901
    06b_trauma_util                   0.834124
    07a_treatment_wait(trauma)        3.715446
    07b_treatment_util(trauma)        0.367507
    08_total_time(trauma)           301.521195
    09_throughput                   161.000000


You can extract a single metric like this:


``` python
print(results["01a_triage_wait"].iloc[0])
```


    57.120114285877285


# Running multiple replications

Running several replications at once is very similar to running a single replication -- you just use the `multiple_replications()` function instead. You'll need to provide an instance of `Scenario`, along with the number of replications you want to run.


``` python
scenario = Scenario()

# Run multiple replications
results = multiple_replications(scenario, n_reps=20)
results.head()
```


|  | 00_arrivals | 01a_triage_wait | 01b_triage_util | 02a_registration_wait | 02b_registration_util | 03a_examination_wait | 03b_examination_util | 04a_treatment_wait(non_trauma) | 04b_treatment_util(non_trauma) | 05_total_time(non-trauma) | 06a_trauma_wait | 06b_trauma_util | 07a_treatment_wait(trauma) | 07b_treatment_util(trauma) | 08_total_time(trauma) | 09_throughput |
|----|----|----|----|----|----|----|----|----|----|----|----|----|----|----|----|----|
| rep |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |
| 1 | 230.0 | 24.280943 | 0.613250 | 103.242292 | 0.854504 | 31.089680 | 0.861719 | 152.483394 | 0.890904 | 234.759918 | 236.508444 | 1.028887 | 13.701783 | 0.607996 | 346.698079 | 171.0 |
| 2 | 227.0 | 57.120114 | 0.621348 | 90.002385 | 0.836685 | 14.688492 | 0.847295 | 120.245474 | 0.912127 | 233.882040 | 133.813901 | 0.834124 | 3.715446 | 0.367507 | 301.521195 | 161.0 |
| 3 | 229.0 | 28.659383 | 0.573698 | 112.242503 | 0.848514 | 21.374092 | 0.856306 | 94.019885 | 0.868888 | 208.361290 | 276.422566 | 0.874245 | 12.252175 | 0.464740 | 440.515502 | 167.0 |
| 4 | 240.0 | 24.801392 | 0.610618 | 121.535491 | 0.867779 | 31.556444 | 0.868617 | 150.351654 | 0.890564 | 249.690045 | 149.374881 | 0.879746 | 16.520898 | 0.663415 | 281.060924 | 172.0 |
| 5 | 233.0 | 17.679156 | 0.643085 | 103.606078 | 0.870109 | 25.505255 | 0.882532 | 157.255345 | 0.925467 | 233.296674 | 250.712956 | 0.862277 | 9.225844 | 0.421116 | 380.832994 | 164.0 |
