Running faster#

Before changing anything, look where a run’s time goes. Then use the levers below, most effective first. Every time on this page was measured on a MacBook Pro with an Apple M2 Pro (8 performance and 4 efficiency cores), 32 GB of memory, macOS 26, wall-clock, nothing else running; your machine’s will differ.

Where the time goes#

A run says one line when it ends: its time, its window, how many trips completed, the mean trip and the verdict. run.timings() gives, and timings.csv records, the wall-clock time of every stage of the run, iteration by iteration; the same as a table is logged at the DEBUG level (logging.getLogger("openmobisim").setLevel(logging.DEBUG)), and quiet=True silences the line. For Delft’s default scenario, summed over the iterations:

import openmobisim as ms
import pandas as pd

run = ms.examples.case("delft").scenario("base").run("delft-base")
timings = run.timings().to_pandas()
by_stage = timings[timings["stage"] != "total"].groupby("stage")["seconds"].sum()
by_stage.sort_values(ascending=False).round(2).head(8)
Run 'delft-base': 8.3 s, window 07:30–09:30, 65,313 of 65,956 trips completed, mean trip 23.7 min, verdict good (run.timings() has every stage)
stage
loading              3.69
network_read         2.13
itinerary_choice     1.32
route_choice         1.12
partial_loading      1.05
route_update         0.53
static_routes        0.19
itineraries_setup    0.15
Name: seconds, dtype: float64

route_update is the search for new routes on each loading’s times, loading the traffic simulation itself, itinerary_choice the choice among modes and itineraries, and static_routes the bike and walk routes, found once.

The levers#

Lever

What it changes

Effect on results

ms.demand_sample(trips, weight=k)

One simulated traveller per k people, travelling as a group

Totals kept. Queues form and clear in blocks, so delays read a little higher

flow_level=2 (the default) rather than 3 or 4

Queues take no road space; nothing spills back

No spillback; run.report_spillback() shows where it would have mattered

equilibration_options={"gap_tolerance": …}

Stops iterating earlier or later

A looser equilibrium

route_update_options={"tree_step_s": 1800}

Searches new routes from shared search trees, refreshed every 30 minutes of the day

Each route the fastest at the half hour’s middle. With the next row, on the four starter cities: mean trips within 0.3 min and mode shares within 0.5 points of a search per pair, links’ flows no farther apart than two random seeds’ (6.7–20 % against 10–20 %), the run 1.4 to 2.2 times faster (measured below). The starter cases use it

parking_options={"reach_car_step_s": 1800}

Shares the drives to park-and-ride candidates in the same way

Measured with the row above. The starter cases use it

preset="fast"

Both rows above at once (ms.PRESETS); an option given by name wins over the preset

As the two rows. preset="complete", like no preset, keeps every search on its own

route_cache=True

Keeps generated route sets for the next run of the same scenario in the same process

None: results do not depend on it

threads=

The number of cores used

None: results do not depend on it

Each lever, measured#

Each lever against the starter case as it ships, on Delft and on Amsterdam, one run each:

def seconds(run):
    """The run's own time, as its one-line message says it."""
    t = run.timings()
    return sum(s for stage, s in zip(t["stage"], t["seconds"], strict=True) if stage == "total")


# Each lever against the case as it ships: (the scenario's options, the run's).
levers = {
    "as the case ships it": ({}, {}),
    "a search per pair": ({"route_update_options": {}, "parking_options": {}}, {}),
    "every person simulated": ({"weight": 1}, {}),
    "spillback (flow level 4)": ({"flow_level": 4}, {}),
    "a gap of 2 %": ({"equilibration_options": {"gap_tolerance": 0.02}}, {}),
    "one thread": ({}, {"threads": 1}),
}
rows = []
for city in ("delft", "amsterdam"):
    case = ms.examples.case(city)
    for lever, (options, run_options) in levers.items():
        run = case.scenario("base", **options).run(f"{city}-{len(rows)}", quiet=True, **run_options)
        rows.append({
            "city": city,
            "lever": lever,
            "run (s)": round(seconds(run), 1),
            "loadings": int(max(run.convergence()["iteration"])) + 1,
            "mean trip (min)": round(run.mean_travel_time_s / 60, 2),
            "completed (%)": round(100 * run.completion["completed"] / run.completion["total_trips"], 1),
            "verdict": run.convergence_verdict,
        })
pd.set_option("display.width", 160)  # the whole table on one line per row
pd.DataFrame(rows).set_index(["city", "lever"])
                                    run (s)  loadings  mean trip (min)  completed (%)     verdict
city      lever                                                                                  
delft     as the case ships it          8.2         7            23.70           99.0        good
          a search per pair            11.5         7            23.61           99.0        good
          every person simulated        8.1         7            23.70           99.0        good
          spillback (flow level 4)     37.4         4            23.68           94.4        good
          a gap of 2 %                 10.8        10            23.71           99.4        good
          one thread                   17.1         7            23.70           99.0        good
amsterdam as the case ships it         41.0         6            24.20           97.6        good
          a search per pair           119.0         6            24.21           97.6        good
          every person simulated      141.5         6            24.11           97.4        good
          spillback (flow level 4)    289.4        10            24.46           95.3  acceptable
          a gap of 2 %                 51.1        10            24.17           97.7        good
          one thread                  273.9         6            24.20           97.6        good
build = ms.build_info()
import os

print(f"openmobisim {ms.__version__} on {build['target']}, {os.cpu_count()} cores")
openmobisim 0.1.0 on aarch64-apple-darwin, 12 cores

A run’s time varies from one run to the next by a few per cent on the same machine; the results do not, for a given seed and settings.

For thousands of runs, outputs="minimal" writes a few kilobytes per run instead of megabytes (see Results); the results are the same, and the writing is shorter.

The starter cities’ default runs share their searches, and all but Delft simulate one traveller for every few people (Delft, the smallest, simulates everyone). That is how they run in well under two minutes; see Starter cases.

For bikes#

Bike and walk routes are found once per run (static_routes in the timings), not in every iteration, so cycling adds little to a run’s time.