Modeladvanced
Timed event graph
Also called timed marked graph.
A Petri net where each place has one input and one output transition and a holding time. With earliest firing, the firing times obey x(k) = A x(k-1) in max-plus algebra.
A timed event graph is a model of a system of repeated events that wait for each other, such as machines in a production line, stages of a pipeline, or actors in a dataflow program. It is a Petri net in which every place has exactly one transition that puts tokens into it and exactly one transition that takes tokens out, and in which each token must stay a fixed holding time in its place before it can be used. So there are no choices, only synchronization: each event happens as soon as all of its input conditions are ready. Under this earliest firing rule, the time of the -th occurrence of each event is the maximum, over its inputs, of an earlier time plus a delay. In max-plus algebra this is a linear recurrence , and the long-run period of the system is the eigenvalue of given by the max-plus eigenvalue theorem. The untimed version is called a marked graph; homogeneous synchronous dataflow graphs and the pipelines of latency-insensitive design are marked graphs.
The machine
Structure. There are transitions (the events) and a set of places (the conditions). Each place has one input transition , one output transition , a number of initial tokens and a holding time . A token that enters can be used by only time units later. Tokens leave a place in the order they entered. Some authors put the delays on the transitions (firing times) instead of on the places; a transition delay can be moved to its output places, so the two forms are equivalent.
Time axis. Event driven. The state is the vector of daters , where is the time of the -th firing of . Firing itself takes no time.
Dynamics. Under the earliest firing rule, each transition fires as soon as every input place holds a token that has stayed its holding time. Then
A place with can be split into a chain of places with one token each, so assume . Let collect the zero-token places and the one-token places: is the largest over the places from to with , and if there is none. Then
Standing assumption. Every circuit of places carries at least one token. Then the graph of has no circuit, its star is finite, and
The initial condition fixes when the initial tokens become available: the token of a one-token place from is available at . The proof is in proofs/correctness.md of this page. The computation of and of the daters is:
TEG-MATRIX(n, places) ▷ place = (j, i, m, tau) with m ∈ {0, 1}- 1
A0 ← n by n matrix of ε; A1 ← n by n matrix of ε - 2
for each place (j, i, m, tau) do - 3
if m = 0 then A0[i][j] ← max(A0[i][j], tau) else A1[i][j] ← max(A1[i][j], tau) - 4
S ← A0* = E ⊕ A0 ⊕ ... ⊕ A0^(n-1) ▷ error if A0^n ≠ ε: a circuit without tokens - 5
A ← S ⊗ A1 - 6
for k ← 1 to K do x(k) ← A ⊗ x(k-1)
Example 1. Two transitions. Place holds 3, place holds 4, place holds 7 and place holds 2; every place has one token. The self-loops say that a transition cannot fire again before its own processing time has passed. All places have one token, so , and
From the daters are:
| 0 | 1 | 2 | 3 | 4 | 5 | |
|---|---|---|---|---|---|---|
| 0 | 7 | 11 | 16 | 20 | 25 | |
| 0 | 4 | 9 | 13 | 18 | 22 |
For example, : the second firing of waits for the token from the first firing of . After the first step, each transition fires 9 time units every 2 firings, so the cycle time is 4.5, the mean of the circuit (holding times over 2 tokens).
Example 2. A production line with one pallet. starts a part on machine 1, starts it on machine 2, releases it. The places are: (machine 1 busy, 1 token, 3), (the part moves on, 0 tokens, 3), (machine 2 busy, 1 token, 4), (0 tokens, 4) and (the pallet returns, 1 token, 0). Here has the arcs (3) and (4), so adds the path of weight 7, and
With , all initial tokens are available at time 0, and the daters are , , . The single pallet makes the line sequential, and the cycle time is .
What it counts
The model predicts the time of every event under the earliest firing rule, from the holding times and the initial condition alone. The main derived quantities are:
- The cycle time , the mean time between two firings of a transition in the long run, and the throughput .
- The critical circuits, the ones that reach the maximum in . They are the bottleneck: shortening a holding time off the critical circuits does not change .
- Sojourn times of tokens in places and the transient before the periodic regime.
The model does not count resource use, costs or probabilities, and it does not decide between alternatives: a system with routing choices or with competition for a resource needs a general Petri net or another discrete event system model.
Relation to other models
- A timed event graph is a Petri net without conflicts: since each place has one output transition, two transitions never compete for a token. The other special class, the state machine, has one input and one output arc per transition and models choice without synchronization.
- Max-plus algebra is its algebra: the firing equations are linear there, as above. Counting the firings up to a time instead of timing the -th firing gives the dual, min-plus linear form (the counter description of Baccelli et al. 1992).
- A homogeneous synchronous dataflow graph, where each actor consumes and produces one token per firing on each arc, is a marked graph; with execution times it is a timed event graph. Its throughput analysis is the cycle time analysis here.
- The critical path method is the one-shot, acyclic case: each activity happens once, and the start times are the longest paths, given by the max-plus star.
- Cyclic job shop scheduling with a fixed order of operations on each machine is a timed event graph. Choosing that order is the hard part, and it is outside the model.
- Timed automata model time with clocks and allow choice; they are more expressive, and their questions are decided by state-space exploration rather than by linear algebra.
Known results in the model
- Max-plus linearity. If every circuit has a token, (Baccelli et al. 1992; proof on this page).
- Liveness and token invariance. The number of tokens on each circuit never changes, since each transition on the circuit takes one token from the circuit and puts one back. A marked graph is live if and only if every circuit holds at least one token (Murata 1989, section on marked graphs).
- Cycle time. If the graph of is strongly connected, every transition has the same cycle time . It is the maximum cycle mean of , and it equals the maximum over the circuits of the net of the sum of the holding times divided by the number of tokens on the circuit. This is the max-plus eigenvalue theorem, and Karp's algorithm computes in polynomial time.
- Periodic regime. After a finite transient, for a cyclicity (Cohen, Dubois, Quadrat and Viot 1985). In Example 1, and the transient is one step.
- Reducible nets. If the graph is not strongly connected, the transitions can have different cycle times, given by the cycle time vector (Heidergott, Olsder and van der Woude 2006, Chapter 3).
Proof: The earliest firing times of a live timed event graph satisfy x(k) = A0* A1 x(k-1)full
A timed event graph has transitions and a set of places. Each place has one input transition , one output transition , initial tokens and a holding time . Tokens leave a place in the order in which they entered it. Under the earliest firing rule, the -th firing of happens as soon as each input place of holds its -th token and that token has stayed its holding time. The initial token of a place with counts as the output of a firing of at a given time .
Notation is that of max-plus algebra, with . Define by
with .
Lemma 1: the firing equations
For , the firing times satisfy
Proof. Take a place from to . Its tokens enter in the order of the firings of : first the initial tokens, then one token for each firing of . So the -th token that takes from comes from the firing number of , where firing number 0 stands for the initial token. That token is available at . The earliest firing rule fires for the -th time at the latest of these availability times over the input places of :
Grouping the places by and by , and keeping only the largest holding time among parallel places, gives the matrix equation.
Lemma 2: the star of an acyclic matrix
If the graph of has no circuit, then (the matrix with all entries ), , and for every the equation has exactly one solution, .
Proof. is the largest weight of a walk of length from to . Such a walk visits nodes, so it repeats one and contains a circuit, which does not exist. So no such walk exists and the entry is .
Existence: , because .
Uniqueness: let . Substituting the equation into itself times gives
Theorem
If the places with no initial token form no circuit, then for every and the earliest firing times are with .
Proof. By Lemma 1, solves with . The zero-token places are exactly the arcs of the graph of , so by hypothesis that graph has no circuit, and Lemma 2 gives .
If the zero-token places form a circuit, no transition on it can ever fire, because each waits for a token from the one before it; the net is not live. The function mp_star detects this case by checking , which by the argument of Lemma 2 holds exactly when the graph of has a circuit.
A place with tokens can be split into a chain of places with one token each, joined by new transitions with holding time 0 on all but one place, so the restriction to loses no generality. The new transitions add entries to the state vector.
Cost
Building and takes one pass over the places. The star uses max-plus matrix products, plus one more for the circuit check, so operations; a Floyd-Warshall style recursion would give . The product takes and each step of the recurrence takes .
Code
Each implementation follows the pseudocode above. A comment @line links a line of code to a line of the pseudocode. Point at a line of the pseudocode to see the matching code. The build runs the native tests of each language.
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/timed-event-graph/. The authors are listed in prov/KGRQ21.jsonl.
"""Timed event graphs as max-plus linear systems.
A timed event graph has n transitions t_0, ..., t_(n-1) and a list of places.
Each place is a tuple (j, i, m, tau): its one input transition is t_j, its one
output transition is t_i, it holds m initial tokens (0 or 1 here) and a token
must stay tau >= 0 time units in it before t_i can use it.
With the earliest firing rule, the time x_i(k) of the k-th firing of t_i is
x_i(k) = max over places (j, i, m, tau) of tau + x_j(k - m).
Split the places by m: A0 holds the zero-token places, A1 the one-token
places. Then x(k) = A0 x(k) (+) A1 x(k-1) in max-plus notation, and if the
zero-token places form no circuit, x(k) = A x(k-1) with A = A0* A1.
The initial token of a one-token place (j, i, 1, tau) is treated as the
output of a firing of t_j at time x_j(0), so it is available at x_j(0) + tau.
Max-plus zero (epsilon) is float("-inf"). All arithmetic is exact for
integer holding times.
"""
EPS = float("-inf")
__all__ = ["EPS", "mp_mul", "mp_matvec", "mp_star", "teg_matrix", "simulate"]
def mp_mul(a, b):
"""Max-plus product of matrices given as lists of rows."""
m = len(b)
p = len(b[0])
return [[max(row[k] + b[k][j] for k in range(m)) for j in range(p)] for row in a]
def mp_matvec(a, x):
"""Max-plus product of a matrix and a vector."""
return [max(aij + xj for aij, xj in zip(row, x)) for row in a]
def mp_star(a):
"""Return a* = E (+) a (+) ... (+) a^(n-1) for a matrix whose graph has no circuit.
Raise ValueError if the graph of a has a circuit, because then the
zero-token places form a loop with no token and the net deadlocks.
"""
n = len(a)
s = [[0 if i == j else EPS for j in range(n)] for i in range(n)]
p = [row[:] for row in s]
for _ in range(n - 1):
p = mp_mul(p, a)
s = [[max(u, v) for u, v in zip(rs, rp)] for rs, rp in zip(s, p)]
p = mp_mul(p, a)
if any(v != EPS for row in p for v in row):
raise ValueError("the zero-token places form a circuit: the net is not live")
return s
def teg_matrix(n, places):
"""Return the n by n max-plus matrix A with x(k) = A x(k - 1)."""
a0 = [[EPS] * n for _ in range(n)] # @line L1
a1 = [[EPS] * n for _ in range(n)] # @line L1
for j, i, m, tau in places: # @line L2
if tau < 0 or m not in (0, 1) or not (0 <= i < n and 0 <= j < n):
raise ValueError("each place needs 0 or 1 token, tau >= 0 and valid transitions")
if m == 0: # @line L3
a0[i][j] = max(a0[i][j], tau) # @line L3
else: # @line L3
a1[i][j] = max(a1[i][j], tau) # @line L3
s = mp_star(a0) # @line L4
return mp_mul(s, a1) # @line L5
def simulate(a, x0, k):
"""Return [x(0), x(1), ..., x(k)] for x(k + 1) = A x(k)."""
xs = [list(x0)]
for _ in range(k): # @line L6
xs.append(mp_matvec(a, xs[-1])) # @line L6
return xs
test_timed_event_graph.py 183 lines
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/timed-event-graph/. The authors are listed in prov/KGRQ21.jsonl.
import random
from collections import deque
from fractions import Fraction
import pytest
from timed_event_graph import EPS, mp_matvec, mp_mul, mp_star, simulate, teg_matrix
# Two transitions; every place holds one token.
TWO = [(0, 0, 1, 3), (1, 1, 1, 4), (1, 0, 1, 7), (0, 1, 1, 2)]
# A production line: t0 = start on machine 1, t1 = start on machine 2,
# t2 = the part leaves. One pallet circulates.
LINE = [
(0, 0, 1, 3), # machine 1 is busy 3 per part
(0, 1, 0, 3), # the part reaches machine 2 after 3
(1, 1, 1, 4), # machine 2 is busy 4 per part
(1, 2, 0, 4), # the part leaves after 4 on machine 2
(2, 0, 1, 0), # the pallet goes back to machine 1 at once
]
def token_game(n, places, x0, k, rng):
"""Fire enabled transitions in a random order; tokens leave places in FIFO order."""
queues = [deque() for _ in places]
for idx, (j, _, m, tau) in enumerate(places):
if m == 1:
queues[idx].append(x0[j] + tau)
ins = [[idx for idx, p in enumerate(places) if p[1] == i] for i in range(n)]
outs = [[idx for idx, p in enumerate(places) if p[0] == i] for i in range(n)]
times = [[] for _ in range(n)]
while True:
ready = [i for i in range(n) if len(times[i]) < k and all(queues[idx] for idx in ins[i])]
if not ready:
break
i = rng.choice(ready)
t = max(queues[idx].popleft() for idx in ins[i])
times[i].append(t)
for idx in outs[i]:
queues[idx].append(t + places[idx][3])
return times
def elementary_circuit_means(a):
"""All circuit means of the graph of a, by brute force over simple paths."""
n = len(a)
means = []
def dfs(start, node, weight, length, seen):
for nxt in range(n):
w = a[nxt][node] # arc node -> nxt
if w == EPS or nxt < start:
continue
if nxt == start:
means.append(Fraction(weight + w) / (length + 1))
elif nxt not in seen:
dfs(start, nxt, weight + w, length + 1, seen | {nxt})
for s in range(n):
dfs(s, s, 0, 0, {s})
return means
def strongly_connected(a):
n = len(a)
for s in range(n):
seen, stack = {s}, [s]
while stack:
u = stack.pop()
for v in range(n):
if a[v][u] != EPS and v not in seen:
seen.add(v)
stack.append(v)
if len(seen) < n:
return False
return True
def random_teg(rng, n):
order = list(range(n))
rng.shuffle(order)
rank = {t: r for r, t in enumerate(order)}
places = [(i, i, 1, rng.randint(0, 5)) for i in range(n)]
for _ in range(rng.randint(n, 3 * n)):
j, i = rng.randrange(n), rng.randrange(n)
tau = rng.randint(0, 9)
if rank[j] < rank[i] and rng.random() < 0.5:
places.append((j, i, 0, tau)) # zero-token places follow the order, so they form no circuit
else:
places.append((j, i, 1, tau))
return places
def test_two_transition_example():
a = teg_matrix(2, TWO)
assert a == [[3, 7], [2, 4]]
xs = simulate(a, [0, 0], 5)
assert xs == [[0, 0], [7, 4], [11, 9], [16, 13], [20, 18], [25, 22]]
# cycle time 4.5 and cyclicity 2: A^4 = 9 + A^2
a2 = mp_mul(a, a)
a4 = mp_mul(a2, a2)
assert a2 == [[9, 11], [6, 9]]
assert a4 == [[v + 9 for v in row] for row in a2]
assert max(elementary_circuit_means(a)) == Fraction(9, 2)
def test_production_line_uses_the_star():
a = teg_matrix(3, LINE)
assert a == [[3, EPS, 0], [6, 4, 3], [10, 8, 7]]
xs = simulate(a, [-3, -4, 0], 3)
assert xs[1] == [0, 3, 7]
assert xs[2] == [7, 10, 14]
assert xs[3] == [14, 17, 21]
assert max(elementary_circuit_means(a)) == 7
def test_star_rejects_a_token_free_circuit():
with pytest.raises(ValueError):
teg_matrix(2, [(0, 1, 0, 1), (1, 0, 0, 1)])
with pytest.raises(ValueError):
teg_matrix(1, [(0, 0, 0, 2)])
def test_star_matches_longest_paths():
rng = random.Random(5)
for _ in range(50):
n = rng.randint(1, 5)
a = [[EPS] * n for _ in range(n)]
for i in range(n):
for j in range(i):
if rng.random() < 0.5:
a[i][j] = rng.randint(-3, 6) # arcs j -> i with j < i: acyclic
s = mp_star(a)
# longest path by dynamic programming in topological order
for src in range(n):
best = [EPS] * n
best[src] = 0
for v in range(src + 1, n):
best[v] = max([best[u] + a[v][u] for u in range(src, v)] + [EPS])
assert [s[v][src] for v in range(n)] == best
def test_recurrence_matches_token_game():
rng = random.Random(2024)
for _ in range(150):
n = rng.randint(1, 5)
places = random_teg(rng, n)
a = teg_matrix(n, places)
x0 = [rng.randint(-5, 5) for _ in range(n)]
k = 12
xs = simulate(a, x0, k)
times = token_game(n, places, x0, k, rng)
for i in range(n):
assert times[i] == [xs[r][i] for r in range(1, k + 1)]
def test_cycle_time_is_the_maximum_cycle_mean():
rng = random.Random(77)
tested = 0
for _ in range(200):
n = rng.randint(1, 4)
places = random_teg(rng, n)
a = teg_matrix(n, places)
if not strongly_connected(a):
continue
tested += 1
lam = max(elementary_circuit_means(a))
x0 = [rng.randint(-5, 5) for _ in range(n)]
x = x0
for _ in range(400):
x = mp_matvec(a, x)
# eventually periodic: x(k + c) = x(k) + c lam for some c
y, found = x, False
for c in range(1, 25):
y = mp_matvec(a, y)
if all(yi - xi == c * lam for xi, yi in zip(x, y)):
found = True
break
assert found
assert tested > 40
References
- François Baccelli, Guy Cohen, Geert Jan Olsder and Jean-Pierre Quadrat, Synchronization and Linearity, An Algebra for Discrete Event Systems, Wiley, 1992, the chapter on graph theory and Petri nets, and the chapters on the max-plus model of event graphs and its periodic regime
- Bernd Heidergott, Geert Jan Olsder and Jacob van der Woude, Max Plus at Work, Princeton University Press, 2006, Chapter 7, Petri Nets (timed event graphs and their max-plus equations); Chapter 3, the cycle-time vector
- Guy Cohen, Didier Dubois, Jean-Pierre Quadrat and Michel Viot, A linear-system-theoretic view of discrete-event processes and its use for performance evaluation in manufacturing, IEEE Transactions on Automatic Control AC-30(3), 210-220, 1985, the max-plus linear model of a manufacturing system and its cycle time
- Tadao Murata, Petri nets, properties, analysis and applications, Proceedings of the IEEE 77(4), 541-580, 1989, the section on marked graphs (liveness and the token count of circuits) and the section on timed nets
About this page
The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof | H2 full proof |
|
| Lean | L0 no Lean part |
|
| Code (python) | I0 not checked yet |
|
| Visual | V0 none |
|
| Writing | W4 complete |
|
Tag KGRQ21 Edit this page History Report an error Markdown