User Model
This chapter specifies the reference implementations for User Model components in MaaS Blender. The User Model represents the decision-making logic of individual users throughout their journey. It orchestrates trip planning, route selection, reservation, and execution by coordinating with planners and mobility services through event-driven interactions.
The project currently ships two reference implementations under maasblender/src/user_model:
- Simple User Model: for straightforward route selection with basic preference handling.
- Favorite-based User Model: for more sophisticated per-user preferences including favorite services and sorting criteria.
Both implementations share the same event contract and lifecycle but differ in route-selection policies and configuration options.
Common Lifecycle
All User Model implementations follow this general flow:
- The simulator forwards a
DEMANDto the User Model viaUserManager.demand(...). - The manager calls an external route planner (
Planner) withplan(org, dst, dept). - The User Model ranks/filters candidates according to its selection policy (varies by implementation).
- The selected route is converted into a sequence of tasks (
Reserve,Trip,Wait). - The
Userentity runs the tasks in order, emittingRESERVEand/orDEPARTat appropriate times. - On
RESERVEDfailure, the User Model switches to a recovery task sequence (fallback to alternative plan or walking). - The User Model continues to observe
DEPARTEDandARRIVEDto orchestrate transfers and detect leg completion.
Simple User Model
- Files:
user_model/simple/ - Primary class:
UserManager
This model provides a user decision policy with a small set of preferences.
Behavior
- Calls
Planner.plan(org, dst, dept)to retrieve route candidates. - If the
DEMANDprovidesservice, applies preference mode:PreferenceMode.fixed: keeps only plans that contain the specified service.- Otherwise: sorts so that plans containing the service come first.
- If only 1 plan is returned, builds tasks for that plan with walking fallback for mobility legs.
- If 2+ plans are returned, builds a primary sequence and sets the secondary as recovery; recovery is used if the primary mobility leg fails.
- If a route pattern matches "reservation-required" (heuristic: 3-trip route with the middle leg's
serviceinconfirmed_services), the manager builds aReservetask for that leg. Otherwise, legs are built as directTriptasks (e.g., walking or services without reservations). - If
dept > nowand the first task is aTrip, aWait(dept)task is inserted before the first leg.
Configuration
{
"preference_mode": "fixed", // or any other value for prefer mode
"confirmed_services": ["taxi", "bus"] // services that require reservation
}
preference_mode(fromjschema.query.PreferenceMode):"fixed": filters plans to only those that include the specified service per demand.- Otherwise: prefers plans containing the specified service but allows others.
confirmed_services: list of services that require reservation.
Favorite-based User Model
- Files:
user_model/favorite/ - Primary class:
UserManager
This model provides richer, per-user route preferences including favorite service, walking time limits, and candidate sorting.
Behavior
- Each user gets a
RouteFilterinstance. If aUserTypeis provided, the filter becomes aFavoriteSortedRouteFilterthat:- Sorts plans by
SortType(e.g., earliest arrival, least transfers, lowest cost). - Applies checks for favorite service presence and enforces a walking time cap when applicable.
- Sorts plans by
- If
fixed_serviceis provided in theDEMAND, it takes priority: plans are filtered so that the given service is included. - Waiting and failure handling are analogous to the Simple model, including "reservation vs direct depart" and walking fallbacks.
Configuration
{
"user_params": {
"U001": {
"favorite_service": "rail",
"walking_time_limit_min": 15.0,
"sort_type": "earliest_arrival"
},
"U002": {
"favorite_service": "bus",
"walking_time_limit_min": 10.0,
"sort_type": "least_transfers"
},
"U003": null
},
"confirmed_services": ["taxi", "shuttle"]
}
user_params: dictionary mapping user IDs to their preferences (UserTypeornull):favorite_service: preferred mobility service (e.g.,"rail","bus").walking_time_limit_min: maximum acceptable walking time in minutes.sort_type: sorting a criterion fromjschema.query.SortType(e.g.,"earliest_arrival","least_transfers","lowest_cost").
confirmed_services: list of services that require reservation (same as a Simple model).