Add possibility to use a custom speed function for agents#425
Conversation
Grufoony
commented
Mar 12, 2026
- (finally) closes Allow different criteria for setting the velocity of an Agent #43
| ) | ||
|
|
||
| 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
|
|
||
|
|
||
| @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
| 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
|
|
||
|
|
||
| @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
|
|
||
| # 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
| 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
There was a problem hiding this comment.
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::SpeedFunctionand addsFirstOrderDynamics::setSpeedFunction(...)to supportLINEARandCUSTOMspeed models. - Refactors the former
RoadDynamics-based implementation intoFirstOrderDynamics(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 Report❌ Patch coverage is
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
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|