Skip to content

Extract profile selection - #1949

Merged
easbar merged 23 commits into
masterfrom
profile_resolver
Mar 12, 2020
Merged

easbar merged 23 commits into
masterfrom
profile_resolver

Conversation

@easbar

@easbar easbar commented Mar 8, 2020

Copy link
Copy Markdown
Member

This PR aims to extract all the code that is related to selecting the routing profile using the different request parameters like vehicle and weighting. The idea is that here we put all this code into a single place (ProfileResolver) and as a follow-up we can then put this ProfileResolver in front of GraphHopper#calcPaths etc. (c.f. #1859).

Fixes #1800.

@easbar
easbar requested a review from karussell March 8, 2020 22:51
// to allow e.g. changing the u_turn_costs per request (we have to use the minimum weight settings (
// = no turn costs) for the preparation)
// todonow: is it too ugly this way?
Weighting weighting = createWeighting(profile, new PMap().put("__disable_turn_costs", true));

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any opinion here? createWeighting normally uses a DefaultTurnCostProvider if profile.isTurnCosts()==true. But for LM we always want to enforce not using turn costs for the preparation, but to me it seemed like this does not justify adding an extra parameter, so I am using the pmap workaround here...

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would not do this here and just force the usage of no turn cost provider in LM. IMO this is something implementation specific?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you mean? You mean we should always set turn_costs: false for all profiles with LM? But then we cannot use turn costs with LM...

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah you mean somehow disabling the turn costs in PrepareLandmarks? Yes that would be the right thing to do, but how? PrepareLandmarks just gets a Weighting, but how would it disable the turn costs then?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah you mean somehow disabling the turn costs in PrepareLandmarks?

Yes. Isn't it already ignored when we do traversalMode = TraversalMode.NODE_BASED in LandmarkStorage?

@easbar easbar Mar 10, 2020 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yes thats a good point. Yes that should fix it 👍 -> done

FlagEncoder encoder = encodingManager.getEncoder(vehicle);
HintsMap hintsMap = new HintsMap();
RouteResource.initHints(hintsMap, uriInfo.getQueryParameters());
hintsMap.put(Parameters.CH.DISABLE, true);

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it fine to overwrite ch/lm.disable here or should we throw an error?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't the none-CH case the default now? But yeah, I think I had to do this for the /custom endpoint too.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, no graphHopper.resolveProfile(hintsMap) will give us a CH profile or error if it does not exist, so we have to explicitly disable CH/LM to obtain the profile no matter whether a CH/LM profile for these hints exists or not.

@karussell karussell Mar 10, 2020 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, if there is a profile without CH, then this will work without the parameter, but if the profile is listed in profiles_ch then we need the disabling parameter?

The problem is, what if we add a new algorithm xy? Then in theory I would expect that the profile selection remains unchanged and we only specify profiles_xy: [car_xy], i.e. an existing profile car will still work without the algorithm xy. Not sure if this is good as it increases the profile count further.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, if there is a profile without CH, then this will work without the parameter, but if the profile is listed in profiles_ch then we need the disabling parameter?

Yes, or to put it differently if we put ch.disable=true as we do here we get the profile matching our hints no matter what LM/CH preparations there are (and for isochrones we are not even using them). But you are right the profile selection depends on the available LM/CH preparations (it always did).

The problem is, what if we add a new algorithm xy? Then in theory I would expect that the profile selection remains unchanged

But why? If there is a new algorithm we likely want to use it? If the selection remained unchanged what would the new algorithm be good for? In this sense I think its better not to expose the algorithm selection such that it can always be changed internally. As a user of the API I am generally not interested which algorithm is used to solve my problem. What I think is problematic though, that sometimes it can be hard to figure out which algorithm needs to be used, because for certain parameters only certain algorithms work (for example blocked areas etc.).

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, regarding the parameter setting and I think I better understand this now: we have to do it here to avoid a failure if LM or CH is configured.

