Skip to content

Which decision is best? Optimise it

In short

  • You build: a script that searches for the number of call-centre agents with the lowest daily cost.
  • You need: Python 3.11 or later, pip install nagasim, and the replications tutorial.
  • Time: about ten minutes.

An objective such as cost has a best value over your decision variables, and the engine can search for it. By the end you describe one decision as a factor, give the search a budget of replications, and read which choice won and what the search tried.

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

  1. Save this script as goal-optimize.py. It models a call centre where two calls arrive every minute and an agent takes two minutes per call. Agents cost 20 an hour whether busy or idle, and every caller in the system costs 0.5 a minute. More agents cost more wages and fewer waiting callers, so the total cost has a lowest point.
from nagasim import build as nb, optimize

m = nb.Model(time_limit=480, warmup=0)         # one 8-hour day, in minutes
m.add_resource(name="Agents", capacity=5, cost_busy=20, cost_idle=20, cost_time_unit="hours")
m.add_entity(name="Call", holding_cost=0.5)    # every caller in the system costs 0.5 a minute
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()

staff = optimize.Factor(path="resources.Agents.capacity", kind="integer", low=5, high=12, name="agents")
found = optimize.optimize(m.to_dict(), [staff], response="system/Total Cost", goal="min",
                          budget=80, replications_per_point=4, seed=1)

print(f"best: {found.best['agents']} agents, mean cost {found.best_mean:.0f}")
tried = found.history[["agents", "Mean", "N"]].drop_duplicates("agents").sort_values("agents")
tried["Mean"] = tried["Mean"].round().astype(int)
print(tried.to_string(index=False))
  1. Run it.
python goal-optimize.py

You should see this. The script seeds itself, so the numbers are the same on your machine.

best: 6 agents, mean cost 2056
 agents  Mean  N
      5  2498  4
      6  2093  4
      7  2126  4
      8  2254  4
      9  2405  4
     10  2562  4
     11  2721  4
     12  2880  4
  1. Read the first line. It names the winner and its mean cost. The search confirms its winner with ranking and selection, a final comparison of the best few points. That step gives the winner more replications than the table shows, so its mean differs a little from the table row.

  2. Read the table. Each row is one number of agents the search tried, with the mean cost over N replications. Cost falls as agents are added up to the winner and rises after it. The offered load is four agents' work, so five agents are busy 80% of the time. Callers queue, and holding cost dominates.

  3. Look at how the decision was described. Factor names the override path of the value to change, its kind (integer), and its low and high bound. Every point runs on the same seeds, so the difference between two points is the effect of the decision and not of luck. See common random numbers.

  4. Treat the answer as advice for this model. The search never changes the model file. To keep the result, add it as a scenario or set the value yourself.

Check yourself

  • Predict, then run: if a caller in the system cost 1.0 a minute instead of 0.5, would the best number of agents be lower, the same or higher? Change holding_cost and compare.
  • Explain it back: why can the search not simply choose the largest number of agents?

Done when

  • [ ] The script prints a best number of agents and a table with one row per number tried.
  • [ ] You can say what budget limits and why the winner has more replications than the table shows.
  • [ ] You can say what a search does not change in your model.

If stuck

What happens Likely cause Fix
ModelValidationError: override path 'resources.Agent.capacity': no resources entry named 'Agent' The factor's path names something the model does not have Copy the name from the model. Here the resource is Agents.
ValueError: unknown response KPI: 'system/Nope' The response key is misspelled Copy the key from the KPI reference.
ValueError: factor 'a': high 1.0 is below low 3.0 The bounds are the wrong way round Swap low and high.
The table has fewer rows than the range The budget ran out before every value was tried, or the search did not need them Raise budget to try more points. A search is not an exhaustive sweep.

Elements this page uses

Next: Sensitivity and design of experiments.