How many replications do I need, and what warm-up?¶
In short
- You build: a script that finds how many replications a target precision needs, and how long a warm-up to set.
- You need: Python 3.11 or later,
pip install nagasim, and the model from step 1 of the learning path, a single queue.- Time: about ten minutes.
One run of a model is one sample, so a result needs many runs and a stated precision. By the end you ask the engine for the replications a precision target needs. You read the half-width it reaches. You set a warm-up from the data instead of a guess.
This page has three parts: Step by step, Check yourself and If stuck. The last section lists the reference pages for the elements used.
Step by step¶
- Save this script as
goal-replications.py. It builds the one-queue model, asks for a precision, asks for a warm-up, and asks for the precision again with that warm-up.
from nagasim import api, build as nb, experiments
m = nb.Model(time_limit=480, warmup=0) # one 8-hour day, in minutes
m.add_resource(name="Server")
arrive = m.create(distribution=nb.Exponential(rate=1.0)) # a customer a minute
serve = m.process(resource_name="Server", distribution=nb.Exponential(rate=1.25))
arrive >> serve >> m.dispose()
layout = m.to_dict()
wait = "resource/Server/Time in Queue Avg"
# 1. Ask for a precision instead of guessing a count: stop when the half-width is within 10% of the mean.
results, report = experiments.run_until_precision(layout, wait, relative=0.10, n0=5, seed=1)
print(f"{report['replications']} replications: mean wait {report['mean']:.2f} minutes, "
f"half-width {report['half_width']:.2f}, target met: {report['met']}")
# 2. Ask MSER-5 where the start-up bias ends, on 50 runs each twice as long as the day.
long_runs = api.run_model(layout, replications=50, seed=1, run_length=1000, warmup=0)
suggestion = experiments.suggest_warmup(long_runs)
print(f"suggested warm-up {suggestion.warmup:.0f} minutes, reliable: {suggestion.reliable}")
# 3. Run the day again, now with that warm-up, to the same precision.
results, report = experiments.run_until_precision(layout, wait, relative=0.10, n0=5, seed=1,
warmup=suggestion.warmup)
print(f"with the warm-up: {report['replications']} replications: mean wait {report['mean']:.2f}, "
f"half-width {report['half_width']:.2f}")
- Run it.
python goal-replications.py
You should see this. The script seeds itself, so the numbers are the same on your machine.
84 replications: mean wait 3.17 minutes, half-width 0.32, target met: True
suggested warm-up 84 minutes, reliable: True
with the warm-up: 106 replications: mean wait 3.37, half-width 0.34
-
Read the first line.
run_until_precisionruns five replications first. Then it estimates how many more it needs so that the half-width of the confidence interval is within 10% of the mean. It keeps adding until the target is met or the cap of 200 is reached. -
Read the second line. The model starts empty, so the first customers wait less than they would in a busy hour.
suggest_warmupapplies MSER-5 to 50 longer runs and returns the time after which the start-up bias is gone.reliable: Truemeans the answer fell in the first half of the run, so you can trust it. -
Read the third line. The warm-up discards the empty start of each run, so every replication now measures a busy system. Here the same precision needs more replications than before, because each one now keeps less of the day.
-
Compare both means with the exact value. This queue is an M/M/1 queue, and its exact mean wait is 3.2 minutes. Both intervals hold 3.2, which is what a 95% interval should do about nineteen times in twenty.
Check yourself¶
- Predict, then run: halving the target to
relative=0.05should need about four times the replications, because the half-width shrinks with the square root of the count. Change it and compare with the cap.report["met"]tells you whether the cap stopped the run. - Explain it back: why do you set the warm-up from long runs and then apply it to the short day?
Done when¶
- [ ] The script prints three lines and the first says
target met: True. - [ ] You can say what
reliablemeans and what you do when it isFalse. - [ ] You can say why a warm-up needs more replications for the same precision.
If stuck¶
| What happens | Likely cause | Fix |
|---|---|---|
ModuleNotFoundError: No module named 'nagasim' |
The package is not installed in the Python you ran | Run pip install nagasim with the same interpreter, then run the script again. |
ValueError: unknown KPI |
The KPI key is misspelled | Copy the key from the KPI reference. It has the form resource/Server/Time in Queue Avg. |
target met: False and the count equals the cap |
The target needs more than max_replications |
Relax relative, or pass a higher max_replications. Say which you chose when you report the result. |
reliable: False |
The suggested warm-up fell in the second half of the run, so the run is too short to tell | Make the runs longer or add replications, then ask again. Do not use the suggestion as it stands. |
Elements this page uses¶
- Python:
run_until_precision,suggest_warmupandrun_model. - Blocks: CREATE, PROCESS and DISPOSE.
- Distribution: Exponential.
- KPI:
Time in Queue Avgin the resource summary. - Background: Experiments explains MSER-5 in full.
Next: Optimise a decision.