Boolean edgeBased = getEdgeBased(hintsMap);
Integer uTurnCosts = getUTurnCosts(hintsMap);
return (edgeBased == null || p.isEdgeBased() == edgeBased) &&
(uTurnCosts == null || p.getWeighting().getTurnCostProvider().getName().equals(uTurnCosts.toString())) &&

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not like how we rely on the turn cost provider name to contain the u-turn costs here, but so far I do not have a better idea to do this. Also this is only needed to cover a (probably exotic) case: Multiple edge-based profiles with different u-turn costs. Once we have the profile request parameter we 'only' need to support this u-turn costs comparison here for legacy reasons, so this makes me even more want to get rid of it.

@karussell karussell Mar 10, 2020 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, probably we have to accept that ProfileResolver is mainly for backward compatibility? The bigger problem is the new getTurnCostProvider method, which I would prefer to be private. But I also do not see how.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I would like to remove/not add getTurnCostProvider as well. But how else can we check what u-turn costs a weighting is using? Maybe we can skip this selection rule and throw an error if there are multiple edge-based weightings for the given weighting and vehicle. In this case one would have to use the profile parameter when using multiple edge-based CH preparations with different u-turn costs.

@easbar easbar Mar 10, 2020 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, probably we have to accept that ProfileResolver is mainly for backward compatibility?

Yes absolutely, maybe not yet in this PR but ProfileResolver only exists for backward-compatibility.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this case one would have to use the profile parameter when using multiple edge-based CH preparations with different u-turn costs.

This sounds not too bad.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

