pgRouting / pgRoutingLayer

pgRouting Layer plugin for QGIS
https://qgis.pgrouting.org/
GNU General Public License v2.0
23 stars 18 forks source link

Support v2.1 TRSP(pgr_trsp) via edges #4

Closed sanak closed 7 years ago

sanak commented 9 years ago

Create new function files (trsp_via_nodes.py/trsp_via_edges.py).

sanak commented 9 years ago

I think that via nodes case is no problem. The problem is via edges case. If round trip edge (edge ID=10) is returned twice, then I can generate round trip (go and back) geometry from the result.

 seq | start_v | end_v | vertex | edge | cost | agg_cost 
-----+---------+-------+--------+------+------+----------
   1 |       7 |    10 |     -1 |    6 |  0.5 |        0
   2 |       7 |    10 |      8 |    7 |    1 |      0.5
   3 |       7 |    10 |      5 |   10 |  0.5 |      1.5
   4 |      10 |     2 |     -1 |   10 |  0.5 |      2.0
   5 |      10 |     2 |      5 |    4 |  0.5 |      2.5

By the way, in trsp edge case, above SUM(cost) != MAX(agg_cost). So, I think that agg_cost should be as follows in edge case.

 seq | start_v | end_v | vertex | edge | cost | agg_cost 
-----+---------+-------+--------+------+------+----------
   1 |       7 |    10 |     -1 |    6 |  0.5 |      0.5
   2 |       7 |    10 |      8 |    7 |    1 |      1.5
   3 |       7 |    10 |      5 |   10 |  0.5 |      2.0
   4 |      10 |     2 |     -1 |   10 |  0.5 |      2.5
   5 |      10 |     2 |      5 |    4 |  0.5 |      3.0
cvvergara commented 9 years ago

What about this, is it of any help?

 ...
    ARRAY[6,10,4]::integer[], ARRAY[0.49,0.51,0.51]::float8[]
 ...

Here, -1 represents an id of the vertex that is in edge 6 at postition = 49% from the start of the edge -2 represents an id of the vertex that is in edge 10 at postition = 51% from the start of the edge -3 represents an id of the vertex that is in edge 4 at postition = 51% from the start of the edge

 seq | start_v | end_v | vertex | edge | cost | agg_cost 
-----+---------+-------+--------+------+------+----------
   1 |      -1 |    -2 |     -1 |    6 | 0.49 |        0
   2 |      -1 |    -2 |      8 |    7 |    1 |     0.49
   3 |      -1 |    -2 |      5 |   10 | 0.49 |     1.49
   4 |      -1 |    -2 |     -2 |   -1 |    0 |     1.98
   5 |      -2 |    -3 |     -2 |   10 | 0.49 |     1.98
   6 |      -2 |    -3 |      5 |    4 | 0.49 |     2.47
   7 |      -2 |    -3 |     -3 |   -1 |    0 |     2.96

You can see in row 6 & 7 that we are moving from v5 towards v2 but stopping at v(-3) This is edge 4:

 id | source | target | cost | reverse_cost 
----+--------+--------+------+--------------
  4 |      2 |      5 |    1 |            1

So if you are rendering geometry, its going the opposite direction of the geometry

cvvergara commented 9 years ago

Here are some equivalences in my demo: The via points are the start of the edge

SELECT * FROM pgr_dijkstraViaEdges('
    SELECT id ,
        source,
        target,
        cost, reverse_cost
        FROM edge_table',
    ARRAY[6,10,4]::integer[], ARRAY[0,0,0]::float8[], false);
 seq | start_v | end_v | vertex | edge | cost | agg_cost 
-----+---------+-------+--------+------+------+----------
   1 |       7 |     5 |      7 |    6 |    1 |        0
   2 |       7 |     5 |      8 |    7 |    1 |        1
   3 |       7 |     5 |      5 |   -1 |    0 |        2
   4 |       5 |     2 |      5 |    4 |    1 |        2
   5 |       5 |     2 |      2 |   -1 |    0 |        3
(5 rows)

