Skip to main content

Rust mode-sequence search kernel for mobility

Project description

mobility-mode-sequence-search

Rust package for mode-sequence search in mobility.

Scope

This package owns:

  • compact indexing of mode and cost data
  • top-k mode-sequence search
  • chain-level parallelism
  • a Python extension API that accepts and returns columnar data

The main mobility repository remains responsible for orchestration, validation, and any fallback Python implementation.

How It Works

This package searches feasible mode sequences for an ordered chain of places.

For example, if someone goes from home to a shop, feasible answers might include:

  • walk, walk
  • car outbound, car return
  • bike outbound, bike return

Feasibility is constrained by a few simple rules:

  • a vehicle can only be used if it is currently at the traveler's location
  • if a vehicle leaves home, it must end back at home
  • some outbound choices come in matched pairs, requiring a linked return mode later in the same subtour

Conceptually, the algorithm has four stages:

  1. Split long chains into smaller home-to-home segments when possible.
  2. Search each segment incrementally, always expanding the cheapest partial answer first.
  3. Merge the best segment-level answers into full-chain answers.
  4. Keep only the leading answers needed to reach the requested cumulative probability threshold.

Before search, each input chain is closed internally by appending the starting location to the end. This matches the legacy Python backend, which searches a closed tour rather than the raw caller-provided chain.

Pseudocode

for each chain:
  split the chain into home-to-home segments

  for each segment:
    start with one empty partial answer

    while there are still partial answers to explore:
      take the cheapest partial answer so far

      if it already covers every leg:
        if all vehicles are back home:
          save it as a feasible full answer
        continue

      look up the next leg's available modes

      for each allowed mode:
        update vehicle locations
        update any forced future return-mode rule

        if the partial answer is still feasible:
          put the extended partial answer back into the queue

  merge the best segment answers into full-chain answers
  prune the low-probability tail
  write the retained answers as output rows

Implementation details are in rust/search.rs and rust/input.rs.

Python API

import polars as pl
from mobility_mode_sequence_search import search_mode_sequences

result = search_mode_sequences(
    location_chain_steps=pl.DataFrame(...),
    leg_mode_costs=pl.DataFrame(...),
    mode_metadata=pl.DataFrame(...),
    k_sequences=20,
    cumulative_prob_threshold=0.98,
    n_threads=None,
)

Input Schemas

location_chain_steps

Grouped form:

  • utility_profile_id: UInt32 (optional, defaults to zero)
  • dest_seq_id: UInt64
  • locations: List[UInt32]

Long-form:

  • utility_profile_id: UInt32 (optional, defaults to zero)
  • dest_seq_id: UInt64
  • seq_step_index: UInt32
  • location: UInt32

leg_mode_costs

  • utility_profile_id: UInt32 (optional, defaults to zero)
  • origin: UInt32
  • destination: UInt32
  • mode_id: UInt16
  • cost: Float64

mode_metadata

  • mode_id: UInt16
  • needs_vehicle: Boolean
  • vehicle_id: UInt8 | Utf8 | null
  • multimodal: Boolean
  • is_return_mode: Boolean
  • return_mode_id: UInt16 | null

At the package boundary, vehicle_id may be integer/null or string/null. String labels are normalized internally into numeric ids before search. Mixed integer and string representations within one call are rejected.

Several utility profiles can be searched in one call. Chains read the costs with the same utility_profile_id, while mode metadata and the Rayon thread pool are shared across all profiles. Each referenced profile must provide the mode costs needed by its chains. Calls without the column keep the original single-profile behavior.

Output Schema

  • utility_profile_id: UInt32 (when supplied on the chains)
  • dest_seq_id: UInt64
  • mode_seq_index: UInt32
  • seq_step_index: UInt32
  • location: UInt32
  • mode_index: UInt16

Development

mamba run -n mobility python -m pip install -e .[dev]
mamba run -n mobility python -m pytest
mamba run -n mobility python -m maturin build --release

