pgrouting icon indicating copy to clipboard operation
pgrouting copied to clipboard

pgr_ksp doesn't return cheapest paths

Open bernhardbarker opened this issue 8 years ago • 10 comments

Steps to reproduce the problem

Run:

select *
from pgr_ksp(
	'select 1 as id, 1 as source, 2 as target, 1 as cost union
	select 2 , 2 , 3 , 1  union
	select 3 , 3 , 0 , 99 union
	select 4 , 1 , 12, 1  union
	select 5 , 12, 13, 1  union
	select 6 , 12, 14, 1  union
	select 7 , 13, 14, 1  union
	select 8 , 14, 0 , 1',
	1, 0, 2, directed := TRUE);

Expected behavior and actual behavior

I am getting a path with agg_cost 101 when there exists a path not shown with agg_cost 4.

Is this a bug or am I misunderstanding something?

Actual:

seq	path_id	path_se	node	edge	cost	agg_cost
1	1	1	"1"	"1"	"1"	"0"
2	1	2	"2"	"2"	"1"	"1"
3	1	3	"3"	"3"	"99"	"2"
4	1	4	"0"	"-1"	"0"	"101"
5	2	1	"1"	"4"	"1"	"0"
6	2	2	"12"	"6"	"1"	"1"
7	2	3	"14"	"8"	"1"	"2"
8	2	4	"0"	"-1"	"0"	"3"

Expected: (the last path can be seen by increasing k from 2 to 3)

seq	path_id	path_se	node	edge	cost	agg_cost
1	1	1	"1"	"4"	"1"	"0"
2	1	2	"12"	"6"	"1"	"1"
3	1	3	"14"	"8"	"1"	"2"
4	1	4	"0"	"-1"	"0"	"3"
5	2	1	"1"	"4"	"1"	"0"
6	2	2	"12"	"5"	"1"	"1"
7	2	3	"13"	"7"	"1"	"2"
8	2	4	"14"	"8"	"1"	"3"
9	2	5	"0"	"-1"	"0"	"4"

Specifications like the version of pgRouting/PostGIS and PostgreSQL as well as Operating System

PostgreSQL 10.3 on x86_64-apple-darwin14.5.0, compiled by Apple LLVM version 7.0.0 (clang-700.1.76), 64-bit
POSTGIS="2.4.3 r16312" PGSQL="100" GEOS="3.6.2-CAPI-1.10.2 4d2925d6" PROJ="Rel. 4.9.3, 15 August 2016" GDAL="GDAL 2.1.3, released 2017/20/01" LIBXML="2.9.7" LIBJSON="0.12.1" RASTER
pgRouting = (2.5.2,v2.5.2,60585f1f7,master,1.66.0)

I'm on Mac 10.13.3.

bernhardbarker avatar Mar 16 '18 07:03 bernhardbarker

