Repository navigation
Fix #3180: one row per step on undirected edges with different costs #3191
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
2388e69
6f07db7
889dd2b
028e8a1
b8d68a7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3798,6 +3798,9 @@ msgstr "" | |
| 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" | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The issue title is not that |
||
| msgstr "" | ||
|
|
||
| msgid "4.1.0 Requirements" | ||
| msgstr "" | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,124 @@ | ||
| /* :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 */ | ||
|
|
||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I Do like your tests, |
||
|
|
||
| /* | ||
| * Issue #3180: undirected graph where cost and reverse_cost differ | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 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 | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is about pgr_star why do you mention all three functions? |
||
| * 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 | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do you mention pgr_dijkstra if it does not have a problem? |
||
| * reference here. | ||
| * | ||
| * The chain below costs 7 one way and 2 the other, so the cheapest route | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What do you mean by chain? |
||
| * from 1 to 6 uses each edge once at cost 2: 6 rows and agg_cost 10. | ||
| * The nodes sit on a line one unit apart, so the heuristic never overestimates. | ||
| */ | ||
|
|
||
| BEGIN; | ||
|
|
||
| SET extra_float_digits=-3; | ||
|
|
||
| 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( | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A general comment about issue_3180_q and issue_3180_variant |
||
| fn TEXT, cols TEXT, incols TEXT, tbl TEXT, verts TEXT, flag TEXT, extra TEXT) | ||
| RETURNS TEXT AS | ||
| $BODY$ | ||
| SELECT 'SELECT ' || cols || ' FROM ' || fn || '(''SELECT ' || incols | ||
| || ' FROM ' || tbl || ''', ' || verts || ', ' || flag || extra || ')'; | ||
| $BODY$ | ||
| LANGUAGE sql IMMUTABLE; | ||
|
|
||
| /* the same four checks for one function: the bug, then the two controls */ | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe here is a good opportunity to mention what is not self documented now the sentence: Note: The self documentation of what each query does its on the pgtap comment |
||
| CREATE OR REPLACE FUNCTION issue_3180_variant( | ||
| fn TEXT, ref TEXT, cols TEXT, verts TEXT, h0 TEXT, h5 TEXT) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. hmm so reading forward down
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. verts = hmm, so they are the parameters they are not precisely vertices. `1::BIGINT, 6::BIGINT',
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. for h0 and h5 I allways see on the function call: So its not the heuristic only its the heuristic + something else. From what I see in this example call: It can be simplified to: Just because you are not using any other heiristic than 0 or 5 It can be simplified to: Just because the |
||
| RETURNS SETOF TEXT AS | ||
| $BODY$ | ||
| DECLARE | ||
| inc TEXT := 'id, source, target, cost, reverse_cost, x1, y1, x2, y2'; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Self documented variable name: |
||
| rinc TEXT := 'id, source, target, cost, reverse_cost'; | ||
| BEGIN | ||
| RETURN QUERY SELECT set_eq( | ||
| issue_3180_q(fn, cols, inc, 'edges_3180', verts, 'false', h0), | ||
| issue_3180_q(ref, cols, rinc, 'edges_3180', verts, 'false', ''), | ||
| fn || ': undirected must match ' || ref || ' (agg_cost 10)'); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You are not doing a comparison of exclusively the last row's agg_cost so |
||
|
|
||
| IF h5 IS NOT NULL THEN | ||
| RETURN QUERY SELECT set_eq( | ||
| issue_3180_q(fn, cols, inc, 'edges_3180', verts, 'false', h5), | ||
| issue_3180_q(ref, cols, rinc, 'edges_3180', verts, 'false', ''), | ||
| fn || ': undirected with heuristic 5 must match ' || ref || ' (agg_cost 10)'); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as above, but the above one does not mention the heuristic used, |
||
| END IF; | ||
|
|
||
| RETURN QUERY SELECT set_eq( | ||
| issue_3180_q(fn, cols, inc, 'edges_3180', verts, 'true', ''), | ||
| issue_3180_q(ref, cols, rinc, 'edges_3180', verts, 'true', ''), | ||
| fn || ': control, directed matches ' || ref || ' (agg_cost 35)'); | ||
|
|
||
| RETURN QUERY SELECT set_eq( | ||
| issue_3180_q(fn, cols, inc, 'edges_3180_sym', verts, 'false', ''), | ||
| issue_3180_q(ref, cols, rinc, 'edges_3180_sym', verts, 'false', ''), | ||
| fn || ': control, cost = reverse_cost matches ' || ref || ' (agg_cost 35)'); | ||
| END; | ||
| $BODY$ | ||
| LANGUAGE plpgsql; | ||
|
|
||
| CREATE OR REPLACE FUNCTION issue_3180_tests() | ||
| RETURNS SETOF TEXT AS | ||
| $BODY$ | ||
| BEGIN | ||
|
|
||
| IF NOT min_version('4.1.0') THEN | ||
| RETURN QUERY SELECT skip(1, 'pgr_aStar: Fix implemented on 4.1.0'); | ||
| RETURN; | ||
| END IF; | ||
|
|
||
| CREATE TEMP TABLE edges_3180 AS | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why are you creating the table within the function? |
||
| 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), | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is a more simpler way to write the rows 2 to 5.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So, this table has all the edges have same cost & reverse cost |
||
| (3::BIGINT, 3::BIGINT, 4::BIGINT, 7::FLOAT, 2::FLOAT, 2::FLOAT, 0::FLOAT, 3::FLOAT, 0::FLOAT), | ||
| (4::BIGINT, 4::BIGINT, 5::BIGINT, 7::FLOAT, 2::FLOAT, 3::FLOAT, 0::FLOAT, 4::FLOAT, 0::FLOAT), | ||
| (5::BIGINT, 5::BIGINT, 6::BIGINT, 7::FLOAT, 2::FLOAT, 4::FLOAT, 0::FLOAT, 5::FLOAT, 0::FLOAT) | ||
| ) AS t(id, source, target, cost, reverse_cost, x1, y1, x2, y2); | ||
|
|
||
| /* the same chain with one weight for both directions */ | ||
| CREATE TEMP TABLE edges_3180_sym AS | ||
| SELECT id, source, target, cost, cost AS reverse_cost, x1, y1, x2, y2 FROM edges_3180; | ||
|
|
||
| RETURN QUERY SELECT * FROM issue_3180_variant('pgr_aStar', 'pgr_dijkstra', | ||
| 'seq, node, edge, cost, agg_cost', '1::BIGINT, 6::BIGINT', | ||
| ', 0, 1::FLOAT, 1::FLOAT', ', 5, 1::FLOAT, 1::FLOAT'); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Difficult to understand what you are passing to the variant function. |
||
|
|
||
| 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'); | ||
|
|
||
| RETURN QUERY SELECT * FROM issue_3180_variant('pgr_aStarCostMatrix', 'pgr_dijkstraCostMatrix', | ||
| 'start_vid, end_vid, agg_cost', 'ARRAY[1, 6]::BIGINT[]', | ||
| ', 0, 1::FLOAT, 1::FLOAT', ', 5, 1::FLOAT, 1::FLOAT'); | ||
|
|
||
| /* one row per step: an edge id must not appear twice */ | ||
| RETURN QUERY SELECT is( | ||
| (SELECT count(*) - count(DISTINCT edge) FROM pgr_aStar( | ||
| 'SELECT id, source, target, cost, reverse_cost, x1, y1, x2, y2 FROM edges_3180', | ||
| 1::BIGINT, 6::BIGINT, false, 0, 1::FLOAT, 1::FLOAT) WHERE edge <> -1), | ||
| 0::BIGINT, | ||
| 'pgr_aStar: no edge id appears twice on the undirected route'); | ||
|
|
||
| DROP TABLE edges_3180; | ||
| DROP TABLE edges_3180_sym; | ||
|
|
||
| END; | ||
| $BODY$ | ||
| LANGUAGE plpgsql; | ||
|
|
||
| SELECT issue_3180_tests(); | ||
|
|
||
| SELECT * FROM finish(); | ||
| ROLLBACK; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,124 @@ | ||
| /* :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 */ | ||
|
|
||
|
|
||
| /* | ||
| * Issue #3180: undirected graph where cost and reverse_cost differ | ||
| * | ||
| * 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_bdAstar, pgr_bdAstarCost and pgr_bdAstarCostMatrix 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 | ||
| * reference here. | ||
| * | ||
| * The chain below costs 7 one way and 2 the other, so the cheapest route | ||
| * from 1 to 6 uses each edge once at cost 2: 6 rows and agg_cost 10. | ||
| * The nodes sit on a line one unit apart, so the heuristic never overestimates. | ||
| */ | ||
|
|
||
| BEGIN; | ||
|
|
||
| SET extra_float_digits=-3; | ||
|
|
||
| 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( | ||
| fn TEXT, cols TEXT, incols TEXT, tbl TEXT, verts TEXT, flag TEXT, extra TEXT) | ||
| RETURNS TEXT AS | ||
| $BODY$ | ||
| SELECT 'SELECT ' || cols || ' FROM ' || fn || '(''SELECT ' || incols | ||
| || ' FROM ' || tbl || ''', ' || verts || ', ' || flag || extra || ')'; | ||
| $BODY$ | ||
| LANGUAGE sql IMMUTABLE; | ||
|
|
||
| /* 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) | ||
| RETURNS SETOF TEXT AS | ||
| $BODY$ | ||
| DECLARE | ||
| inc TEXT := 'id, source, target, cost, reverse_cost, x1, y1, x2, y2'; | ||
| rinc TEXT := 'id, source, target, cost, reverse_cost'; | ||
| BEGIN | ||
| RETURN QUERY SELECT set_eq( | ||
| issue_3180_q(fn, cols, inc, 'edges_3180', verts, 'false', h0), | ||
| issue_3180_q(ref, cols, rinc, 'edges_3180', verts, 'false', ''), | ||
| fn || ': undirected must match ' || ref || ' (agg_cost 10)'); | ||
|
|
||
| IF h5 IS NOT NULL THEN | ||
| RETURN QUERY SELECT set_eq( | ||
| issue_3180_q(fn, cols, inc, 'edges_3180', verts, 'false', h5), | ||
| issue_3180_q(ref, cols, rinc, 'edges_3180', verts, 'false', ''), | ||
| fn || ': undirected with heuristic 5 must match ' || ref || ' (agg_cost 10)'); | ||
| END IF; | ||
|
|
||
| RETURN QUERY SELECT set_eq( | ||
| issue_3180_q(fn, cols, inc, 'edges_3180', verts, 'true', ''), | ||
| issue_3180_q(ref, cols, rinc, 'edges_3180', verts, 'true', ''), | ||
| fn || ': control, directed matches ' || ref || ' (agg_cost 35)'); | ||
|
|
||
| RETURN QUERY SELECT set_eq( | ||
| issue_3180_q(fn, cols, inc, 'edges_3180_sym', verts, 'false', ''), | ||
| issue_3180_q(ref, cols, rinc, 'edges_3180_sym', verts, 'false', ''), | ||
| fn || ': control, cost = reverse_cost matches ' || ref || ' (agg_cost 35)'); | ||
| END; | ||
| $BODY$ | ||
| LANGUAGE plpgsql; | ||
|
|
||
| CREATE OR REPLACE FUNCTION issue_3180_tests() | ||
| RETURNS SETOF TEXT AS | ||
| $BODY$ | ||
| BEGIN | ||
|
|
||
| IF NOT min_version('4.1.0') THEN | ||
| RETURN QUERY SELECT skip(1, 'pgr_bdAstar: Fix implemented on 4.1.0'); | ||
| RETURN; | ||
| END IF; | ||
|
|
||
| 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), | ||
| (3::BIGINT, 3::BIGINT, 4::BIGINT, 7::FLOAT, 2::FLOAT, 2::FLOAT, 0::FLOAT, 3::FLOAT, 0::FLOAT), | ||
| (4::BIGINT, 4::BIGINT, 5::BIGINT, 7::FLOAT, 2::FLOAT, 3::FLOAT, 0::FLOAT, 4::FLOAT, 0::FLOAT), | ||
| (5::BIGINT, 5::BIGINT, 6::BIGINT, 7::FLOAT, 2::FLOAT, 4::FLOAT, 0::FLOAT, 5::FLOAT, 0::FLOAT) | ||
| ) AS t(id, source, target, cost, reverse_cost, x1, y1, x2, y2); | ||
|
|
||
| /* the same chain with one weight for both directions */ | ||
| CREATE TEMP TABLE edges_3180_sym AS | ||
| SELECT id, source, target, cost, cost AS reverse_cost, x1, y1, x2, y2 FROM edges_3180; | ||
|
|
||
| RETURN QUERY SELECT * FROM issue_3180_variant('pgr_bdAstar', 'pgr_dijkstra', | ||
| 'seq, node, edge, cost, agg_cost', '1::BIGINT, 6::BIGINT', | ||
| ', 0, 1::NUMERIC, 1::NUMERIC', ', 5, 1::NUMERIC, 1::NUMERIC'); | ||
|
|
||
| RETURN QUERY SELECT * FROM issue_3180_variant('pgr_bdAstarCost', 'pgr_dijkstraCost', | ||
| 'start_vid, end_vid, agg_cost', '1::BIGINT, 6::BIGINT', | ||
| ', 0, 1::NUMERIC, 1::NUMERIC', ', 5, 1::NUMERIC, 1::NUMERIC'); | ||
|
|
||
| RETURN QUERY SELECT * FROM issue_3180_variant('pgr_bdAstarCostMatrix', 'pgr_dijkstraCostMatrix', | ||
| 'start_vid, end_vid, agg_cost', 'ARRAY[1, 6]::BIGINT[]', | ||
| ', 0, 1::NUMERIC, 1::NUMERIC', ', 5, 1::NUMERIC, 1::NUMERIC'); | ||
|
|
||
| /* one row per step: an edge id must not appear twice */ | ||
| RETURN QUERY SELECT is( | ||
| (SELECT count(*) - count(DISTINCT edge) FROM pgr_bdAstar( | ||
| 'SELECT id, source, target, cost, reverse_cost, x1, y1, x2, y2 FROM edges_3180', | ||
| 1::BIGINT, 6::BIGINT, false, 0, 1::NUMERIC, 1::NUMERIC) WHERE edge <> -1), | ||
| 0::BIGINT, | ||
| 'pgr_bdAstar: no edge id appears twice on the undirected route'); | ||
|
|
||
| DROP TABLE edges_3180; | ||
| DROP TABLE edges_3180_sym; | ||
|
|
||
| END; | ||
| $BODY$ | ||
| LANGUAGE plpgsql; | ||
|
|
||
| SELECT issue_3180_tests(); | ||
|
|
||
| SELECT * FROM finish(); | ||
| ROLLBACK; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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?