Performance Fixture

A standalone synthetic performance case is available at tests/perf_synthetic_case.py.

Example:

mamba run -n mobility python tests/perf_synthetic_case.py --n-chains 2000 --chain-len 18 --n-locations 128 --k-sequences 20

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mobility_mode_sequence_search-0.1.1.tar.gz (22.1 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

mobility_mode_sequence_search-0.1.1-cp311-abi3-win_amd64.whl (208.8 kB view details)

Uploaded CPython 3.11+Windows x86-64

mobility_mode_sequence_search-0.1.1-cp311-abi3-manylinux_2_34_x86_64.whl (333.3 kB view details)

Uploaded CPython 3.11+manylinux: glibc 2.34+ x86-64

mobility_mode_sequence_search-0.1.1-cp311-abi3-macosx_11_0_arm64.whl (292.2 kB view details)

Uploaded CPython 3.11+macOS 11.0+ ARM64

File details

Details for the file mobility_mode_sequence_search-0.1.1.tar.gz.

File metadata

File hashes

Hashes for mobility_mode_sequence_search-0.1.1.tar.gz
Algorithm Hash digest
SHA256 cd937d469b711eefe30629051c8da66a87ea87b0cc93462e265fecb5eade98f4
MD5 07905eff6da78de8a0f4490df60f16bc
BLAKE2b-256 bee4fc736e577653f3e254373967e16aa3d806695753dacf79f3a977fb52100d

See more details on using hashes here.

Provenance

The following attestation bundles were made for mobility_mode_sequence_search-0.1.1.tar.gz:

Publisher: wheels.yml on mobility-team/mobility-mode-sequence-search

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file mobility_mode_sequence_search-0.1.1-cp311-abi3-win_amd64.whl.

File metadata

File hashes

Hashes for mobility_mode_sequence_search-0.1.1-cp311-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 0b4aaebaaea87efafb5afa56d50213ebf4e41d5f1ba23dfcd55fd86a0c36566f
MD5 3a68c5618a1916c773d2df08d5cf8cd6
BLAKE2b-256 65b8e4d90cf6378ce752c2f5f64060bad7a7fa3ccebf92023f703b4b9760d4b5

See more details on using hashes here.

Provenance

The following attestation bundles were made for mobility_mode_sequence_search-0.1.1-cp311-abi3-win_amd64.whl:

Publisher: wheels.yml on mobility-team/mobility-mode-sequence-search

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file mobility_mode_sequence_search-0.1.1-cp311-abi3-manylinux_2_34_x86_64.whl.

File metadata

File hashes

Hashes for mobility_mode_sequence_search-0.1.1-cp311-abi3-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 003f09c137b039a0cdccc15c8003948566a0cd7e6c861fb9ed37b1bb71da9569
MD5 1e0ba015983c8ec824db53ae8ea058b6
BLAKE2b-256 0fa4131b433ea8be49ae662522320404277643b3d5f796413978276f75d9bbe5

See more details on using hashes here.

Provenance

The following attestation bundles were made for mobility_mode_sequence_search-0.1.1-cp311-abi3-manylinux_2_34_x86_64.whl:

Publisher: wheels.yml on mobility-team/mobility-mode-sequence-search

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file mobility_mode_sequence_search-0.1.1-cp311-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for mobility_mode_sequence_search-0.1.1-cp311-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 8286da0e7f215747675cd5b825dc5696ceca7e0a6d7cfd44204569638c33f35d
MD5 00a098031324056b0eaf1a5341c36a65
BLAKE2b-256 6627fea29f8d0a20729ab746d23713cc85a0d829e21953347405a9143dfd334d

See more details on using hashes here.

Provenance

The following attestation bundles were made for mobility_mode_sequence_search-0.1.1-cp311-abi3-macosx_11_0_arm64.whl:

Publisher: wheels.yml on mobility-team/mobility-mode-sequence-search

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page