I altered your edges data, a little and found more strange behavior of this function. image It is giving different shortest paths for different values of k(=1, 2), which is quite different from the expected output of the algorithm (yen's algorithm).

codeSG avatar Apr 09 '18 17:04 codeSG

I had the same issue with my uni assignment, the pgr_ksp function with different k values show different results.

Environment: PostgreSQL 9.2.7 on x86_64-redhat-linux-gnu, compiled by gcc (GCC) 4.8.2 20140120 (Red Hat 4.8.2-13), 64-bit on pg_routing 2.4.2

  • When k = 1, where path_id = 1, the result is same with pgr_dijkstra() function, which is what I expect.
  • But when k = 2, where path_id = 1, the result is different, not with the minimal cost.

And by setting heap_paths:=true, I found the results in heap are ordered in ascending order of the number of edges, rather than the cost. The one with minimum cost can be found in heap.

Not sure I can attach the data because of the disclosure terms of uni, but happy to test more with other data/ versions..

kentwang94 avatar May 03 '18 01:05 kentwang94

@kentwang94

Right now I am fixing this bug with the fix, I am getting this results, can you please check

select *
from pgr_ksp(
 'select 1 as id, 1 as source, 2 as target, 1 as cost union
 select 2 , 2 , 3 , 1  union
 select 3 , 3 , 0 , 99 union
 select 4 , 1 , 12, 1  union
 select 5 , 12, 13, 1  union
 select 6 , 12, 14, 1  union
 select 7 , 13, 14, 1  union
 select 8 , 14, 0 , 1',
 1, 0, 2, directed := TRUE);
 seq | path_id | path_seq | node | edge | cost | agg_cost 
-----+---------+----------+------+------+------+----------
   1 |       1 |        1 |    1 |    4 |    1 |        0
   2 |       1 |        2 |   12 |    6 |    1 |        1
   3 |       1 |        3 |   14 |    8 |    1 |        2
   4 |       1 |        4 |    0 |   -1 |    0 |        3
   5 |       2 |        1 |    1 |    4 |    1 |        0
   6 |       2 |        2 |   12 |    5 |    1 |        1
   7 |       2 |        3 |   13 |    7 |    1 |        2
   8 |       2 |        4 |   14 |    8 |    1 |        3
   9 |       2 |        5 |    0 |   -1 |    0 |        4
(9 rows)

select *
from pgr_ksp(
 'select 1 as id, 1 as source, 2 as target, 1 as cost union
 select 2 , 2 , 3 , 1  union
 select 3 , 3 , 0 , 99 union
 select 4 , 1 , 12, 1  union
 select 5 , 12, 13, 1  union
 select 6 , 12, 14, 1  union
 select 7 , 13, 14, 1  union
 select 8 , 14, 0 , 1',
 1, 0, 3, directed := TRUE);
 seq | path_id | path_seq | node | edge | cost | agg_cost 
-----+---------+----------+------+------+------+----------
   1 |       1 |        1 |    1 |    4 |    1 |        0
   2 |       1 |        2 |   12 |    6 |    1 |        1
   3 |       1 |        3 |   14 |    8 |    1 |        2
   4 |       1 |        4 |    0 |   -1 |    0 |        3
   5 |       2 |        1 |    1 |    4 |    1 |        0
   6 |       2 |        2 |   12 |    5 |    1 |        1
   7 |       2 |        3 |   13 |    7 |    1 |        2
   8 |       2 |        4 |   14 |    8 |    1 |        3
   9 |       2 |        5 |    0 |   -1 |    0 |        4
  10 |       3 |        1 |    1 |    1 |    1 |        0
  11 |       3 |        2 |    2 |    2 |    1 |        1
  12 |       3 |        3 |    3 |    3 |   99 |        2
  13 |       3 |        4 |    0 |   -1 |    0 |      101
(13 rows)

cvvergara avatar Jun 22 '18 20:06 cvvergara

@codeSG Can you please post:

  • the data you are using, on that image,
  • The exact query done with the results you got
  • and the expected results and the results please don't use images to post code & results that makes it hard for use to reproduce

cvvergara avatar Jun 22 '18 21:06 cvvergara

@cvvergara

What specifically do you want me to check?

The results you've got are correct.

I got incorrect answers for both versions (2.6.0,v2.6.0,a6226c4,release/2.6,1.65.1) and (2.4.0,v2.4.0,6cf07edf1,master,1.53.0), and this is the result I got:

 seq | path_id | path_seq | node | edge | cost | agg_cost 
-----+---------+----------+------+------+------+----------
   1 |       1 |        1 |    1 |    1 |    1 |        0
   2 |       1 |        2 |    2 |    2 |    1 |        1
   3 |       1 |        3 |    3 |    3 |   99 |        2
   4 |       1 |        4 |    0 |   -1 |    0 |      101
   5 |       2 |        1 |    1 |    4 |    1 |        0
   6 |       2 |        2 |   12 |    6 |    1 |        1
   7 |       2 |        3 |   14 |    8 |    1 |        2
   8 |       2 |        4 |    0 |   -1 |    0 |        3
(8 rows)

 seq | path_id | path_seq | node | edge | cost | agg_cost 
-----+---------+----------+------+------+------+----------
   1 |       1 |        1 |    1 |    1 |    1 |        0
   2 |       1 |        2 |    2 |    2 |    1 |        1
   3 |       1 |        3 |    3 |    3 |   99 |        2
   4 |       1 |        4 |    0 |   -1 |    0 |      101
   5 |       2 |        1 |    1 |    4 |    1 |        0
   6 |       2 |        2 |   12 |    6 |    1 |        1
   7 |       2 |        3 |   14 |    8 |    1 |        2
   8 |       2 |        4 |    0 |   -1 |    0 |        3
   9 |       3 |        1 |    1 |    4 |    1 |        0
  10 |       3 |        2 |   12 |    5 |    1 |        1
  11 |       3 |        3 |   13 |    7 |    1 |        2
  12 |       3 |        4 |   14 |    8 |    1 |        3
  13 |       3 |        5 |    0 |   -1 |    0 |        4
(13 rows)

kentwang94 avatar Jun 23 '18 01:06 kentwang94

In pgRouting 2.6.2, pgr_KSP still suffers from weird behaviour: the paths returned are NOT ordered by cost ( ascending) (this does not imply that they are indeed the shortest paths, as discussed above). Yet, the API documentation states: path_id | BIGINT | Path identifier. The ordering of the paths For two paths i, j if i < j then agg_cost(i) <= agg_cost(j). While this is somewhat cryptic, it implies that the paths should be returned ordered, or at least the numbers will reflect the order.

If we can rely on the ordering, we could test further whether they are indeed the k shortest paths. This way, they can be any paths...

tomkom avatar Apr 17 '19 04:04 tomkom

I can also confirm that between multiple users, we have found inconsistent behaviour of pgr_KSP. Further, when testing with k=3, the third shortest path was not the same as when testing with a large k, say 200 - and the results were inconsistent between users (not replicable).

tomkom avatar Apr 29 '19 06:04 tomkom

@tomkom At least for your first comment we need to reproduce Plese send us a link to some example data to be reproduce the problem

cvvergara avatar May 02 '19 14:05 cvvergara

@cvvergara Early you wrote that you had a fix? Was that merged? If so which commit in master?

snow-abstraction avatar Dec 22 '19 21:12 snow-abstraction

@snow-abstraction

DROP EXTENSION
CREATE EXTENSION
psql (12.2 (Ubuntu 12.2-2.pgdg18.04+1), server 12.3 (Ubuntu 12.3-1.pgdg18.04+1))
You are now connected to database "sampledata" as user "vicky".
sampledata=# select pgr_version();                                     
 pgr_version 
-------------
 3.0.0
(1 row)

sampledata=# select *                                                  
from pgr_ksp(
 'select 1 as id, 1 as source, 2 as target, 1 as cost union
 select 2 , 2 , 3 , 1  union
 select 3 , 3 , 0 , 99 union
 select 4 , 1 , 12, 1  union
 select 5 , 12, 13, 1  union
 select 6 , 12, 14, 1  union
 select 7 , 13, 14, 1  union
 select 8 , 14, 0 , 1',
 1, 0, 2, directed := TRUE);
 seq | path_id | path_seq | node | edge | cost | agg_cost 
-----+---------+----------+------+------+------+----------
   1 |       1 |        1 |    1 |    4 |    1 |        0
   2 |       1 |        2 |   12 |    6 |    1 |        1
   3 |       1 |        3 |   14 |    8 |    1 |        2
   4 |       1 |        4 |    0 |   -1 |    0 |        3
   5 |       2 |        1 |    1 |    4 |    1 |        0
   6 |       2 |        2 |   12 |    5 |    1 |        1
   7 |       2 |        3 |   13 |    7 |    1 |        2
   8 |       2 |        4 |   14 |    8 |    1 |        3
   9 |       2 |        5 |    0 |   -1 |    0 |        4
(9 rows)

On version 3.0.0

cvvergara avatar Jul 22 '20 11:07 cvvergara