createWeighting(profile, new PMap());
} catch (IllegalArgumentException e) {
// todonow: update, what are we really checking here?
throw new IllegalArgumentException("The profile '" + profile.getName() + "' was configured with an unknown weighting '" + profile.getWeighting() + "'");

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In #1776 I stumbled over this check as I wanted to inject the graph into the Weighting but at this time graph is null.

But with #1950 I found a good solution so that I kept this method here :)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah ok. The graph being null problem when running this check should also be already fixed with this: 639bd9a

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aha, where did you stumble over this?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well its required for this branch because the turn cost provider is created in createWeighting now (and thus there is a call to ghStorage.getTurnCostStorage()).

@easbar
easbar merged commit 1fff78a into master Mar 12, 2020
@easbar
easbar deleted the profile_resolver branch March 12, 2020 07:26
@karussell

Copy link
Copy Markdown
Member

@easbar I just noticed when I merged this PR into another PR: why has the hopper.createWeighting(profileConfig, hints) method a separate hints parameter when the profileConfig also has a hints? Is this identical and we can remove hints from createWeighting?

@easbar

easbar commented Mar 12, 2020

Copy link
Copy Markdown
Member Author

No its not identical. The idea is that hints are the hints we get from a request (in addition to the selected profile). For example in case we select a profile with short_fastest weighting the hints parameter might contain/overwrite the distance factor from this profile. However, we need to restrict this to certain cases (for example the distance factor cannot be overwritten for CH). See also the javadocs of GraphHopper#createWeighting (but maybe they need to be a bit more clear)

@karussell

Copy link
Copy Markdown
Member

Ok, I see. So all hints unrelated to the profile like which algo (e.g. ch/lm.disable) go into that extra hint.

@easbar

easbar commented Mar 12, 2020

Copy link
Copy Markdown
Member Author

Yes, exactly. But we might have to add some 'merging' rules (like take the distance factor from profile.hints, but if hints also has it overwrite it). This will be similar to using server-custom-models and overwriting with request-custom-models?

@karussell

karussell commented Mar 12, 2020 •

Copy link
Copy Markdown
Member

But we might have to add some 'merging' rules (like take the distance factor from profile.hints, but if hints also has it overwrite it).

Hmmh, this is a bit ugly. I think we should either make the difference really strict or use only one HintsMap. Because otherwise we end up looking into two maps for all internal usage.

For CustomModel this is a bit different as we can do the merging part without internal knowledge because the "CustomRequest" is automatically serialized into a CustomModel via jackson as it has only up-front-known parameters. For example:

hints: {ch.disable: true, distance_factor=0}
profile.hints: {}
=> merged_hints: how can we do this without internal knowledge about a specific profile?

custom request: { vehicle_weight: 50, ch.disable=true }
=> request custom_model: : { vehicle_weight: 50 }
import custom_model: { vehicle_weight: 40, max_speed: {...} }
merged custom_model: { vehicle_weight: 50, max_speed: {...} }

@easbar

easbar commented Mar 12, 2020

Copy link
Copy Markdown
Member Author

either make the difference really strict

What do you mean?

or use only one HintsMap

This is not possible imo, because the two maps are two different things: One contains the parameters that were configured on the server side and the other contains the parameters passed in with the request by the user.

Because otherwise we end up looking into two maps for all internal usage.

Yes, and I think this is exactly what we want: There are two different sets of parameters and it should be up to 'us' or whoever implements the method what to do (how to create a weighting) with these parameters.

how can we do this without internal knowledge about a specific profile?

Why are we supposed to do this without internal knowledge about the profile? I understand createWeighting(profile, hints) like this: "Create a weighting for this profile and these request hints (and whoever implements the method can decide how these parameters should be interpreted)".

@karussell

Copy link
Copy Markdown
Member

Ah, ok. So the "general query hints" are the only thing we need and use inside the Weighting but still the profile.hints could modify this "general query hints" if necessary (?) And the current location where this "overwrite" happens is in GraphHopper.createWeighting.

I see ... I had planned (for the CustomModel) the "overwrite" to happen before that method to keep the params of that method low.

@easbar

easbar commented Mar 13, 2020 •

Copy link
Copy Markdown
Member Author

Yes there are query hints and properties given in the profile. Ok, we could do the overwrite before createWeighting, but I decided otherwise for the following two reasons:

  • I still wanted to allow making the weighting creation customizable. Therefore I thought the most general thing we can do is to give everyone who implements this method access to both the selected profile and the hints coming in via the request. If we did some of the 'merging' before the method this might be too opinionated for some use cases.

  • If we only passed ProfileConfig to createWeighting I am not sure where to put the request hints (we should not modify the profile). If we only pass the hints to createWeighting we no longer have access to the profile: Sure we could put the profile fields (weighting, vehicle etc.) into the hints (merge it), but to me this felt weird, because then these fields lose their 'special' meaning (and they will just be some more 'hints').

I had planned (for the CustomModel) the "overwrite" to happen before that method to keep the params of that method low.

Ok if this is somehow required for CustomModel we might change this again. But if its only about reducing the number of parameters I rather have one (or two) more parameters than trying to combine some parameters into one when really they are different things.

@karussell

karussell commented Mar 13, 2020 •

Copy link
Copy Markdown
Member

I still wanted to allow making the weighting creation customizable

Yes, maybe this is a good idea. The problem at the moment is that hints only allows "flat" String,String key-value pairs. I.e. for CustomModel I have to introduce a CustomProfileConfig (with a customModel property or a separate HashMap in the GraphHopper class) and additionally CustomModel as parameter in createWeighting. And so I though I do the overwrite before createWeighting so that I can at least avoid one of these changes.

But maybe a simpler solution could be to allow String,Object key-value pairs?

@easbar

easbar commented Mar 14, 2020 •

Copy link
Copy Markdown
Member Author

Sure yes we can change this to Map<String, Object>. That should not be a problem at all. Its already a PMap (a Map<String, String> with some camel case/underscore conversion) and I do not think we need or even want the camel case/underscroe conversion here. But maybe we need some easy way to do things like getInt etc.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Replace Weighting.matches(HintsMap) with a different hook

2 participants