Repository navigation
Fix #3180: one row per step on undirected edges with different costs - #3191
AdityaRanaX wants to merge 5 commits into
Conversation
|
No actionable comments were generated in the recent review. WalkthroughThe path-reconstruction constructor now emits one row per step. It prefers a matching edge with the stored step cost and otherwise selects the cheapest match. New pgTAP tests cover affected A* and bidirectional routing variants. Release materials document issue ChangesPath result correction
Estimated code review effort: 3 (Moderate) | ~20 minutes Change: Bug fix · Severity of issue fixed: Medium Suggested labels: Suggested reviewers: Merge Risk:
|
cvvergara
left a comment
There was a problem hiding this comment.
Your solution is interesting.
Your pgtap tests are very very interesting and include the other functions that are silently affected.
Please work the pgtap tests first.
First do the file under tools/testers that has the currently duplicated code
(no name changes on the function names)
commit.
Then make the pgtap tests work ( you have to include the file figure it out)
commit.
Then make the now non duplicated code as much as possible self documented.
Maybe some of my comments do not apply once you have the common code set.
Then create more tables with the characteristics I mentioned somewhere.
(things might pass or fail with your solution)
if they fail then modify your solution.
|
|
||
|
|
||
| /* | ||
| * Issue #3180: undirected graph where cost and reverse_cost differ |
There was a problem hiding this comment.
You have a lot of explanation to do in these pgtap files:
As a start: why? you didnt copy/paste the exact title of the issue
| * | ||
| * On an undirected graph an edge whose cost and reverse_cost are both non | ||
| * negative and different is kept as two internal edges that share the same | ||
| * id. pgr_aStar, pgr_aStarCost and pgr_aStarCostMatrix returned a row for |
There was a problem hiding this comment.
This is about pgr_star why do you mention all three functions?
| * negative and different is kept as two internal edges that share the same | ||
| * id. pgr_aStar, pgr_aStarCost and pgr_aStarCostMatrix returned a row for | ||
| * each of them, so every node and edge came back twice and agg_cost added | ||
| * both weights. The pgr_dijkstra family uses each edge once and is the |
There was a problem hiding this comment.
Why do you mention pgr_dijkstra if it does not have a problem?
| * both weights. The pgr_dijkstra family uses each edge once and is the | ||
| * reference here. | ||
| * | ||
| * The chain below costs 7 one way and 2 the other, so the cheapest route |
| CREATE TEMP TABLE edges_3180 AS | ||
| SELECT * FROM (VALUES | ||
| (1::BIGINT, 1::BIGINT, 2::BIGINT, 7::FLOAT, 2::FLOAT, 0::FLOAT, 0::FLOAT, 1::FLOAT, 0::FLOAT), | ||
| (2::BIGINT, 2::BIGINT, 3::BIGINT, 7::FLOAT, 2::FLOAT, 1::FLOAT, 0::FLOAT, 2::FLOAT, 0::FLOAT), |
There was a problem hiding this comment.
There is a more simpler way to write the rows 2 to 5.
which is it?
There was a problem hiding this comment.
So, this table has all the edges have same cost & reverse cost
You are not testing with tables with edges (still asymmetrical) where between rows the cost & reverse cost varies
You are not testing with tables that have parallel edges with edge_id that are different.
|
|
||
| /* the same four checks for one function: the bug, then the two controls */ | ||
| CREATE OR REPLACE FUNCTION issue_3180_variant( | ||
| fn TEXT, ref TEXT, cols TEXT, verts TEXT, h0 TEXT, h5 TEXT) |
There was a problem hiding this comment.
hmm so reading forward down
cols is not columns (like of the columns passed to a function)
Those are the result columns the variant function will compare.
a better name for it is result_cols
There was a problem hiding this comment.
verts = hmm, so they are the parameters they are not precisely vertices.
`1::BIGINT, 6::BIGINT',
'ARRAY[1, 6]::BIGINT[]',
vert_params would be a better name
There was a problem hiding this comment.
for h0 and h5
I allways see on the function call:
', 0, 1::FLOAT, 1::FLOAT', ', 5, 1::FLOAT, 1::FLOAT');
So its not the heuristic only its the heuristic + something else.
From what I see in this example call:
RETURN QUERY SELECT * FROM issue_3180_variant('pgr_aStarCost', 'pgr_dijkstraCost',
'start_vid, end_vid, agg_cost', '1::BIGINT, 6::BIGINT',
', 0, 1::FLOAT, 1::FLOAT', ', 5, 1::FLOAT, 1::FLOAT');
It can be simplified to:
RETURN QUERY SELECT * FROM issue_3180_variant('pgr_aStarCost', 'pgr_dijkstraCost',
'start_vid, end_vid, agg_cost', '1::BIGINT, 6::BIGINT');
Just because you are not using any other heiristic than 0 or 5
It can be simplified to:
RETURN QUERY SELECT * FROM issue_3180_variant('pgr_aStarCost', 'pgr_dijkstraCost',
'start_vid, end_vid, agg_cost', '1::BIGINT, 6::BIGINT', 0); -- Using the 0 heuristic
Just because the 1::FLOAT, 1::FLOAT is allways used.
| SELECT CASE WHEN min_version('4.1.0') THEN plan(13) ELSE plan(1) END; | ||
|
|
||
| /* builds: SELECT <cols> FROM <fn>('SELECT <incols> FROM <tbl>', <verts>, <flag><extra>) */ | ||
| CREATE OR REPLACE FUNCTION issue_3180_q( |
There was a problem hiding this comment.
A general comment about issue_3180_q and issue_3180_variant
They seem to be repeated code on the three pgtap tests.
if its some functionality you need for more than one function the appropriate place in a file on tools/teststers. (please that is for general tests so no mention of issue_3180 is to be used.
Can be asymmetrical_tests.sql
| /* :file: This file is part of the pgRouting project. | ||
| :copyright: Copyright (c) 2026-2026 pgRouting developers | ||
| :license: Creative Commons Attribution-Share Alike 3.0 https://creativecommons.org/licenses/by-sa/3.0 */ | ||
|
|
There was a problem hiding this comment.
I Do like your tests,
They need work , please no AI involved.
| msgid ":issue:`3101`: pgr_edgeColoring not building graph correctly" | ||
| msgstr "" | ||
|
|
||
| msgid ":issue:`3180`: On undirected graphs where cost and reverse_cost differ, pgr_aStar, pgr_aStarCost, pgr_aStarCostMatrix, pgr_bdAstar, pgr_bdAstarCost, pgr_bdAstarCostMatrix, pgr_bdDijkstra, pgr_bdDijkstraCost and pgr_bdDijkstraCostMatrix returned every node and edge twice, with both costs added up" |
| /* TODO(v4) add correct predecessor node */ | ||
| path.push_back({m_end_id, -1, 0, 0, 0}); | ||
| } else { | ||
| /* |
There was a problem hiding this comment.
I (sorta) like your solution,
Where the problem is the the Path class is not catching the min parallel cost.
The sorta is because the Path has serious issues that have not been addressed, and the non spoken plan is to get rid of this class. And you are not going to address any issue on this class or attempt to get rid of this class.
I don't like your comment.
Self documented code is important.
You are talking about edges with the same id, what will happen when there are parallel edges with different id? and the other edge has a lower cost?
|
Closing this, since #3196 and #3197 fixes #3180 in a cleaner way: it keeps only the cheapest edge between two vertices when the graph is built, so parallel edges with different ids are handled too, and the Path class stays untouched. Thank you @cvvergara and @robe2 for reviewing my PR. The comments on self-documenting tests and on moving shared helpers to tools/testers were very useful for me. |
Fixes #3180.
Changes proposed
include/cpp_common/path.hppto generate only one output row per path step when rebuilding a path, instead of adding a row for every edge with the same ID.pgtap/astar/edge_cases/issue_3180.pg,pgtap/bdAstar/edge_cases/issue_3180.pgandpgtap/bdDijkstra/edge_cases/issue_3180.pg, covering the nine affected functions.doc/src/release_notes.rstandNEWS.md.How I found the issue
While testing an undirected graph, I noticed that
pgr_bdDijkstrawas returning a higher total cost thanpgr_dijkstrafor the same route.I reproduced the issue using a small chain of six vertices and five edges. After comparing their results, I checked the other functions in the
aStar,bdAstarandbdDijkstrafamilies and found the same problem.Why it happens
On an undirected graph, when
costandreverse_costare different (and both are non-negative), pgRouting stores two internal edges with the same ID. This behaviour is documented indoc/src/pgRouting-concepts.rst.The affected functions rebuild their paths using
Path(graph, original, only_cost). During this process, the code looks for edges matching the current edge ID but adds a result row for every match instead of selecting just one.As a result, each step appears twice (once with cost 7 and once with cost 2), so both costs are added to the total.
pgr_dijkstrais not affected because it builds its path differently.What I changed
I updated the loop in
path.hppto select only one matching edge for each path step.It first looks for an edge whose cost matches the cost used for that step. If no exact match is found, it selects the cheapest matching edge.
This follows the same selection logic already used by
get_edge_idand keeps the original graph representation unchanged.Test results
I tested the fix using a chain of five edges, each with
cost = 7andreverse_cost = 2. Both tables below hold the same 5 edges; only the table name differs.Before the fix
Query:
Actual output:
pgr_bdDijkstrareturned 11 rows instead of 6, with a final accumulated cost of 45 instead of 10.After the fix
Query:
Actual output:
After the fix,
pgr_bdDijkstrareturns the expected 6 rows with a total cost of 10, matchingpgr_dijkstra.Additional testing
cost = reverse_costcontinue to pass before and after the change.astar,bdAstarandbdDijkstra. No.resultfiles changed.pgr_dijkstra,pgr_KSPandpgr_dijkstraVia). All 12 produced matching results on the tested graph.pgr_dijkstra/pgr_dijkstraCost.GitHub Actions for the last commit: https://github.com/AdityaRanaX/my-pgrouting/actions
Not included
pgr_bdAstarhas some separate issues that I plan to report in another issue.The Update Locale bot also added one commit containing locale-file updates.
@pgRouting/admins
Summary by CodeRabbit