Project-OSRM / osrm-backend

Open Source Routing Machine - C++ backend
http://map.project-osrm.org
BSD 2-Clause "Simplified" License
6.42k stars 3.4k forks source link

Is there any way to compute multiple source-destination pairs in a single request? #6686

Closed mkumarb closed 8 months ago

mkumarb commented 1 year ago

As part of OSRM table API, we can pass multiple sources & destinations and in turn we get back a matrix of sources * destinations.

Is there any way I can pass only the pairs that I want OSRM to compute & fetch distance/durations for. For example (used JSON as a representation for easier understanding):

{
    "sourceDestinationPairs": [
        {
            "source": {
                "lat": 12.77,
                "lng": 88.76
            },
            "destination": {
                "lat": 13.77,
                "lng": 86.76
            }
        },
        {
            "source": {
                "lat": 13.77,
                "lng": 77.76
            },
            "destination": {
                "lat": 14.77,
                "lng": 77.76
            }
        }
    ]
}

as opposed to the existing table API which accepts input as below:

{
    "sources": [
        {
            "lat": 12.77,
            "lng": 88.76
        },
        {
            "lat": 13.77,
            "lng": 77.76
        }
    ],
    "destinations": [
        {
            "lat": 13.77,
            "lng": 86.76
        },
        {
            "lat": 14.77,
            "lng": 77.76
        }
    ]
}

In the existing table API, I get 2 * 2 = 4 combinations whereas I only want the distance/duration for the 2 pairs specified as part of sourceDestinationPairs

I could not find any API which returns distance/durations only for the specified source & destination pairs. Is this not added due to any potential performance issues?

danpat commented 1 year ago

The table API doesn't support what you describe. It does have NxM support using the sources and destinations parameters (e.g. 3x10, or 8x3), but if all you want is an arbitrary list of pairs, then it'll still return more than what you want.

What you're asking for is what I'd call a "sparse matrix". I'm not sure the API response for /table is particularly efficient for this, it would be full of a lot of null entries and just a few useful values. I suppose as long as it's not too many pairs, it'd be ok, but it wouldn't scale very nicely to large pair lists.

I seem to remember writing up a description for an API like this a long time ago, but I can't find it. From memory, my suggestion was to add pairs=A,B;D,E;... to the API, mutually exclusive with sources/destinations.

Either way, someone would need to implement it - we don't have it today.

mkumarb commented 1 year ago

Thank you for your response @danpat . In our case, we have a use case where we compute N N matrix where all we need is N pairs of distances. N N seems like too much computation for our use case and also slows down the response times as a result. So, we were looking to optimize this by just using pairs Do you see any concerns if we try & provide support for pairs?

jcoupey commented 1 year ago

we compute N * N matrix where all we need is N pairs of distances.

Looks like the table endpoint is overkill for your use-case. Did you simply try to run N independent route requests across the required pairs? Would probably be worth benchmarking this approach as it is much simpler than adjusting the existing code for table, plus you could run a lot of the requests in parallel.