Fix gtfs creation from solution - #51
Merged
Merged
Conversation
feat: specify intervals to fix when running pso
…rval_constraint feat: FleetPerIntervalConstraintHandler can include drt
feat: drt to pt vehicle equivalence
feat: seed solution with reduced fleet, ref #55
Contributor
There was a problem hiding this comment.
Pull request overview
This PR improves fidelity between PSO-encoded PT schedules and the generated GTFS by making template extraction and trip generation direction-aware (including fallback direction inference when direction_id is missing), and extends the optimization pipeline with masking, DRT fleet cost scaling, and new sampling/export utilities.
Changes:
- Update
SolutionConverterGTFS template extraction + trip generation to handle per-direction templates and split headways across directions to avoid service inflation. - Add masked optimization support (
fixed_intervals) and propagate a globaldrt_cost_factorinto fleet constraints. - Extend initial population sampling with “service reduction” perturbations and add/expand tests around these behaviors.
Reviewed changes
Copilot reviewed 15 out of 16 changed files in this pull request and generated 9 comments.
Show a summary per file
| File | Description |
|---|---|
src/transit_opt/gtfs/gtfs.py |
Direction-aware template extraction, splitting factor handling, and direction-preserving trip generation. |
src/transit_opt/preprocessing/prepare_gtfs.py |
Route round-trip estimation updated to account for loop vs linear topology using direction/headsign signals. |
src/transit_opt/optimisation/problems/transit_problem.py |
Masked optimization support (active vs fixed intervals), updated bounds handling, and DRT-aware constraint evaluation routing. |
src/transit_opt/optimisation/problems/base.py |
Add DRT fleet scaling (drt_cost_factor) and fleet mode selection for per-interval constraints. |
src/transit_opt/optimisation/runners/pso_runner.py |
Inject global drt_cost_factor, support fixed_intervals, and pass new sampling parameters into the population builder. |
src/transit_opt/optimisation/utils/population_builder.py |
Add biased “service reduction” sampling that shifts PT headway indices upward (leaving DRT unchanged). |
src/transit_opt/optimisation/config/config_manager.py |
Extend SamplingConfig schema for reductions (but currently not fully wired in). |
src/transit_opt/gtfs/solution_manager.py |
Add fleet stats CSV export per solution (bus vs DRT, per interval). |
src/transit_opt/optimisation/utils/solution_loader.py |
Minor import/style adjustments and updated default headways. |
tests/test_gtfs.py |
Update tests for new template structure (interval → direction → template) and add direction logic test coverage. |
tests/test_transit_problem.py |
Add masking tests and update formatting/imports. |
tests/test_sampling.py |
Add service reduction sampling tests and update formatting/quoting. |
tests/test_constraints.py |
Add DRT cost factor and PT/DRT fleet-mode tests; formatting cleanup. |
configs/config_template.yaml |
Document new config knobs: fixed_intervals, global drt_cost_factor, and sampling reductions. |
notebooks/3b_fix_directions.ipynb |
Investigation notebook documenting direction inference / splitting strategies. |
💡 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.
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Fix gtfs io discrepancy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This pull request introduces improvements to the GTFS route template extraction and trip generation logic. This affects the creation of GTFS feeds from PSO solutions. Misalignment between the PSO encoded representation of a PT schedule and the GTFS created from that representation means that MATSim simulations were not evaluating the actual PT solution provided by PSO.
Problem
The
SolutionConverterwas failing to generate bi-directional service in the output GTFS feeds.extract_route_templates, the logic looked for any valid trip within the time window for a givenroute_id. It would grab the first one it found (e.g., 'Outbound') and use that template for all generated trips for that route.trips.txtonly contained service for one direction.stop_times.txtis generated directly from these templates, the file was missing all stop times for the return direction.GTFS extraction and trip generation improvements:
extract_route_templates: Now detects and splits routes bydirection_idor dominanttrip_headsigns (ifdirection_idis missing, which it is for the Leeds BODS data I am using), applies splitting factors for multi-direction routes, and stores per-direction templates for each interval.trip_headsignandshape_idis now properly propagated and fallback logic is improved.Final thoughts
Decoding solutions to GTFS is proving to be a difficult problem, and there is still some discrepency between the fleet size per interval from the PSO results, and the fleet size per interval of the created GTFS. This PR has gone some way to reduce the discrepancy, but further improvements are possible. A next step could be to trace the steps in encoding and decoding and ensure that they are exactly the same
Update
Done in #57 , see this notebook: https://github.com/Hussein-Mahfouz/transit_opt/blob/ff7a0ca743fdd1a67b6c7bfe7dad46c027e7e5e4/notebooks/4_validate_gtfs_io.ipynb