Smart Robots
Senior 6 · ICT
Smart Robots
Lesson 3 of 5
25 min
At a busy crossroads in the morning, seventeen cars queue on the road from a residential area while two wait on the cross street. A traditional traffic light gives each road the same 30 seconds of green, every cycle, whatever is happening. The long queue grows; the cross street's green runs empty. A traffic police officer would watch the queues and change the lights to suit them, but cannot stand at every junction all day. A smart traffic controller does that job with cameras or road sensors and a decision rule: it is a robot whose actuators are the lights themselves. This lesson uses it to practise designing an AI-enhanced robotic system from first principles.
Definition
Requirement: a statement of what a system must do (functional) or how well and how safely it must do it (non-functional). Example: "Every road gets at least 10 s of green in each cycle."
Adaptive traffic control: changing signal timings in response to measured traffic, instead of following a fixed timetable.
Capacity (of a road at a signal): the largest number of vehicles per unit time that can pass, given its share of green time.
Before building, answer every row. The right-hand column is the smart traffic controller.
| Question | Smart traffic controller |
|---|---|
| Purpose: what problem, for whom? | Reduce waiting at a junction for drivers, riders and pedestrians |
| Environment | Outdoor crossroads: rain, night, motorcycles, pedestrians, emergency vehicles |
| Sensors (sense) | Cameras counting queues; or loops in the road; a pedestrian push button |
| Decision method (think) | Rules that extend or end a green from queue lengths; fuzzy logic or learning as upgrades |
| Actuators (act) | The signal heads; a display of waiting time |
| Connectivity and data | Counts sent to a traffic control centre; no images stored |
| Safety requirements | Minimum green; amber and all-red between greens; never two conflicting greens; a fixed-time fallback if sensors fail |
| Ethics and privacy | Count vehicles, do not identify people; explain the system to the public |
| Testing | Simulate first, then trial at one junction with engineers watching |
You cannot experiment with real traffic, but you can with a simulation. The program below models one hour at a junction, second by second: cars arrive at random (at rates labelled illustrative), one car leaves every 2 seconds from the road with the green, and switching the lights costs 4 seconds of amber and all-red. It compares three controllers on exactly the same random arrivals: an equal fixed split, a fixed split tuned to the morning traffic, and a smart controller that senses the queues. It also runs a second hour in which the busy road changes half-way through, as happens when morning traffic gives way to traffic leaving the city centre. It was run with python3.
import random
def rates(t, flip):
"""Cars arriving per second (illustrative). With flip, the busy road changes after 30 min."""
busy_ns = not (flip and t >= 1800)
return {"NS": 0.30, "EW": 0.10} if busy_ns else {"NS": 0.10, "EW": 0.30}
SWITCH_TIME = 4 # seconds of amber and all-red between greens
def other(d):
return "EW" if d == "NS" else "NS"
def fixed_time(green, t_green, queues):
return t_green >= 30 # always 30 s of green, whatever the queues
def fixed_tuned(green, t_green, queues):
return t_green >= (45 if green == "NS" else 15) # split chosen for this demand
def smart(green, t_green, queues):
"""Sense the queues: keep green while cars are flowing, up to 50 s;
switch when this road is empty and cars wait on the other road."""
if t_green < 10: # minimum green, for pedestrians and safety
return False
if not queues[other(green)]: # nobody waiting on the other road
return False
return not queues[green] or t_green >= 50
def simulate(controller, flip=False, seconds=3600, seed=7):
rng = random.Random(seed) # same arrivals for every controller
queues = {"NS": [], "EW": []} # arrival time of each waiting car
green, t_green, switching = "NS", 0, 0
waits = []
for t in range(seconds):
for road, rate in rates(t, flip).items():
if rng.random() < rate:
queues[road].append(t)
if switching: # lights changing: nobody moves
switching -= 1
if switching == 0:
green, t_green = other(green), 0
continue
if t_green % 2 == 0 and queues[green]: # one car leaves every 2 s
waits.append(t - queues[green].pop(0))
t_green += 1
if controller(green, t_green, queues):
switching = SWITCH_TIME
still_waiting = [seconds - a for q in queues.values() for a in q]
all_waits = waits + still_waiting
return sum(all_waits) / len(all_waits), len(waits), len(still_waiting)
for flip in (False, True):
print("Busy road changes after 30 min" if flip else "Same demand all hour")
for name, ctrl in (("fixed 30-30", fixed_time), ("fixed 45-15", fixed_tuned), ("smart", smart)):
mean, passed, left = simulate(ctrl, flip)
print(f" {name:11} mean wait {mean:6.1f} s cars through {passed} still queuing {left}")
Output:
Same demand all hour
fixed 30-30 mean wait 429.9 s cars through 1124 still queuing 310
fixed 45-15 mean wait 20.0 s cars through 1431 still queuing 3
smart mean wait 14.8 s cars through 1430 still queuing 4
Busy road changes after 30 min
fixed 30-30 mean wait 302.4 s cars through 1311 still queuing 159
fixed 45-15 mean wait 227.0 s cars through 1131 still queuing 339
smart mean wait 15.2 s cars through 1465 still queuing 5Worked example 1: why did the equal split fail?
Explain the 310 cars still queuing for "fixed 30-30" with steady demand, using capacity.
The tuned 45–15 plan gives N–S cars per second, just above its demand, so it copes, until the busy road changes.
Worked example 2: reading the results
Fehr & Peers: Adaptive Signals Explained. A transport-engineering firm explains how detectors and timing changes reduce delay; compare its description with the smart controller in the simulation.
Warning
Safety rules come before cleverness. A smart signal controller must never show green to two conflicting roads, must always include amber and all-red clearance, must give pedestrians enough time to cross, and must fall back to a safe fixed plan if its sensors fail. These rules are enforced by separate safety hardware in real controllers, so that a software error cannot cause a collision.
Tip
Check capacity before you simulate. Multiply each road's share of green time by the flow of one green lane. If demand is higher than that capacity, the queue will grow without limit, whatever controller you test. The quick calculation tells you which simulation results to expect.
traffic.py. Then change the arrival rates to describe a quieter evening (for example 0.15 and 0.12) and run it again. Which controller wins now, and by how much?smart, change the minimum green from 10 s to 20 s and the maximum from 50 s to 30 s. Record the effect on the mean wait in both scenarios.Check: you can explain each change in the results by referring to capacity or to what the controller senses.
Practice
Sources and credits
This lesson adapts CC BY-SA material and is shared under CC BY-SA 4.0.