Routing API with avoid values

Hello!

It seems like despite setting the avoid query parameter to &avoid=motorway does not actually modify a route with a clear alternative. Note the vehicle type is set to car in the query params, so that shouldn’t be the problem.

This does not seem to work either for any avoid combination, i.e. &avoid=ferry;motorway;tunnel.

There is no apparent error; it just doesn’t avoid motorways, like it should.

Went over the documentation a few times:
https://docs.graphhopper.com/#operation/getRoute


Example

Trying to route from 45.506129,-73.693729 to 45.503716,-73.634744.

With avoid=motorway:
https://graphhopper.com/api/1/route?key=YourApiKey&point=45.506129,-73.693729&point=45.503716,-73.634744&vehicle=car&locale=en&instructions=true&calc_points=true&points_encoded=false&avoid=motorway

Without any avoid query param:
https://graphhopper.com/api/1/route?key=YourApiKey&point=45.506129,-73.693729&point=45.503716,-73.634744&vehicle=car&locale=en&instructions=true&calc_points=true&points_encoded=false

Note: Replace YourApiKey with, well… your GH API Key to try it.

In both instances, paths[0].distance and paths[0].time are the same; which really it should not be:

distance: 6679.333,
weight: 845.092705,
time: 744861,

A diff of the two API responses show that they are exactly the same.

You can try to do this same route in Google Maps and toggle highways on and off, and you’ll get completely different paths.


Has this been reported already? Any suggestions?

Thank you!

Currently it is required to add &ch.disable=true.

Also please note that the parameter is just a hint to the algorithm: it tries to avoid it if possible but if the detour would be too big it will still use the motorway.

1 Like

@karussell How does it determine a detour that would be “too big”?

The cost of the road segment for that avoidance parameter will be 10 or more times higher. The details will change though. It might also happen that we find a way to strictly exclude a certain road class but at the moment this can result in too long response times and so we have to do a certain upper limit.

If you still want this strict exclusion or need to avoid certain roads for the Route Optimization or Matrix API, then you would currently need a dedicated setup where we take care of your properties while data import, but they cannot be changed per requests.

Alright, that’s good information. I did add &ch.disable=true to my query parameters, and it worked as expected. :slightly_smiling_face:

If you still want this strict exclusion or need to avoid certain roads for the Route Optimization or Matrix API, then you would currently need a dedicated setup where we take care of your properties while data import, but they cannot be changed per requests.

So based on what you’re saying, if we were to use a dedicated setup, the avoidance parameters would be “hard-coded” and we wouldn’t be able to change them in the POST body of our request to /vrp/optimize?

From what I understood from an email exchange I had with Stefan, generally the Route Optimization API doesn’t have avoidance parameters because:

… we need to calculate travel time matrices for the Route Optimization API very fast. To achieve high performance, we use the Contraction Hierarchies (CH) algorithm. However, if you want to use “avoid” per request you need to disable CH. Currently, this would be too slow for the Route Optimization API.

But if we were to use a dedicated setup, how bad would it be to disable CH on the Route Optimization API and have flexible avoidance parameters in our POST body? Would the slowdown still be that significant?

If it makes any difference, our Route Optimization requests are, on average, between 20 to 50 stops at this time.

Thanks a lot for taking the time to answer these questions!

yes

We can support both with two custom vehicle profiles.

how bad would it be to disable CH on the Route Optimization API and have flexible avoidance parameters in our POST body?

So bad that we do not support it. So the only option is to have a dedicated setup with two custom profiles. Of course we are working on making this more flexible but this will take months or even years.

1 Like