How do I cost a model and find the cost drivers?¶
In short
- You build: a script that prices a call centre's day and splits the total into agents busy, agents idle and callers waiting. It then ranks the inputs by how far they move the total.
- You need: Python 3.11 or later,
pip install nagasim, and the sensitivity tutorial.- Time: about ten minutes.
A total cost is only useful when you know what it is made of. By the end you put a cost on a resource and on every caller in the system, and read the split. Then you use a tornado to see which input is the main cost driver.
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-cost.py. It is the call centre of the optimisation tutorial with six agents. Read the two cost lines with care, because they use different time units.
from nagasim import build as nb, optimize
m = nb.Model(time_limit=480, warmup=0) # one 8-hour day, in minutes
# An agent costs 24 per hour while busy and 18 per hour while idle. These are quoted per hour
# because `cost_time_unit` says so.
m.add_resource(name="Agents", capacity=6, cost_busy=24, cost_idle=18, cost_time_unit="hours")
# A caller in the system costs 0.5 per MINUTE: `holding_cost` is per model time unit, which is
# minutes here, whatever `cost_time_unit` says.
m.add_entity(name="Call", holding_cost=0.5)
arrive = m.create(e_type="Call", distribution=nb.Exponential(rate=2.0)) # two calls a minute
serve = m.process(resource_name="Agents", distribution=nb.Exponential(rate=0.5)) # 2 minutes each
arrive >> serve >> m.dispose()
results = m.run(replications=10, seed=1)
busy = results.kpi("resource/Agents/Busy Cost")
idle = results.kpi("resource/Agents/Idle Cost")
holding = results.kpi("system/Total Entity Holding Cost")
total = results.kpi("system/Total Cost")
for name, part in (("agents busy", busy), ("agents idle", idle), ("callers waiting and served", holding)):
print(f"{name:<27}{part:8.0f} {100 * part / total:3.0f}%")
print(f"{'total per day':<27}{total:8.0f}")
# Check the holding line by hand: 0.5 a minute for each caller in the system, for 480 minutes.
wip = results.kpi("system/Avg WIP")
print(f"by hand: 0.5 x {wip:.2f} callers x 480 minutes = {0.5 * wip * 480:.0f}")
# Which input moves the total most? One at a time, each across a plausible range.
factors = [
optimize.Factor(path="resources.Agents.capacity", kind="integer", low=5, high=7, name="agents"),
optimize.Factor(path=f"blocks.{arrive.name}.params.dist_params.rate", low=1.6, high=2.0, name="arrival rate"),
optimize.Factor(path="resources.Agents.cost_busy", low=20, high=28, name="busy cost"),
optimize.Factor(path=f"blocks.{serve.name}.params.dist_params.rate", low=0.45, high=0.55, name="service rate"),
]
print(optimize.tornado(m.to_dict(), factors, "system/Total Cost", replications=5, seed=1)
[["Factor", "KPI at low", "KPI at high", "Swing"]].round(0).to_string(index=False))
- Run it.
python goal-cost.py
You should see this. The script seeds itself, so the numbers are the same on your machine.
agents busy 769 35%
agents idle 287 13%
callers waiting and served 1114 51%
total per day 2170
by hand: 0.5 x 4.64 callers x 480 minutes = 1114
Factor KPI at low KPI at high Swing
arrival rate 1813.0 2165.0 351.0
service rate 2160.0 1835.0 325.0
busy cost 1852.0 2079.0 227.0
agents 2035.0 2052.0 17.0
-
Know the two units.
cost_busyandcost_idleare quoted percost_time_unit, which the script sets to hours. The holding cost of an entity is per model time unit, which is minutes here. It does not followcost_time_unit. A holding cost of 0.5 therefore means 0.5 per minute, and 30 per hour. -
Read the split. A day costs 2170. Agents working cost 769, agents waiting for a call cost 287, and callers in the system cost 1114, which is more than half. The waiting callers cost more than the agents do.
-
Read the hand check. The holding cost is the price per minute times the average number of callers in the system times the 480 minutes of the day. The average is 4.64 callers, and 0.5 times 4.64 times 480 is 1114, the same figure. If your own check does not match, a unit is wrong.
-
Read the tornado. Each row moves one input from its low to its high value with the others in the middle, sorted by the swing in total cost. The arrival rate moves the total most, by 351. The service rate follows with 325, then the busy cost with 227.
-
Read the last row. The number of agents moves the total by only 17. Another agent adds idle cost but removes waiting, and the two nearly cancel. The number of agents is a poor lever here, but the arrival and service rates are strong levers.
Check yourself¶
- Predict, then run: if you set
holding_costto 0.25, will the callers' share of the day stay above half? Change it and check. - Explain it back: why does adding an agent change the total so little when it changes the wait so much?
Done when¶
- [ ] The script prints a three-line split, a total, a hand check and a four-row tornado.
- [ ] You can say which unit
holding_costis in, and which unitcost_busyis in. - [ ] You can name the main cost driver and say what it tells you to measure better.
If stuck¶
| What happens | Likely cause | Fix |
|---|---|---|
| The holding cost is 60 times too large or too small | holding_cost was read as per hour, or cost_busy as per minute |
Holding is per model time unit. Resource costs are per cost_time_unit. Run the hand check. |
| The total has no holding line | The entity type has no holding_cost, or the entities have a different e_type |
Give the CREATE block e_type="Call" for the entity type that has the cost. |
unknown section 'entities' in the tornado |
Entity settings are not override paths | Vary a resource, a block or a variable instead. |
| The ranking changes between runs | Five replications leave noise in the swings | Raise replications, and compare swings only when they differ clearly. |
Elements this page uses¶
- Python:
Modelwithadd_resourceandadd_entity,Results, andFactorandtornadofromnagasim.optimize. - Blocks: CREATE, PROCESS and DISPOSE.
- KPIs:
Busy CostandIdle Costin the resource summary, andTotal Entity Holding CostandTotal Costin the system frame. - Background: Experiments covers searches over costs, and the model file format lists every cost field.
Back to the learning path.