SELECT * FROM pgr_dijkstraViaVertices('
    SELECT id ,
        source,
        target,
        cost, reverse_cost
        FROM edge_table',
    ARRAY[7,5,2]::integer[], false);
 seq | start_v | end_v | vertex | edge | cost | agg_cost 
-----+---------+-------+--------+------+------+----------
   1 |       7 |     5 |      7 |    6 |    1 |        0
   2 |       7 |     5 |      8 |    7 |    1 |        1
   3 |       7 |     5 |      5 |   -1 |    0 |        2
   4 |       5 |     2 |      5 |    4 |    1 |        2
   5 |       5 |     2 |      2 |   -1 |    0 |        3
(5 rows)

This is doing the same on the trspVia:

SELECT seq, id1 AS path, id2 AS node, id3 AS edge, cost FROM pgr_trspVia('
    SELECT id AS id,
        source::int4 AS source,
        target::int4 AS target,
        cost::float8 AS cost 
        FROM edge_table',
    ARRAY[6,10,4]::integer[], ARRAY[0,0,0]::float8[], false, false, null);
 seq | path | node | edge | cost 
-----+------+------+------+------
   1 |    1 |    7 |    6 |    1
   2 |    1 |    8 |    7 |    1
   3 |    1 |    5 |   -1 |    0
   4 |    2 |    5 |    4 |    1
   5 |    2 |    2 |   -1 |    0
(5 rows)

SELECT seq, id1 AS path, id2 AS node, id3 AS edge, cost FROM pgr_trspVia('
    SELECT id AS id,
        source::int4 AS source,
        target::int4 AS target,
        cost::float8 AS cost 
        FROM edge_table',
    ARRAY[7,5,2]::integer[],  false, false, null);
 seq | path | node | edge | cost 
-----+------+------+------+------
   1 |    1 |    7 |    6 |    1
   2 |    1 |    8 |    7 |    1
   3 |    2 |    5 |    4 |    1
   4 |    2 |    2 |   -1 |    0
(4 rows)
cvvergara commented 9 years ago

Just for pgRouting user readability, In my demo:

pgr_dijkstraViaEdges(
pgr_dijkstraViaVertices('

Vs

pgr_trspVia(   ---need to look at the arguments to see if its via edges
pgr_trspVia(   ---need to look at the arguments to see if its via vertices

I think this will look much more clear when its in a middle of a large function

pgr_trspViaEdges(
pgr_trspViaVertices(
cvvergara commented 9 years ago

@sanak is that what you are looking for?

I made this function pgr_dijkstraViaCompleteEdges

SELECT * FROM pgr_dijkstraViaCompleteEdges(
    'SELECT id , source, target, cost, reverse_cost FROM edge_table',
    ARRAY[6,10,4]::bigint[], false);

and this are the results:

 seq | start_e | end_e | vertex | edge | cost | agg_cost 
-----+---------+-------+--------+------+------+----------
   1 |       6 |    10 |      7 |    6 |    1 |        1
   2 |       6 |    10 |      8 |    7 |    1 |        2
   3 |       6 |    10 |      5 |   10 |    1 |        3
   5 |      10 |     4 |     10 |   10 |    1 |        4
   6 |      10 |     4 |      5 |    4 |    1 |        5
   7 |      10 |     4 |      2 |   -1 |    0 |        5
(6 rows)

Here is a query with fractions:

SELECT seq, id1 AS path, id2 AS node, id3 AS edge, cost FROM pgr_trspVia('
    SELECT id AS id,
        source::int4 AS source,
        target::int4 AS target,
        cost::float8 AS cost 
        FROM edge_table',
    ARRAY[6,10,4]::integer[], ARRAY[0.49,0.51,0.51]::float8[], false, false, null);

 seq | path | node | edge | cost 
-----+------+------+------+------
   1 |    1 |   -1 |    6 | 0.51
   2 |    1 |    8 |    7 |    1
   3 |    1 |    5 |   10 | 1.02
   4 |    2 |    5 |    4 | 0.49
(4 rows)

And another one:

SELECT * FROM pgr_dijkstraViaEdges(
    'SELECT id , source, target, cost FROM edge_table',
    ARRAY[6,10,4]::bigint[], ARRAY[0.49,0.51,0.51]::float8[], false);
 seq | start_v | end_v | vertex | edge | cost | agg_cost 
-----+---------+-------+--------+------+------+----------
   1 |      -1 |    -2 |     -1 |    6 | 0.51 |        0
   2 |      -1 |    -2 |      8 |    7 |    1 |     0.51
   3 |      -1 |    -2 |      5 |   10 | 0.51 |     1.51
   4 |      -1 |    -2 |     -2 |   -1 |    0 |     2.02
   5 |      -2 |    -3 |     -2 |   10 | 0.51 |     2.02
   6 |      -2 |    -3 |      5 |    4 | 0.49 |     2.53
   7 |      -2 |    -3 |     -3 |   -1 |    0 |     3.02
(7 rows)

or maybe the last one do you need it like this:

 seq | start_e | end_e | vertex | edge | cost | agg_cost 
-----+---------+-------+--------+------+------+----------
   1 |      6  |    10 |     -1 |    6 | 0.51 |        0
   2 |      6  |    10 |      8 |    7 |    1 |     0.51
   3 |      6  |    10 |      5 |   10 | 0.51 |     1.51
   4 |      10 |     4 |     -2 |   10 | 0.51 |     2.02
   5 |      10 |     4 |      5 |    4 | 0.49 |     2.53
   6 |      10 |     4 |     -3 |   -1 |    0 |     3.02
(7 rows)
sanak commented 9 years ago

@cvvergara Sorry, I may miss some of your posts, but is the following function for pgrouting >= v2.2 release ?

In my opinion, pgRoutingLayer (v2.1) should use only documented API in v2.1, so if above functions are for future ones, then those should be discussed on not here (pgRoutingLayer issues) but pgrouting issues (https://github.com/pgRouting/pgrouting/issues).

And about the followings, we really need other members (at least, @woodbri and @dkastl) review. (I think that a negative id is quite big changing.) -1 represents an id of the vertex that is in edge 6 at postition = 49% from the start of the edge -2 represents an id of the vertex that is in edge 10 at postition = 51% from the start of the edge -3 represents an id of the vertex that is in edge 4 at postition = 51% from the start of the edge

cvvergara commented 9 years ago

@sanak You are the one that wants pgr_trspVia fixed, for release 2.1 but I don't think Steve is going to do fix it, he is busy in other projects. pgr_trspVia uses the 1 to 1 pgr_trsp and that one has so many bugs that what ever you do in pgr_trspVia, because it uses pgr_trsp it will 1) have inherited bugs from pgr_trsp. 2) have bugs by it self (if any)

about the -1 -2 & -3 ... Its just because there is no original vertex in the middle of those edges, whats the id of those "newly created vertices"

woodbri commented 9 years ago

Yeah, I'm sorry, but there is no way I can make time to look at this for a few months. If there are bugs, then it would be great if someone would open specific issues showing how to reproduce them or add tests that fail.

I most always use pgr_trsp() with edge and position and as far as I can tell this works ok.

There is also code sitting in branch remotes/origin/trsp-improvements waiting to get merged and tested in trsp, that fixes some bugs and builds the graph once then reuses it for multiple via queries to speed things up.

woodbri commented 9 years ago

I think we need to release pgrouting 2.1.0 before Vicky has to leave for FOSS4G. So I think if the changes she has already made are not sufficient then you will probably need to wait till 2.2 after she is back from FOSS4G.

sanak commented 9 years ago

@woodbri, @cvvergara Okay, sorry for bothering. Let's concentrate not this trsp_via mode so much, but release 2.1.0 at first.

cvvergara commented 8 years ago

pgrouting 2.2 has now pgr_dijkstraVia as proposed function. and its being used to wrap trsp_vertex when there are no restrictions.

cvvergara commented 8 years ago

This issue is tagged for 2.1, but it mentions 2 fucntions, I could only manage to improve the via node, but the via edge is too complicated with the data it returns. I will change the title to via edge only and open a new issue of via node. I'll remove the milestone, and leave this issue open because it has a lot of interesting comments for when we work on via node.

cvvergara commented 7 years ago

The problem is not the GUI is TRSP pgRouting code