2

I am currently developing application that want to use the fastest time to arrive as parameter for the routing. And i know that pgRouting has 'cost_s' column which calculated from length_m and maxspeed column. The algorithm i use on my application is A*, and i import the osm file with osm2pgrouting. I can implement the parameter on the algorithm, but i think the result is weird, here's the normal result with cost column as cost:

enter image description here

The code i use is this:

SELECT a.seq AS seq, b.gid AS gid, b.name AS name, a.cost AS cost, b.the_geom AS geom, b.source, b.target, b.x1 AS x, b.y1 AS y FROM pgr_astar('
    SELECT gid::integer AS id,
         source::integer,
         target::integer,
         cost::double precision AS cost,
         reverse_cost::double precision AS reverse_cost,
         x1, y1, x2, y2
        FROM ways',
    116452, 60644, true, true) AS a LEFT JOIN ways AS b ON (a.id2 = b.gid) ORDER BY a.seq;

And here's the route from using cost_s column as cost parameter: enter image description here

The code i use is this:

SELECT a.seq AS seq, b.gid AS gid, b.name AS name, a.cost AS cost, b.the_geom AS geom, b.source, b.target, b.x1 AS x, b.y1 AS y FROM pgr_astar('
    SELECT gid::integer AS id,
         source::integer,
         target::integer,
         cost_s::double precision AS cost,
         reverse_cost::double precision AS reverse_cost,
         x1, y1, x2, y2
        FROM ways',
    116452, 60644, true, true) AS a LEFT JOIN ways AS b ON (a.id2 = b.gid) ORDER BY a.seq;

How can be the route with cost_s parameter(which using shorter time to arrive) take farther/longer route than the normal cost route?

  • 1
    Hard to tell from the images. What is the total length of those two routes, in terms of comparable cost? – John Powell Mar 30 '17 at 7:14
  • You may want to use a tool like QGIS to show the costs of each road link colored to find suspicious values. From the screenshot it seems that you're routing through a park eventually. When you use length as cost it will find the shortest path, but when traversing speed of some way in the park is set as too high, then it may not choose that route. – dkastl Mar 30 '17 at 12:56
  • @JohnBarça the total cost of the first route (cost as parameter) is 0.0517855310609797 , and the 2nd route (cost_s as parameter) is 204.474940793924 . – Ridho perdana Mar 31 '17 at 0:19
  • @dkastl the result from using length_m and cost_s parameter was still the same. The route still take the longer way than using cost as parameter. Is it still because the speed? – Ridho perdana Mar 31 '17 at 0:19
  • There are 2 possibilities if you get a different route than expected: 1) you have some disconnections in your network, but this is probably not the case, because you found a route with different costs. Or 2) your costs are the cause for the route. There is no magic: all shortest path algorithms minimize costs. Can you once try without reverse_cost, because eventually there are some negative reverse costs, which will be treated then as oneway. And you could try pgr_dijkstra, if it gives you a different result or the same. – dkastl Mar 31 '17 at 13:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.