Skip to content

Add possibility to use a custom speed function for agents#425

Merged
Grufoony merged 5 commits intomainfrom
customSpeedFunction
Mar 12, 2026
Merged

Add possibility to use a custom speed function for agents#425
Grufoony merged 5 commits intomainfrom
customSpeedFunction

Conversation

@Grufoony
Copy link
Collaborator

)

from tqdm import trange
from numba import cfunc, float64

Check warning

Code scanning / Pylintpython3 (reported by Codacy)

third party import "from numba import cfunc, float64" should be placed before "from dsf.cartography import get_cartography" Warning

third party import "from numba import cfunc, float64" should be placed before "from dsf.cartography import get_cartography"


@cfunc(float64(float64, float64), nopython=True, cache=True)
def custom_speed(max_speed, density):

Check warning

Code scanning / Pylintpython3 (reported by Codacy)

Missing function or method docstring Warning

Missing function or method docstring
dynamics = Dynamics(road_network, seed=args.seed)
dynamics.setWeightFunction(PathWeight.TRAVELTIME)
dynamics.setSpeedFunction(SpeedFunction.LINEAR, 0.8)
# To use a custom speed function, you must pass the pointer to the compiled function using the address attribute

Check warning

Code scanning / Pylintpython3 (reported by Codacy)

Line too long (116/100) Warning

Line too long (116/100)


@cfunc(float64(float64, float64), nopython=True, cache=True)
def custom_speed(max_speed, density):

Check warning

Code scanning / Pylint (reported by Codacy)

Missing function docstring Warning

Missing function docstring

# Create a dynamics model for the road network
dynamics = Dynamics(road_network, seed=args.seed, alpha=0.8)
dynamics = Dynamics(road_network, seed=args.seed)

Check warning

Code scanning / Pylint (reported by Codacy)

Constant name "dynamics" doesn't conform to UPPER_CASE naming style Warning

Constant name "dynamics" doesn't conform to UPPER_CASE naming style
dynamics = Dynamics(road_network, seed=args.seed)
dynamics.setWeightFunction(PathWeight.TRAVELTIME)
dynamics.setSpeedFunction(SpeedFunction.LINEAR, 0.8)
# To use a custom speed function, you must pass the pointer to the compiled function using the address attribute

Check warning

Code scanning / Pylint (reported by Codacy)

Line too long (116/100) Warning

Line too long (116/100)
CHECK(simColumns.count("name") == 1);
CHECK(simColumns.count("alpha") == 1);
CHECK(simColumns.count("speed_fluctuation_std") == 1);
CHECK(simColumns.count("speed_function") == 1);

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 10.4 rule Note test

MISRA 10.4 rule
)

from tqdm import trange
from numba import cfunc, float64

Check warning

Code scanning / Prospector (reported by Codacy)

Unable to import 'numba' (import-error) Warning

Unable to import 'numba' (import-error)
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR implements issue #43 by allowing users to configure how agent velocity is computed via a selectable “speed function” (built-in linear model or user-provided custom function), and propagates the change through tests, Python bindings, and examples.

Changes:

  • Introduces dsf::SpeedFunction and adds FirstOrderDynamics::setSpeedFunction(...) to support LINEAR and CUSTOM speed models.
  • Refactors the former RoadDynamics-based implementation into FirstOrderDynamics (and updates tests accordingly).
  • Updates pybind11 bindings + example script to expose/configure speed functions from Python (including a Numba cfunc pointer path).

Reviewed changes

Copilot reviewed 7 out of 8 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
test/mobility/Test_dynamics.cpp Updates construction/configuration to use the new weight/speed setter APIs and adjusts summary expectations.
src/dsf/utility/Typedef.hpp Adds SpeedFunction enum for selecting speed model.
src/dsf/mobility/RoadDynamics.hpp Removes the generic RoadDynamics header (logic moved/specialized elsewhere).
src/dsf/mobility/FirstOrderDynamics.hpp Adds speed-function support and inlines/refactors dynamics API surface.
src/dsf/mobility/FirstOrderDynamics.cpp Implements speed-function-based travel-time weighting and updates DB metadata schema/insert.
src/dsf/bindings.cpp Exposes SpeedFunction and adds a Python setSpeedFunction wrapper (including Numba pointer support).
examples/simulate_city.py Demonstrates configuring PathWeight + SpeedFunction, including a Numba custom speed function.
examples/requirements.txt Updates example dependencies to include numba (but currently drops numpy).

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

You can also share your feedback on Copilot code review. Take the survey.

@codecov
Copy link

codecov bot commented Mar 12, 2026

Codecov Report

❌ Patch coverage is 74.50097% with 396 lines in your changes missing coverage. Please review.
✅ Project coverage is 87.21%. Comparing base (846a146) to head (437126e).
⚠️ Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
src/dsf/mobility/FirstOrderDynamics.cpp 72.20% 393 Missing ⚠️
src/dsf/mobility/FirstOrderDynamics.hpp 93.33% 3 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #425      +/-   ##
==========================================
- Coverage   87.77%   87.21%   -0.56%     
==========================================
  Files          52       52              
  Lines        6321     6428     +107     
  Branches      701      714      +13     
==========================================
+ Hits         5548     5606      +58     
- Misses        754      803      +49     
  Partials       19       19              
Flag Coverage Δ
unittests 87.21% <74.50%> (-0.56%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@Grufoony Grufoony merged commit 452de9d into main Mar 12, 2026
43 of 45 checks passed
@Grufoony Grufoony deleted the customSpeedFunction branch March 12, 2026 11:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Allow different criteria for setting the velocity of an Agent

2 participants