Skip to content

Add the tram route model to the experimental examples - #3796

Merged
jackiekazil merged 3 commits into
mesa:mainfrom
codebreaker32:examples/tram-model
Aug 13, 2026
Merged

jackiekazil merged 3 commits into
mesa:mainfrom
codebreaker32:examples/tram-model

Conversation

@codebreaker32

@codebreaker32 codebreaker32 commented Aug 11, 2026 •

Copy link
Copy Markdown
Collaborator

Adds a Tram Route Model under mesa/examples/experimental/, as a worked demonstration of mesa.experimental.states — ContinuousState and Threshold( ref #3766 )

What it models

A single tram runs an ordered route of stations. Leaving a stop it accelerates at a fixed rate, coasts once it reaches cruise speed, brakes at an analytically-computed point, arrives, dwells, and departs for the next stop.

Why this example

Every transition is a Threshold crossing rather than a per-tick check:

_cruise_threshold = Threshold(state=speed, limit=cruise_speed, callback="start_coasting", direction="rising")
_braking_threshold = Threshold(state=position, limit=brake_point, callback="brake", direction="rising")
_stop_threshold = Threshold(state=speed, limit=0.0, callback="arrive_at_station", direction="falling")

The two things it is meant to show:

  • Chained continuous state. speed' = acceleration and position' = speed, so position extrapolates as a piecewise quadratic and is exact at any time between events — never a stale value waiting for the next tick.
  • Limits that are Observables, not constants. cruise_speed and brake_point are Observables, and brake_point is rewritten at the start of every segment. The thresholds observe those attributes and re-solve for the new crossing time themselves; nothing re-arms them.

The result is a model with no per-agent step logic. TransitSystem.step exists only to sample for the DataCollector behind the plots, delete it and the tram still runs the route with identical timings, driven entirely by the event queue.

The brake point is derived from the segment's peak speed, min(cruise_speed, sqrt(2d / (1/a + 1/b))), not from cruise speed. On a short segment, or with weak acceleration, the tram never reaches cruise speed; assuming it does puts the brake point outside the segment, and a rising threshold that begins already past its limit never fires at all. This matters because the app exposes all four kinematic parameters as sliders.

Visualization

Screenshot (327) Screenshot (328) Screenshot (329)

Demonstrates continuous states and thresholds: a tram accelerates, coasts,
brakes and dwells its way along a route, with every transition solved for
analytically and scheduled on the event queue rather than polled for on
each tick.
@codebreaker32

Copy link
Copy Markdown
Collaborator Author

The core logic resides in model.py and agents.py which is a roughly ~230 line diff

@codebreaker32 codebreaker32 added example Changes the examples or adds to them. experimental Release notes label labels Aug 11, 2026
@github-actions

Copy link
Copy Markdown

Performance benchmarks:

Model Size Init time [95% CI] Run time [95% CI]
BoltzmannWealth small 🔵 -0.4% [-0.8%, -0.1%] 🔵 +0.0% [-0.1%, +0.2%]
BoltzmannWealth large 🔵 +0.3% [-0.1%, +0.7%] 🔵 +0.2% [-0.2%, +0.5%]
Schelling small 🔵 -0.4% [-0.8%, -0.0%] 🔵 -0.4% [-1.1%, +0.5%]
Schelling large 🔵 -0.7% [-1.2%, -0.3%] 🔵 -0.7% [-1.6%, +0.2%]
WolfSheep small 🔵 -1.0% [-1.4%, -0.7%] 🔵 +2.6% [-1.5%, +6.7%]
WolfSheep large 🔵 -1.1% [-1.4%, -0.7%] 🔵 -0.1% [-0.5%, +0.3%]
SugarscapeG1mt small 🔵 -0.5% [-0.7%, -0.3%] 🔵 +0.8% [-0.8%, +2.3%]
SugarscapeG1mt large 🔵 -1.4% [-2.0%, -0.8%] 🔵 +2.1% [+0.9%, +3.2%]
BoidFlockers small 🔵 -0.7% [-0.9%, -0.5%] 🔵 -0.8% [-1.2%, -0.5%]
BoidFlockers large 🔵 +0.1% [-0.1%, +0.3%] 🔵 -0.8% [-1.1%, -0.5%]

@jackiekazil jackiekazil left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good.
Thank you for breaking this into another PR & for adding the UI & the little tram icon is a nice touch.

@jackiekazil
jackiekazil merged commit 66f405f into mesa:main Aug 13, 2026
11 of 12 checks passed
@jackiekazil

Copy link
Copy Markdown
Member

Thank you @codebreaker32

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

example Changes the examples or adds to them. experimental Release notes label

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants