Turn restrictions shortest path (TRSP)

Turn restrictions shortest path is an algorithm that can make use of turn restrictions, so it possible to model real-world scenarios. In terms of performance, it should be close to A*.

For this example, we will focus on a smaller area to demonstrate how to define and use turn restrictions that should be fed to the pgr_trsp function.

Let's first calculate a route from vertex 26306 to vertex 98111:

select * from pgr_trsp('select gid::int4 as id, source::int4, target::int4, length_m::float8 as cost from pgr.ways', 26306, 98111, false, false); 

Our output is as follows:

 Seq|   id1|    id2|                cost
----+------+-------+--------------------
0| 26306| 41122| 0.00282144411605047
1| 51491| 132340|0.000500071364908483
2| 73162| 132341| 0.00714439682618376
3| 26893| 157023| 0.00634007469987476
4| 98111| -1| 0

This is pretty much the same as what we have seen so far. The meanings of the columns are: sequence, vertex ID to start from, edge ID to follow, and cost of traveling from node (id1) using edge (id2).

Now let's create the turn restriction table, as described in the documentation:

CREATE TABLE pgr.restrictions ( 
id serial,
to_cost double precision,
target_id integer,
via_path text
);

And define some restrictions:

INSERT INTO pgr.restrictions (to_cost,target_id,via_path) VALUES (10000, 157023, '132341'); 

The preceding code means that if you are traveling along edge 132341, then the cost of traveling via edge 157023 is going to be 10000.

Let's see if our restriction works:

select * from pgr_trsp('select gid::int4 as id, source::int4, target::int4, cost::float8 as cost from pgr.ways', 26306, 98111, false, false, 'select to_cost, target_id, via_path from pgr.restrictions'); 

And it looks like it does; the sequence of edges to travel has changed:

 Seq|   id1|    id2|                cost
----+------+-------+--------------------
0| 26306| 41122| 0.00282144411605047
1| 51491| 132340|0.000500071364908484
2| 73162| 91344| 0.00321165171991199
3| 39894| 136118| 0.00652205259101692
4| 2717| 66313| 0.00394702020516963
5| 26893| 157023| 0.00634007469987476
6| 98111| -1| 0

It is possible to define more than one edge as the access path:

INSERT INTO pgr.restrictions VALUES (2, 10000, 157023, '66313,136118'); 

This time, what the restriction says is: if you are traveling via edges 136118 - 66313, then the cost of continuing onto 157023 is 10000.

Notice the route chain that defines the access path to the restricted edge comes in reverse order.

Once again, the order of the edges to travel has changed. For a change, instead of showing the tabular data again, let's see how our routes look on a map:

  • Orange route is the route without the turn restrictions; it does indeed look like the shortest path
  • Yellow route was calculated with the first restriction: if coming from 132341, then the penalty of traveling onto 157023 was 10000
  • Green route was calculated with two restrictions - the first one and another one says that traveling onto 157023 from 136118 - 66313 would incur a penalty of 10000
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset