Log inStart free
LESSON 15.01 · 6 STAGES · 2 CONTROLS · 3 CHECKS

Discrete-time vs discrete-event simulation

Two simulators run the same six requests: one ticks a clock, one jumps between events.


EXPLAINER · 15.01 · SIMULATION BAY
01
Stage 1 of 6 · Six requests
Six requests arrive at one server. A waiting room holds 1 more; any other is dropped.
Explain it simply

Each row is one request, r1 to r6. The dot shows when it arrives, and the dashed bar shows how long its work takes. The server works on one request at a time, and one more can wait. Both simulators get the same six requests, one row set each.

Two sets of six rows on one time axis from 0 to 16 minutes. Each row has an arrival dot and a dashed bar for its work.

arrival and workclockwaitdroppeddeparture
Toy: 6 requests, 1 server, a waiting room for 1
HOVER OR TAP A SHAPE TO SEE ITS NUMBERS
1 min
How far the top clock moves per tick.
Departure first
Which of two events at the same time goes first.
STAGE 1 / 6
MATH · STAGE 1HOVER A SYMBOL OR A SHAPE
ai, si\sy{a}{data}{a_i},\ \sy{s}{data}{s_i}
a1=1.5, s1=1a_1 = 1.5,\ s_1 = 1

Request i arrives at time a and needs s minutes of work.

Show the code ▾Hide the code ▴ · Try it yourself · Python 3
# Discrete-time vs discrete-event simulation of one server with a waiting room for 1 request.
import heapq

REQUESTS = [(1.5, 1), (2, 1.5), (3.5, 2), (6.5, 3), (9, 1.5), (9.5, 3)]  # (arrival, work) in minutes
DT = 1  # time step of the discrete-time simulation, minutes
DEP_FIRST = True  # tie rule: a departure before an arrival at the same time
ROOM = 1  # places in the waiting room


def summary(start):
    waits = [s - a for (a, w), s in zip(REQUESTS, start) if s is not None]
    return len(waits), len(start) - len(waits), sum(waits) / len(waits)


def discrete_time():
    start = [None] * len(REQUESTS)
    state = {"busy": None, "done_at": 0, "seen": 0}
    room, ticks = [], 0

    def leave(t):  # a departure is due when the work is done
        if state["busy"] is not None and state["done_at"] <= t:
            state["busy"] = None
            if room:
                i = room.pop(0)
                state["busy"], state["done_at"], start[i] = i, t + REQUESTS[i][1], t

    def come(t):  # every request that arrived since the last tick joins now
        while state["seen"] < len(REQUESTS) and REQUESTS[state["seen"]][0] <= t:
            i = state["seen"]
            if state["busy"] is None:
                state["busy"], state["done_at"], start[i] = i, t + REQUESTS[i][1], t
            elif len(room) < ROOM:
                room.append(i)
            state["seen"] += 1  # a request that finds the room full is dropped

    while state["seen"] < len(REQUESTS) or state["busy"] is not None or room:
        ticks += 1
        t = ticks * DT  # the clock moves one step
        for check in (leave, come) if DEP_FIRST else (come, leave):
            check(t)
    return start, ticks


def discrete_event():
    start = [None] * len(REQUESTS)
    rank = {"dep": 0, "arr": 1} if DEP_FIRST else {"arr": 0, "dep": 1}
    queue = []  # the event queue: (time, tie rank, request, kind), earliest first

    def schedule(t, kind, i):
        heapq.heappush(queue, (t, rank[kind], i, kind))

    schedule(REQUESTS[0][0], "arr", 0)
    busy, room, events = None, [], 0
    while queue:
        t, _, i, kind = heapq.heappop(queue)  # the clock jumps to the earliest event
        events += 1
        if kind == "arr":
            if i + 1 < len(REQUESTS):
                schedule(REQUESTS[i + 1][0], "arr", i + 1)
            if busy is None:
                busy, start[i] = i, t
                schedule(t + REQUESTS[i][1], "dep", i)
            elif len(room) < ROOM:
                room.append(i)
        else:
            busy = None
            if room:
                busy = room.pop(0)
                start[busy] = t
                schedule(t + REQUESTS[busy][1], "dep", busy)
    return start, events


for name, (start, updates), unit in (("discrete time", discrete_time(), "ticks"), ("discrete event", discrete_event(), "events")):
    served, dropped, wait = summary(start)
    print(f"{name}: served {served}, dropped {dropped}, mean wait {wait:.2f} min, {updates} {unit}")
# discrete time: served 6, dropped 0, mean wait 1.17 min, 15 ticks
# discrete event: served 6, dropped 0, mean wait 0.50 min, 12 events

Copy it into a file and run it. It follows the time step and the tie rule.

Common mix-ups

MYTHThe order of simultaneous events does not matter.

FIXAt 9.5 min, arrival first finds the room full, so request 6 is dropped. Departure first serves all 6 requests.

MYTHA bigger time step only rounds the times a little.

FIXWith a 2 min step, requests 5 and 6 meet at the same tick. The room overflows, and request 6 is dropped.

Predict, then reveal
0 / 3 REVEALED
Q01Go from a 1 min step to a 0.5 min step. How do the ticks change?
1 · GUESS2 · TRY IT IN THE DIAGRAM
Q02With arrival first, how many requests does the event simulation serve?
1 · GUESS2 · TRY IT IN THE DIAGRAM
Q03With a 2 min step, is the mean wait higher or lower than with events?
1 · GUESS2 · TRY IT IN THE DIAGRAM

Takeaways

  1. A discrete-time simulation checks every tick, even when nothing happens.
  2. A discrete-event simulation jumps its clock to the next event in the queue.
  3. Simultaneous events need a fixed tie rule; the order can change results.