A traffic simulation needs a network, a demand and a simulator. This chapter makes the SUMO and MATSim networks of Monaco, runs a short SUMO simulation with random trips and puts the counts back on the duckOSM edges. The id of each SUMO edge and of each MATSim link is the edge_id, so no table of ids is necessary.
You learn
what a traffic simulation needs
the SUMO network with to_sumo and its options
how you read it with sumolib
edge_map from the SUMO ids to edge_id
a short SUMO run and its counts on the edges
a closed street with traci, and where its traffic goes
# Install duckOSM when it is not installed (for example in Colab). It takes approximately 1 minute.# Until duckOSM 0.9.0 is on PyPI, the SDK of these notebooks is in the nightly wheel of main (the GitHub pre-release "nightly").# roadstyle (the maps) also comes from its nightly wheel, so the newest roadstyle parts work with no roadstyle release.import importlib.util, json, os, urllib.requestdef nightly_wheel(repo="Khoshkhah/duckOSM", name=".whl"): # the address of the newest nightly wheel of a repository release = json.load(urllib.request.urlopen(f"https://api.github.com/repos/{repo}/releases/tags/nightly"))returnnext(a["browser_download_url"] for a in release["assets"] if name in a["name"] and a["name"].endswith(".whl"))if importlib.util.find_spec("duckosm") isNone: # not installed: install it with the extras of this notebook%pip install -q "duckosm[sim,viz] @ {nightly_wheel()}"%pip install -q traci%pip install -q --force-reinstall --no-deps "roadstyle @ {nightly_wheel('Khoshkhah/roadstyle', 'manylinux')}"import duckosm # the SDKifnothasattr(duckosm, "network"): # an old duckOSM (0.8.0 or older), without the SDK%pip install -q --force-reinstall --no-deps "duckosm @ {nightly_wheel()}"raiseSystemExit("duckOSM is updated: restart the session (in Colab: Runtime > Restart session), then run this cell again")print("duckosm", duckosm.__version__) # the version# The network of chapter 1 (monaco.duckdb). The chapter opens it. When you run this chapter alone (in Colab), the file# is not there, so this cell makes it one time.ifnot os.path.exists("monaco.duckdb") ornot duckosm.open("monaco.duckdb").has_mode("driving", "walking", "cycling"): duckosm.network(osm_id=2220322, modes=["driving", "walking", "cycling"], path="monaco.duckdb")
12.1 What a traffic simulation needs
A traffic simulation moves vehicles one by one on a road network and gives the result: the count of vehicles on each road, their speed, the queues and the travel times. It needs three parts:
Part
What it is
Where it comes from
the network
the roads, their lanes and speeds, and the turns at each junction
duckOSM: to_sumo or to_matsim
the demand
the trips: where each vehicle starts, where it goes, and when
your origin-destination data, or random trips for a test
the simulator
the program that moves the vehicles
SUMO (microscopic: each vehicle and each lane) or MATSim (agents and their daily plans)
The module duckosm.sim makes the network for the two simulators, and gives you the way back from their results to the duckOSM edges.
Simulator
The duckOSM call
The file
Use it for
SUMO
to_sumo
network.net.xml (and the plain XML files)
each vehicle and lane, traffic lights, junctions
MATSim
to_matsim
network.xml.gz
large areas, daily plans of agents, many iterations
The loop of a simulation: the duckOSM network, the SUMO network, the demand, the simulation, and the results back on the edges
ImportantKey idea
The id of each SUMO edge and of each MATSim link is the duckOSM edge_id. So the results of a simulation join back to the network, to your own data (chapter 4) and to the map, with no table of ids to keep.
Chapter 1 builds Monaco with three modes (driving, walking and cycling) and saves it as monaco.duckdb. This chapter opens that file with duckosm.open: it builds nothing. The simulation uses the driving network. The extra sim installs SUMO (with its program netconvert) and sumolib.
net = duckosm.open("monaco.duckdb")net.driving
driving of monaco.duckdb
mode
edges
nodes
km
oneway_km
driving
2840
1768
96.2
35.5
The driving network of Monaco has 2840 edges and 1768 nodes, 96.2 km of roads.
12.3 The SUMO network: to_sumo
net.driving.to_sumo(out_dir) (the same as duckosm.sim.to_sumo(net.driving, out_dir)) writes the network of one mode in SUMO’s plain XML files and the netconvert config (chapter 6). The simulation needs the .net.xml, so this chapter gives run_netconvert=True: duckOSM then runs SUMO’s netconvert on the config.
File
What it has
network.nod.xml
the nodes: one SUMO junction for each duckOSM node
network.edg.xml
the edges: the id (the edge_id), the lanes, the speed, the shape
network.con.xml
the connections: the turns that are allowed at each junction, from driving.edge_graph, so the turn restrictions apply
network.add.xml
the bus stops, for SUMO (--additional-files)
network.netccfg
the netconvert config: the input files and the options of netconvert
network.net.xml
the network that SUMO reads, made by netconvert
network.tll.xml
the signal programs that netconvert made; edit them and run netconvert -c network.netccfg again
The call gives a dict with the path of each file and the counts.
Argument
Default
What it does
net_name
"network"
the first part of each file name
run_netconvert
False
True: also run netconvert, for the .net.xml and the .tll.xml
connections
True
False: no .con.xml, so netconvert chooses the turns itself
config
none
a dict of options that change the default config, or your own .netccfg file
files = net.driving.to_sumo("out/sumo", run_netconvert=True){k: str(v) for k, v in files.items()}
The SUMO network has 1768 nodes and 2840 edges, the same numbers as the driving network, and 5082 connections. The nodes have 18 traffic lights and 12 stop signs, and network.add.xml has 106 bus stops. net is the path of network.net.xml, the file that SUMO reads; tll has the signal programs that netconvert made.
12.3.1 The netconvert options
duckOSM gives netconvert a small set of options, duckosm.sim.DEFAULT_NETCFG. They keep each duckOSM edge as one SUMO edge with the same id.
the nodes are in lon and lat, and netconvert projects them to the UTM zone of the area
geometry.remove
false
netconvert must not join two edges into one, so each SUMO edge is one edge_id
junctions.join
false
netconvert must not join near nodes into one junction
no-turnarounds.except-deadend
true
no U-turns, except at a dead end
numerical-ids
false
the ids stay as duckOSM writes them
tls.default-type
actuated
OSM has no signal timings: the program of each traffic light follows the traffic
12.3.2 Your own options: config=
config= takes your own .netccfg file, and netconvert then uses it in place of the defaults. This cell writes a file with the defaults and one change, geometry.remove = true, to show why the default is false.
cfg = {**duckosm.sim.DEFAULT_NETCFG, "geometry.remove": "true"}withopen("merge.netccfg", "w") as f: f.write("<configuration>\n"+"".join(f' <{k} value="{v}"/>\n'for k, v in cfg.items()) +"</configuration>\n")merged = net.driving.to_sumo("out/sumo_merged", config="merge.netccfg", run_netconvert=True)len(duckosm.sim.edge_map(merged)), files["n_edges"]
SUMO out/sumo_merged: done in 0.4 s
SUMO[driving]: 18 traffic lights, 12 stop signs, 106 bus stops
running: netconvert -c merge.netccfg --node-files out/sumo_merged/network.nod.xml --edge-files out/sumo_merged/network.edg.xml --output-file out/sumo_merged/network.net.xml --connection-files out/sumo_merged/network.con.xml
(1629, 2840)
With geometry.remove = true, only 1629 of the 2840 edges keep their edge_id as a SUMO edge. netconvert joined the other 1211 edges into their neighbors.
WarningWarning
Keep geometry.remove set to false. With true, netconvert joins the edges between two junctions into one SUMO edge, and keeps only the id of the first edge. The other edges then have no SUMO edge, and their results are lost.
12.3.3 Fewer SUMO edges, with all the edge ids: merge=True
duckOSM cuts a road at each node that another network uses, for example a footway crossing, because the multimodal routing needs the cut. SUMO does not need these cuts. The table driving.chains joins the edges where the road only continues, and merge=True writes one SUMO edge for each chain:
chained = net.driving.to_sumo("out/sumo_chains", merge=True, run_netconvert=True)duckosm.sim.edge_map(chained) # each SUMO edge, with every edge_id of its chain
On Monaco, the 2840 edges become 1775 SUMO edges, and the junctions go from 2995 to 1592. All 18 traffic lights stay. Each joined edge has <param key="edge_ids">, so edge_map gives back every edge_id, and no result is lost.
12.3.4 Without netconvert
With the default run_netconvert=False, to_sumo writes the plain XML files and the config, and does not run netconvert. Use it when SUMO is on another machine, or when you change the config first (chapter 6).
plain = net.driving.to_sumo("out/sumo_plain")sorted(k for k in plain ifnot k.startswith("n_"))
SUMO out/sumo_plain: done in 0.1 s
SUMO[driving]: 18 traffic lights, 12 stop signs, 106 bus stops
['add', 'con', 'edg', 'netccfg', 'nod']
The dict has the plain XML files (nod, edg, con), the bus stops (add) and the config (netccfg), and no net. Run netconvert -c out/sumo_plain/network.netccfg to make the network.
12.4 Read the network with sumolib: sumo_net
duckosm.sim.sumo_net(files) reads the .net.xml with sumolib (sumolib.net.readNet), with the internal edges of the junctions. It takes the path of a .net.xml or the dict of to_sumo. The result is a sumolib.net.Net: use sumolib’s own calls on it.
Sumolib reads 2840 SUMO edges and 1768 junctions: one SUMO edge for each duckOSM edge, and one junction for each node. The 15 traffic lights are the programs of the 18 traffic-light junctions (chapter 6): the lights within 20 m of each other share one program.
The id of the SUMO edge is an edge_id, and the ids of its two junctions are OSM node ids. The edge has 1 lane, 47.3 m. The speed is 5.56 m/s (20 km/h), from the maxspeed_kmh of the edge.
12.5 From the SUMO ids to the edge ids: edge_map
duckosm.sim.edge_map(path) gives a DataFrame with two columns: id, the id of each edge in the file (text), and edge_id, its duckOSM edge_id (a number). duckOSM writes the edge_id as the SUMO id, so the two are the same in a network of to_sumo. When a tool changes an id, edge_map still finds the edge_id:
The id in the file
edge_map gives
123
one row: 123
123#1 (netconvert cut the edge 123)
one row: 123
123+456 (two edges joined)
two rows: 123 and 456
:junction_0 (an internal edge of a junction)
no row
path can be a .net.xml, an .edg.xml, the dict of to_sumo, or a MATSim network (section “MATSim” below).
edges = duckosm.sim.edge_map(files)print(len(edges), "rows;", int((edges.id!= edges.edge_id.astype(str)).sum()), "ids that netconvert changed")edges.head(3)
2840 rows; 0 ids that netconvert changed
id
edge_id
0
1003228148726728921
1003228148726728921
1
1014480835261600237
1014480835261600237
2
1015158544797938083
1015158544797938083
edge_map gives 2840 rows, one for each SUMO edge, and netconvert changed no id. So here id and edge_id are the same number, as text and as an integer.
TipTip
Join the output of a simulation on id, then the network on edge_id. The next section does this.
12.6 Run SUMO
SUMO needs the demand. For a test, SUMO’s tool randomTrips.py makes random trips between random edges. This cell makes one trip each second for 900 s (15 minutes), and asks SUMO to write an edgeData file: for each edge, the number of vehicles that entered it, and their speed and time on it. Then sumo runs the trips for 30 minutes.
Program
Arguments
What it does
randomTrips.py
-n the network, -e 900, -p 1, --validate
900 random trips, one each second, each one with a possible route
sumo
-n the network, -r the trips, -a the edgeData file, --end 1800
the simulation, 30 minutes
sumo.SUMO_HOME is the folder of the SUMO of the extra sim.
The return code 0 tells that SUMO ran with no error. SUMO wrote out/sumo/edgedata.xml, with one <edge> element for each SUMO edge.
12.7 Join the counts back to the edges
pandas.read_xml reads the edgeData file: one row for each SUMO edge, with the column entered (the vehicles that entered the edge). The merge on id with edge_map gives each row its edge_id. The sum by edge_id puts the parts of an edge together when netconvert cut it.
count 2840.000000
mean 18.644366
std 25.391023
min 0.000000
25% 1.000000
50% 7.000000
75% 25.000000
max 144.000000
Name: entered, dtype: float64
Each of the 2840 edges has a count. Half of the edges have 7 cars or fewer in the 30 minutes, and some have none. The busiest edge has 144 cars.
net.driving.map(color_by=cars)
The main roads and the tunnels have the most cars (green and yellow): the random trips cross Monaco on them. The small streets have few cars or none.
top = net.driving.edges(where=f"edge_id IN ({','.join(map(str, cars.nlargest(3).index))})")[["edge_id", "name", "highway"]]top.assign(cars=top.edge_id.map(cars)).sort_values("cars", ascending=False)
edge_id
name
highway
cars
0
4230741665531352064
Place d'Armes
secondary
144
1
4551155039579564675
Boulevard Albert 1er
secondary
128
2
8037909646003515605
Boulevard Albert 1er
secondary
128
NoteWhat the output shows
The busiest edge is on the Place d’Armes (144 cars). The next two are two edges of the Boulevard Albert 1er (128 cars each). These are secondary roads. The names come from the duckOSM edges, through the edge_id.
12.8 Control the simulation with traci
The run above starts SUMO and reads its file at the end. traci is SUMO’s Python interface: Python starts SUMO, moves it one second at a time, reads values while it runs, and changes the network. The edge_id is the key in both directions: it names the street to change in SUMO, and it brings the SUMO values back to the duckOSM edges.
The question here: when the busiest street is closed after 10 minutes, where does its traffic go? The steps:
Ask SUMO to count the vehicles on each edge in the time window from 600 s to 1200 s (an edgeData file with begin and end).
Run the same trips one time with no change. This is the baseline.
Run them again with traci. At 600 s, close the busiest edge to cars (traci.edge.setDisallowed), and send the vehicles on the road to a new route (traci.vehicle.rerouteTraveltime). Every 10 s, read the number of vehicles in the network. At 900 s, read the position of each vehicle.
Compare the two runs in the same window, edge by edge.
--ignore-route-errors lets SUMO go on when a trip has no route after the closure (SUMO then drops the trip).
import tracitraci.start(run + ["-a", "out/sumo/closed.add.xml","--fcd-output", "out/sumo/fcd.parquet", "--fcd-output.geo", "true"])in_network, positions = [], Nonefor t inrange(1, 1801): traci.simulationStep()if t ==600:for sid in closed_sumo: traci.edge.setDisallowed(sid, ["passenger"])for vehicle in traci.vehicle.getIDList(): traci.vehicle.rerouteTraveltime(vehicle)if t %10==0: in_network.append((t, traci.vehicle.getIDCount()))if t ==900: positions = pd.DataFrame([traci.simulation.convertGeo(*traci.vehicle.getPosition(v))for v in traci.vehicle.getIDList()], columns=["x", "y"])traci.close()in_network = pd.DataFrame(in_network, columns=["t", "vehicles"])net.driving.edges(where=f"edge_id = {closed}")[["edge_id", "name"]], len(positions)
Retrying in 1 seconds
( edge_id name
0 4230741665531352064 Place d'Armes,
255)
The closed edge is the Place d’Armes, the busiest edge of the first run. At 900 s, 255 vehicles are in the network. traci prints “Retrying in 1 seconds” while it connects to SUMO.
import matplotlib.pyplot as pltfig, ax = plt.subplots(figsize=(8, 3))ax.plot(in_network.t /60, in_network.vehicles, color="#1f6feb")ax.axvline(10, color="#d62728", linestyle="--", label="the edge closes")ax.set_xlabel("minutes"); ax.set_ylabel("vehicles in the network"); ax.legend()plt.show()
The trips start in the first 15 minutes, so the network fills up to about 250 vehicles. The closure at 10 minutes does not change this total much: the vehicles take other routes. After 15 minutes no new trip starts, and the network is empty after about 25 minutes.
Each red dot is one vehicle at 15 minutes. traci gives its position in SUMO’s coordinates, and traci.simulation.convertGeo turns it into longitude and latitude. The black ring is the closed edge. The same run also wrote every vehicle at every second to out/sumo/fcd.parquet (SUMO’s FCD output). A later version of this manual can play this file as moving vehicles on the live map, with roadstyle’s vehicle player.
In the window from 10 to 20 minutes, 72 fewer vehicles enter the Place d’Armes than in the baseline, and 52 fewer enter two edges of the Avenue Prince Pierre, the street that leads to the square. The Avenue d’Ostende and the Avenue Princesse Alice each get 36 more: the vehicles go around the closed square on them.
Blue streets have fewer vehicles than in the baseline, and red streets have more. Gray streets have no change. The blue streets lead to the closed square. The red streets, along the port and up to Monte Carlo, are the new routes around it.
12.9 MATSim
MATSim simulates agents: each agent has a plan for the day, and MATSim improves the plans over many iterations. net.driving.to_matsim(out_path) (the same as duckosm.sim.to_matsim) writes MATSim’s network.xml.
Argument
Default
What it does
mode
the mode of the view
one mode, or a list of modes (one network, each link with its modes)
crs
the UTM zone of the area
the projected CRS of the coordinates (MATSim needs metres)
gzip
True
write network.xml.gz
The id of each MATSim link is the edge_id too, so edge_map reads the MATSim network in the same way: give it the network.xml or network.xml.gz.
MATSim out/matsim_network.xml.gz: done in 0.1 s
{'nodes': 1768, 'links': 2840, 'crs': 'EPSG:32632'}
(2840, True)
NoteWhat the output shows
The MATSim network has 1768 nodes and 2840 links, in the CRS EPSG:32632 (UTM zone 32N, the zone of Monaco). edge_map reads 2840 links, and their ids are the edge ids of the network.
12.9.1 MATSim lanes and signals: to_matsim_lanes
MATSim can also simulate lanes and traffic signals. duckosm.sim.to_matsim_lanes(gmns_db, out_dir) writes MATSim’s lanes.xml and the signal files (signalSystems.xml, signalGroups.xml, signalControl.xml). It reads the lanes and the movements of a GMNS database, which to_gmns makes (chapter 13 tells more about GMNS).
GMNS out/gmns.duckdb: done in 14.2 s
{'lanes_links': 2832, 'signal_systems': 1}
['lanes.xml', 'signalControl.xml', 'signalGroups.xml', 'signalSystems.xml']
NoteWhat the output shows
The GMNS export takes most of the time of the cell, because it runs netconvert for the lanes. lanes.xml has lanes for 2832 links, and the signal files have one signal system: the one signal of the GMNS tables of Monaco.
12.10 Try it yourself
1. Make the SUMO network with no .con.xml (connections=False), and count the files.
TipShow the answer
f = net.driving.to_sumo("out/sumo_nocon", connections=False, run_netconvert=True)sorted(k for k in f ifnot k.startswith("n_"))
2. Find the five edges with the highest mean speed in the SUMO run. The edgeData column speed is in m/s.
TipShow the answer
speed = counts.merge(edges, on="id").groupby("edge_id").speed.mean()speed.nlargest(5) *3.6# km/h
3. Write one MATSim network for driving and walking. Build a network with the two modes first.
TipShow the answer
both = duckosm.open("monaco.duckdb")duckosm.sim.to_matsim(both, "out/matsim_dw.xml.gz", mode=["driving", "walking"])
12.11 Words in this chapter
Word
Meaning
traffic simulation
a program that moves vehicles one by one on a road network
demand
the trips of a simulation: start, end and time
SUMO
Simulation of Urban MObility, a microscopic traffic simulator
netconvert
the SUMO program that makes a .net.xml from the plain XML files
plain XML
SUMO’s simple files of nodes, edges and connections
connection
a turn from one lane of an edge to a lane of the next edge
internal edge
a short SUMO edge inside a junction, for one turn
edgeData
a SUMO output with the counts and speeds of each edge