User/request specific Edge Weighting?

Is it possible to setting edge weightings on a per-request basis? I can see how to change edge weightings dynamically for all users (for example to incorporate real-time traffic conditions), but am wondering if there is an easy (or hard!) way to do this only for a given request.

My use-case is incorporating user-specific preferences into a route, for example to avoid (or preferentially favor) a favorite or hated road.

I’m able to delve into the code myself to customize it, but wanted to get a feel first for whether it is feasible and tips on when to start if it is.


Yes, it’s possible. My understanding is that you’d need to use ‘flexible mode’ for this (i.e. not LM or CH), which will be fine for shorter routes but may get sluggish if you’re looking a routes in the hundreds of kilometers.

You’ll need to create your own Weighting (override createWeightingFactory in the GraphHopper class). Then, just return a lower weight for your preferred edge(s). You can load/store your preferred edges however you want - e.g. via a flat file somewhere, or from a database, or from the request itself. But obviously you should make sure it’s a very fast lookup, otherwise routing will be very slow.

A good place to start would be to sublass graphhopper/ at master · graphhopper/graphhopper · GitHub , and it’s calcEdgeWeight that you’ll want to override.

This is great info, thank you @Sam_Crawford , clearly enough to make it worth delving in and having a go!

LM also works if you want to change the preferences per-request. You only have to make sure that you only increase the edge weight, but this is usually not a problem. Also we already considered this limitation in the custom weighting. I.e. if you use that you can use LM and won’t have a problem with the algorithm. Further you can use the custom model and create a CH preparation from it.

A good place to start would be to sublass

It is highly recommended to use the custom model and e.g. custom EncodedValues instead of subclassing classes. This way you have all the custom weighting features in-built.


Just for completeness, the custom weighting documentation can be found here: graphhopper/ at master · graphhopper/graphhopper · GitHub

Should users be able to change the edge weights for any edge or do you know which edges might be adjusted by users in advance? You probably need to enumerate the edges somehow (create an EncodedValue containing the edge ID?!) so they can be referenced in the custom model.

1 Like
Powered by Discourse