Skip to content

Step 4: Agents

An agent is a small statechart that lives in the model beside the blocks. This step adds four staff members who work, go on a break and return, and the model counts how many are in each state.

from nagasim import build as nb

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="Desks", distribution=nb.Exponential(rate=1.25))
arrive >> serve >> m.dispose()
m.add_schedule(name="afternoon", kind="capacity", points=[[240, 0], [240, 1]])   # closed 4 h, open 4 h
m.add_resource(name="Second", capacity=1, capacity_schedule="afternoon")
m.add_resource_set(name="Desks", members=["Server", "Second"], rule="PreferredOrder")
m.add_entity(name="Watcher")
m.add_level(name="Tank", capacity=100, initial=20,
            thresholds=[{"value": 50, "direction": "rising", "signal": "half full"}])
m.add_flow(name="fill", to="Tank", rate="5")                       # litres a minute in
m.add_flow(name="draw", **{"from": "Tank"}, rate="2")              # and out
watch = m.create(e_type="Watcher", distribution=nb.Constant(value=0.0), max_arrivals=1)
watch >> m.hold(mode="Signal", signal_name="half full") >> m.dispose()
m.add_agent_type(name="Staff", initial_state="Working",
                 states=[{"name": "Working"}, {"name": "OnBreak"}],
                 transitions=[{"from": "Working", "to": "OnBreak", "timeout": "EXPO(120)"},
                              {"from": "OnBreak", "to": "Working", "timeout": "EXPO(15)"}])
m.add_population(name="Team", type="Staff", count=4)

results = m.run(seed=1)
print(results["agent_summary"][["Agent State", "Avg Count", "Entries"]].to_string(index=False))

An agent type lists its states and the transitions between them. A transition fires after a delay drawn from an expression, here EXPO(120) minutes of work and EXPO(15) minutes of break. A population makes four agents of the type, all starting in Working. Nothing in the model reads the staff yet, but they run on the same clock as the queue and the tank.

It prints:

 Agent State  Avg Count  Entries
Team/Working   3.516323       15
Team/OnBreak   0.483677       15

The summary gives the average number of agents in each state and how many times each state was entered. About one in nine of the four staff is on a break at any moment, as the two delays imply. The model now has all four kinds of component, and the next step asks how sure we can be of its numbers.

Next: Replications and scenarios.