Algorithmcore
Supremal controllable sublanguage
Also called supremal controllable sublanguage computation.
Computes the largest controllable sublanguage of a regular specification by a fixed point on the product automaton, in time polynomial in the number of product states.
- Time
- O(n^2 q) worst
- Space
- O(n q) extra
- Where
- n = the number of states of the product of H and G; q = the number of events
- Model
- Word RAM
- Properties
- deterministicexactoffline
- Evidence
- Proof sketchCode in 1 languages
- Origin
- W. Murray Wonham, Peter J. Ramadge, 1987
The supremal controllable sublanguage of a specification is the largest part of that a supervisor can enforce on a plant , when it may disable only the controllable events. It always exists, because a union of controllable languages is controllable. When and the specification are finite automata, it is computed by a fixed point on their product: delete every product state where an uncontrollable event of the plant leads out of the remaining states, delete the states that can no longer reach a marked state, and repeat. What is left is a trim automaton. It is at the same time the least restrictive nonblocking supervisor for . The run takes time for product states and events. Unlike the plain product construction, which only intersects two languages, this algorithm removes states for a reason that looks ahead: a state is bad if an event that nobody can stop leads to a bad state.
In plain words
You have a machine and a list of rules. You can stop some of the machine's moves, but not all of them. Run the machine and the rule book side by side, so that each joint position says where both are. Some positions are traps: from there, a move you cannot stop breaks a rule. Delete those positions. Then delete every position from which you can no longer finish a job. Deleting positions can make new traps, because a move you cannot stop may now lead into a deleted position. So repeat both steps until nothing changes. The positions that are left, with their moves, are the best controller: it stops a move only when it must.
The problem
The input is a plant , a specification automaton over the same alphabet, and the set of uncontrollable events. Both automata are deterministic and finite. Let . The output is a trim automaton with
where is the union of all sublanguages that are controllable, . The supervisor is itself, run in parallel with . After a string , it disables the controllable events that allows but does not.
The algorithm
is the accessible part of . Its states are pairs , and a state is marked when both parts are marked. is the set of states that are still alive.
SUP-C(G, H, Σuc)- 1
A ← accessible part of H × G; S ← the states of A - 2
repeat - 3
changed ← false - 4
for each state x = (h, g) in S do - 5
if some σ in Σuc has δ_G(g, σ) defined and δ_A(x, σ) undefined or not in S then remove x from S; changed ← true - 6
S' ← the states of S that are accessible and coaccessible inside S; if S' ≠ S then S ← S'; changed ← true - 7
until not changed - 8
return A restricted to S
Line L5 is the controllability test. It looks at the events of the plant at , not at the events of . If the plant can execute an uncontrollable and the specification does not allow it ( is undefined), or allows it only into a dead state, then the state is a trap and goes. Line L6 is the nonblocking test. It is a forward search from the initial state and a backward search from the marked states, both inside . The two tests feed each other. Removing a trap can cut the only path to a marked state, and removing a blocking state can make a new trap. So the loop runs until a round changes nothing. The result is the largest fixed point of the operator "remove traps, then trim", in the sense of Wonham and Ramadge (1987).
Example
The plant has states 0 to 4 and the transitions
with and the marked state 0. The specification is the same automaton without the transition, with all states marked: "the event must never occur". The product (line L1) has the 4 states , , and and 5 transitions; state 4 of is not reached, because has no . Only is marked.
| round | removed at L5 | removed at L6 | after the round |
|---|---|---|---|
| 1 | : can do , cannot | : it cannot reach any more | , |
| 2 | : leads to the removed | none | |
| 3 | none | none | , stop |
The result is the state with its self-loop , so . The reason: after , the uncontrollable may take the plant to state 2. From there, the only way back to the marked state is and then , which is forbidden. So the supervisor must never enable . The specification itself, , is not controllable, because and but .
A second example is the buffer of supervisory control theory. There the product has 8 states and 12 transitions, round 1 removes the two states where the buffer is full and machine 1 works, and the result has 6 states and 8 transitions. The tests of the implementation check both examples.
Correctness
Let be the set of states of that strings of reach. The invariant is : no state of is ever removed. A state of passes line L5, because is controllable, so each uncontrollable event of leads to another state of . It passes line L6, because the strings of give paths inside from the initial state and to a marked state. When the loop stops, is trim and no state fails line L5, so is a controllable sublanguage of , and it holds by the invariant. The two inclusions give equality. The full proof is in the correctness proof.
Proof: The fixed point on the product automaton gives the supremal controllable sublanguagefull
Setting. The plant and the specification automaton are deterministic automata over the alphabet , where is the set of uncontrollable events. Let . A language is controllable if , where is the prefix closure of . Line L1 builds , the accessible part of . A state of is a pair , and a string is defined in if and only if it is defined in both and . So and . For we write for the state of that reaches. Its second part is the state of that reaches. We write for the set states of the program, and for the restriction of to .
Lemma 1 (union). A union of controllable languages is controllable. So , the union of all controllable sublanguages of , is controllable and is the largest one.
Proof. Let with each controllable. The prefix closure commutes with unions, so . Take and with . Then for some , so .
Let . This is well defined, because .
Lemma 2 (invariant). At every point of the run, .
Proof. At the start is the full state set of , so the claim holds. We show that no removal takes away a state of . Assume just before a removal.
Line L5 removes if some is defined at in and is undefined or not in . Assume , so for some . Then , and by Lemma 1 . So , is defined, and . This is a contradiction, so .
Line L6 keeps the states of that are accessible and coaccessible inside . Let with . Each prefix of is in , so , and the path of from the initial state to stays in . So is accessible in . There is with . In the same way the path of stays in , and it ends at , which is marked in because . So is coaccessible in , and line L6 keeps it.
Lemma 3 (the fixed point). Let be the set when the loop stops at line L7, and let . Then , , and is controllable.
Proof. If is empty, , and all three claims hold. Otherwise, in the last round line L5 removed nothing, so every state of passed the test of line L5 with this same set . Line L6 also returned , so is trim. . Each string of reaches a state of , from which a marked state of can be reached inside , so . The other inclusion is clear, so . Now let and with . The state is in , and is defined at in . Because passed the test of line L5, . So .
Theorem (correctness). The automaton that line L8 returns is trim, its marked language is , and its language is .
Proof. By Lemma 3, is a controllable sublanguage of , so . Conversely, let . By Lemma 2, every prefix of reaches a state of , so . The state is marked in , because , and it is in , so . So , and by Lemma 3 .
Theorem (termination and cost). Let be the number of states of and . The loop of lines L2 to L7 runs at most rounds, and the run takes dict operations.
Proof. Line L5 and line L6 only remove states. A round that does not stop at line L7 sets changed, so it removes at least one state. So at most rounds change , and the next round stops. Line L1 does work, because each state of has at most outgoing transitions. In one round, line L5 looks at each transition of from the part of each state of , which is work, and the two searches of line L6 also take . So the total is .
Complexity
The model is the word RAM with hashing, and is the number of states of the product (at most ), . Each state has at most outgoing transitions.
- Line L1 is a search of the product: .
- One round: line L5 looks at each event of at the part of each state of , so ; line L6 is two searches, .
- Each round except the last removes at least one state, so there are at most rounds.
The total is time and space. The bound is in the size of the product, not of and alone. In terms of the inputs it is . This page derives the bound for the program above. The cost of one round can drop with a worklist that, when a state goes, re-tests only its uncontrollable predecessors, but the alternation of the two tests can still need many rounds.
Properties
- Exact. The result is the supremal controllable sublanguage, not an approximation.
- Least restrictive. Every other supervisor that keeps the closed loop in and is nonblocking has a marked language inside .
- Regular output. The result is given by a subautomaton of , so is regular when is.
- Possibly empty. If the initial state goes, , and no nonblocking supervisor exists for .
- Marking supervisor. also marks strings. With a supervisor that only disables events, the marked closed-loop language is , and it equals when is -closed. This holds when is -closed, for example when marks all its states, as in the example above. If marks only some states, use as a marking supervisor.
Variants and improvements
- Prefix-closed specifications. If only safety matters, drop line L6 except the accessibility part. Then one backward search over uncontrollable transitions removes all traps in time, as for the safety part of a game in reactive synthesis.
- Partial observation. With unobservable events, the supremal controllable and normal sublanguage replaces it. There is no supremal observable sublanguage in general.
- Modular and decentralized control. One supervisor per specification avoids the product of all specifications. The modules must then be checked for conflicts, that is, for blocking of the joint closed loop.
- Symbolic computation. For large plants, the states are encoded with binary decision diagrams, and the fixed point is computed on sets of states, as in symbolic model checking.
- Petri nets. For nets, supervision based on place invariants adds control places instead of building the reachability graph.
When to use it
Use it when the plant and the specification are finite automata of moderate size, and you want the least restrictive nonblocking supervisor. The product can be large: it is the product of all component machines and all specifications. If the product does not fit in memory, use modular or symbolic methods. If some events are unobservable, compute the supremal controllable and normal sublanguage instead. If the specification is a temporal logic formula, the problem is one of reactive synthesis.
Pitfalls
- Testing the events of instead of at line L5. The test "some uncontrollable is defined at in and leads to a removed state" misses the main case, where can do but forbids it, so is not defined in at all. Line L5 must iterate over the events of at .
- Stopping after one round. One pass of line L5 and line L6 is not enough. In the example, round 1 leaves , from which leads to a removed state. Line L7 must repeat until a round changes nothing. A test of the implementation checks exactly this case.
- Trimming without the controllability test, or the reverse. A trim product of and is nonblocking, but it may not be controllable. A controllable product may block. Both lines L5 and L6 are needed in each round.
- Marking the product by only. A state is marked only if both and mark it, at line L1. With the marking of alone, the target language is , and the plant may be stopped in the middle of a task.
Implementations
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/supremal-controllable-sublanguage/. The authors are listed in prov/1CMXJG.jsonl.
"""The supremal controllable sublanguage of a regular specification.
An automaton is a dict with the keys "init" (the initial state), "delta"
(a dict that maps (state, event) to the next state, so the automaton is
deterministic and partial) and "marked" (a set of states). The plant is G,
the specification is H, and K = Lm(H) & Lm(G) is the target language.
supremal_controllable(g, h, uncontrollable) returns a trim automaton A with
Lm(A) = sup C(K) and L(A) = prefix(sup C(K)), where sup C is taken with
respect to L(G) and the set of uncontrollable events. The states of A are
pairs (h, g) of the product H x G. If sup C(K) is empty, A has no states and
its "init" is None.
Cost, with N = |Q_H| |Q_G| product states and an alphabet of size E: the
product takes O(N E) dict operations, one round of the loop takes O(N E),
and there are at most N + 1 rounds, so O(N^2 E) in total.
"""
from collections import deque
from typing import Hashable, Iterable
Automaton = dict
def out_edges(delta: dict) -> dict:
"""Map each state to a dict {event: next state}."""
out: dict = {}
for (q, e), r in delta.items():
out.setdefault(q, {})[e] = r
return out
def product(h: Automaton, g: Automaton) -> Automaton:
"""The accessible part of H x G, synchronized on every event."""
eh, eg = out_edges(h["delta"]), out_edges(g["delta"])
init = (h["init"], g["init"])
states = {init}
delta = {}
queue = deque([init])
while queue:
x = queue.popleft()
for e, g2 in eg.get(x[1], {}).items():
h2 = eh.get(x[0], {}).get(e)
if h2 is None:
continue
y = (h2, g2)
delta[(x, e)] = y
if y not in states:
states.add(y)
queue.append(y)
marked = {x for x in states if x[0] in h["marked"] and x[1] in g["marked"]}
return {"init": init, "states": states, "delta": delta, "marked": marked}
def trim(states: set, out: dict, init: Hashable, marked: set) -> set:
"""The states of `states` that are accessible from init and coaccessible to a marked state, inside `states`."""
if init not in states:
return set()
acc = {init}
stack = [init]
while stack:
x = stack.pop()
for y in out.get(x, {}).values():
if y in states and y not in acc:
acc.add(y)
stack.append(y)
back: dict = {}
for x in acc:
for y in out.get(x, {}).values():
if y in acc:
back.setdefault(y, []).append(x)
co = {x for x in acc if x in marked}
stack = list(co)
while stack:
y = stack.pop()
for x in back.get(y, []):
if x not in co:
co.add(x)
stack.append(x)
return co
def restrict(a: Automaton, states: set) -> Automaton:
"""The sub-automaton of a on the given states."""
delta = {(x, e): y for (x, e), y in a["delta"].items() if x in states and y in states}
init = a["init"] if a["init"] in states else None
return {"init": init, "states": set(states), "delta": delta, "marked": a["marked"] & states}
def supremal_controllable(g: Automaton, h: Automaton, uncontrollable: Iterable) -> Automaton:
"""A trim automaton for sup C(Lm(H) & Lm(G)) with respect to L(G) and the uncontrollable events."""
unc = set(uncontrollable)
a = product(h, g) # @line L1
out = out_edges(a["delta"])
plant = out_edges(g["delta"])
states = set(a["states"])
while True: # @line L2
changed = False # @line L3
for x in list(states): # @line L4
for e in plant.get(x[1], {}):
if e in unc and out.get(x, {}).get(e) not in states: # @line L5
states.discard(x)
changed = True
break
kept = trim(states, out, a["init"], a["marked"]) # @line L6
if kept != states:
states = kept
changed = True
if not changed: # @line L7
break
return restrict(a, states) # @line L8
def disabled_events(sup: Automaton, g: Automaton) -> dict:
"""For each state (h, g) of sup, the events that G allows at g and the supervisor disables."""
plant = out_edges(g["delta"])
out = out_edges(sup["delta"])
return {x: {e for e in plant.get(x[1], {}) if e not in out.get(x, {})} for x in sup["states"]}
test_supremal_controllable_sublanguage.py 229 lines
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/supremal-controllable-sublanguage/. The authors are listed in prov/1CMXJG.jsonl.
import random
from supremal_controllable_sublanguage import disabled_events, product, supremal_controllable
def step(a, q, e):
return a["delta"].get((q, e))
def strings_of(a, events, length):
"""All strings s with |s| <= length in L(a), each with the state it reaches."""
if a["init"] is None:
return []
out = [((), a["init"])]
frontier = [((), a["init"])]
for _ in range(length):
nxt = []
for s, q in frontier:
for e in events:
r = step(a, q, e)
if r is not None:
nxt.append((s + (e,), r))
out.extend(nxt)
frontier = nxt
return out
def run(a, s):
q = a["init"]
for e in s:
if q is None:
return None
q = step(a, q, e)
return q
def reach(states, succ, start):
seen = {start} if start in states else set()
stack = list(seen)
while stack:
x = stack.pop()
for y in succ(x):
if y in states and y not in seen:
seen.add(y)
stack.append(y)
return seen
def brute_sup_states(g, h, unc, events):
"""The union of all sets X of product states whose restriction is trim and has no bad state."""
a = product(h, g)
order = sorted(a["states"], key=repr)
succ = {x: [a["delta"][(x, e)] for e in events if (x, e) in a["delta"]] for x in order}
pred = {x: [] for x in order}
for x in order:
for y in succ[x]:
pred[y].append(x)
best = set()
for mask in range(1, 1 << len(order)):
xs = {order[i] for i in range(len(order)) if mask >> i & 1}
if a["init"] not in xs:
continue
if reach(xs, lambda x: succ[x], a["init"]) != xs:
continue
co = set()
for m in xs & a["marked"]:
co |= reach(xs, lambda x: pred[x], m)
if co != xs:
continue
ok = True
for x in xs:
for e in unc:
if (x[1], e) in g["delta"] and a["delta"].get((x, e)) not in xs:
ok = False
if ok:
best |= xs
return best
def random_automaton(rng, n, events, p=0.55, pm=0.5):
delta = {(q, e): rng.randrange(n) for q in range(n) for e in events if rng.random() < p}
marked = {q for q in range(n) if rng.random() < pm} or {rng.randrange(n)}
return {"init": 0, "delta": delta, "marked": marked}
def check_string_level(g, h, unc, sup, events, length):
"""L(sup) is in L(G), Lm(sup) is in Lm(H) & Lm(G), and L(sup) is controllable, on all strings up to `length`."""
for s, q in strings_of(sup, events, length):
gq, hq = run(g, s), run(h, s)
assert gq is not None and hq is not None
if q in sup["marked"]:
assert gq in g["marked"] and hq in h["marked"]
if len(s) < length:
for e in unc:
if step(g, gq, e) is not None:
assert step(sup, q, e) is not None
def nonblocking(sup):
if sup["init"] is None:
return True
succ = {}
for (x, _), y in sup["delta"].items():
succ.setdefault(x, []).append(y)
for x in sup["states"]:
seen = reach(sup["states"], lambda z: succ.get(z, []), x)
if not seen & sup["marked"]:
return False
return True
def factory():
d = {}
for m1 in "IW":
for m2 in "IW":
s = m1 + m2
if m1 == "I":
d[(s, "a1")] = "W" + m2
else:
d[(s, "b1")] = "I" + m2
if m2 == "I":
d[(s, "a2")] = m1 + "W"
else:
d[(s, "b2")] = m1 + "I"
g = {"init": "II", "delta": d, "marked": {"II"}}
h = {"init": 0, "delta": {(0, "b1"): 1, (1, "a2"): 0, (0, "a1"): 0, (1, "a1"): 1, (0, "b2"): 0, (1, "b2"): 1}, "marked": {0}}
return g, h
def three_round():
gd = {(0, "a"): 1, (1, "u"): 2, (2, "b"): 3, (3, "v"): 4, (4, "d"): 0, (0, "e"): 0, (1, "c"): 0}
g = {"init": 0, "delta": gd, "marked": {0}}
h = {"init": 0, "delta": {k: v for k, v in gd.items() if k[1] != "v"}, "marked": {0, 1, 2, 3, 4}}
return g, h
def test_page_example_three_rounds():
g, h = three_round()
sup = supremal_controllable(g, h, {"u", "v"})
assert sup["states"] == {(0, 0)}
assert sup["delta"] == {((0, 0), "e"): (0, 0)}
marked = [s for s, q in strings_of(sup, "abcdeuv", 8) if q in sup["marked"]]
assert sorted(marked) == [("e",) * k for k in range(9)]
def test_one_round_without_retrim_is_not_enough():
g, h = three_round()
a = product(h, g)
keep = {x for x in a["states"] if x != (3, 3) and x != (2, 2)}
one_round = {"init": a["init"], "states": keep, "marked": a["marked"] & keep,
"delta": {(x, e): y for (x, e), y in a["delta"].items() if x in keep and y in keep}}
s = ("a",)
assert run(one_round, s) is not None and step(g, run(g, s), "u") is not None
assert step(one_round, run(one_round, s), "u") is None
def test_factory_buffer():
g, h = factory()
sup = supremal_controllable(g, h, {"b1", "b2"})
assert len(product(h, g)["states"]) == 8
assert sup["states"] == {(0, "II"), (0, "IW"), (0, "WI"), (0, "WW"), (1, "II"), (1, "IW")}
assert len(sup["delta"]) == 8
dis = disabled_events(sup, g)
assert dis[(1, "II")] == {"a1"} and dis[(1, "IW")] == {"a1"}
assert dis[(0, "II")] == {"a2"} and dis[(0, "WI")] == {"a2"}
assert nonblocking(sup)
check_string_level(g, h, {"b1", "b2"}, sup, ["a1", "b1", "a2", "b2"], 8)
def test_empty_result():
g = {"init": 0, "delta": {(0, "u"): 1, (1, "a"): 0}, "marked": {0, 1}}
h = {"init": 0, "delta": {(0, "a"): 0}, "marked": {0}}
sup = supremal_controllable(g, h, {"u"})
assert sup["init"] is None and sup["states"] == set() and sup["delta"] == {}
def test_all_controllable_gives_trim_product():
rng = random.Random(7)
for _ in range(100):
g = random_automaton(rng, 3, "abc")
h = random_automaton(rng, 2, "abc")
sup = supremal_controllable(g, h, set())
a = product(h, g)
assert sup["states"] == brute_sup_states(g, h, set(), "abc")
assert sup["states"] <= a["states"]
def test_random_against_subset_brute_force():
rng = random.Random(2024)
events = ["a", "b", "c", "u"]
unc = {"u"}
strict = 0
for _ in range(1500):
g = random_automaton(rng, 3, events, p=0.6, pm=0.5)
h = random_automaton(rng, 3, events, p=0.8, pm=0.6)
sup = supremal_controllable(g, h, unc)
assert sup["states"] == brute_sup_states(g, h, unc, events)
assert nonblocking(sup)
strict += 0 < len(sup["states"]) < len(product(h, g)["states"])
assert strict >= 100
def test_random_two_uncontrollable_events():
rng = random.Random(31)
events = ["a", "b", "u", "v"]
unc = {"u", "v"}
for _ in range(500):
g = random_automaton(rng, 3, events)
h = random_automaton(rng, 2, events)
sup = supremal_controllable(g, h, unc)
assert sup["states"] == brute_sup_states(g, h, unc, events)
assert nonblocking(sup)
def test_random_strings_up_to_length_8():
rng = random.Random(99)
events = ["a", "b", "u"]
unc = {"u"}
nonempty = 0
for _ in range(60):
g = random_automaton(rng, 3, events, p=0.5)
h = random_automaton(rng, 3, events, p=0.6)
sup = supremal_controllable(g, h, unc)
check_string_level(g, h, unc, sup, events, 8)
nonempty += sup["init"] is not None
assert nonempty >= 10
History
Ramadge and Wonham introduced controllable languages and supervisors in 1987, in the SIAM Journal on Control and Optimization. In the same volume, Wonham and Ramadge proved that the supremal controllable sublanguage exists, that it is the largest fixpoint of a monotone operator, and that it can be computed for regular languages. The computation on the product automaton became the standard tool of the theory, and the TCT program of Wonham's group implements it.
References
- W. M. Wonham and P. J. Ramadge, On the supremal controllable sublanguage of a given language, SIAM Journal on Control and Optimization 25(3), 1987, pages 637 to 659, the existence of the supremal controllable sublanguage, its characterization as the largest fixpoint of a monotone operator, and its computation for regular languages
- P. J. Ramadge and W. M. Wonham, Supervisory control of a class of discrete event processes, SIAM Journal on Control and Optimization 25(1), 1987, pages 206 to 230, controllability and the existence of supervisors
- Christos G. Cassandras and Stéphane Lafortune, Introduction to Discrete Event Systems, third edition, Springer, 2021, Chapter 3, Supervisory Control (the supremal controllable sublanguage and its computation on automata)
- W. Murray Wonham and Kai Cai, Supervisory Control of Discrete-Event Systems, Springer, 2019, Chapter 3, Supervision of Discrete-Event Systems, Basics
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 | H1 sketch |
|
| Lean | L0 no Lean part |
|
| Code (python) | I0 not checked yet |
|
| Visual | V0 none |
|
| Writing | W2 basic |
|
Tag 1CMXJG Edit this page History Report an error Markdown