Skip to content

moved LocationIndexMatch.findNClosest from map-matching to LocationIndexTree - #994

Merged
karussell merged 3 commits into
graphhopper:masterfrom
kodonnell:issue_971_and_993
Mar 22, 2017
Merged

karussell merged 3 commits into
graphhopper:masterfrom
kodonnell:issue_971_and_993

Conversation

@kodonnell

@kodonnell kodonnell commented Mar 20, 2017 •

Copy link
Copy Markdown
Contributor

This is for #993 and map-matching 65. Comments:

  • no real logic changed excepted made maxIterations configurable (from hardcoded value of 2 in map-matching) and throws an error if maxIterations 'truncates' the radius (as discussed in map-matching 65). I've added some comments for tidy-ups (that can be made later).
  • renamed to findWithinRadius to better reflect the method (as opposed to the somewhat misleading findNClosest).
  • copied tests over from map-matching and added one to check the error is thrown for maxIterations truncation.
  • make LocationIndexTree.XFirstSearchCheck public #971 was (I think) prompted by map-matching requirements for XFirstSearchCheck. This is no longer the case, so I'm not sure if we can close make LocationIndexTree.XFirstSearchCheck public #971?
  • required by map-matching 87

@boldtrn boldtrn left a comment

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.

Thanks for this PR. While reviewing the code a couple of questions came to my mind, see my comments. Other from that I think this is a great addition to the core and might be very useful for a couple of use-cases.

* @param edgeFilter only include edges from edgeFilter
* @param radius include all results within this radius (in meters) - assuming it's not
* bounded by maxIterations.
* @param maxIterations if not null, search only a square of (2 * maxIterations - 1) tiles

@boldtrn boldtrn Mar 21, 2017 •

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 don't get the purpose of maxIterations and radius.

include all results within this radius (in meters) - assuming it's not bounded by maxIterations.

How can I bound the radius with maxIterations, when maxIterations says:

Note that it is possible that the search radius exceeds this square - in this case, an exception will be thrown.

To me this sounds contradicting. You can bound the radius with maxIterations, but maxIterations has to be at least as big as the radius.

So maxIterations can increase the radius? But why bothering, I could just increase the radius.

BTW: At this point haven't really looked at the code, I just stumbled over the JavaDoc.

Edit:
Ok, I am finished with reviewing the code and still don't understand maxIterations.
Is it necessary to have maxIterations? If yes why? And if yes, wouldn't it be easier to have a wrapper method that allows calling this method without maxIterations?

Edit2:
In MapMatching 65, you specify that this is an edgeCase, but why would one wants to increase the maxIterations to be bigger than the specified radius. Results outside the radius shouldn't be included anyway?

@kodonnell kodonnell Mar 21, 2017 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I don't get the purpose of maxIterations and radius.

The purpose is to find everything within radius. The maxIterations is there to bound to a certain amounts of tiles - as discussed in MapMatching 65. (Personally, I don't think it's required - as long as we always search one tile index 'greater' than the radius - but that's what was in the code - and I'd prefer to keep this a moving of the method as opposed to a rewrite. Yes, I did rewrite it somewhat so that the method actually reflected it's purpose and implementation.)

To me this sounds contradicting

Good point. I think I changed the logic to throw an error after I'd written the first part of the doc. Will update.

So maxIterations can increase the radius?

No. The purpose (I think) is to say "OK, give me all the results within 100m - but don't look more than 2 tiles away". As far as I'm aware, it's there for performance.

Is it necessary to have maxIterations?

Yes and no. I believe it's necessary to e.g. always do at least two iterations, even if you find something in the first. However, we could rewrite the logic to reflect this (i.e. always one 'greater' than the radius). It's not necessary for actually bounding the edges - radius does that. (Again, I'm going with what's there - if we want to change it, that's fine with me.)

wouldn't it be easier to have a wrapper method that allows calling this method without maxIterations?

If you wish - I allowed it to be called with maxIterations = null which does this.

In MapMatching 65, you specify that this is an edgeCase, but why would one wants to increase the maxIterations to be bigger than the specified radius. Results outside the radius shouldn't be included anyway?

See comment by @karussell in MapMatching 65 (which is what I was referring to above) - you always need to do at least two. Maybe my description of it is misleading ...

@karussell - maybe you have some comments? I was hoping to leave the logic generalisations until later, but if it's confusing in it's existing state, we should probably do it now.

@boldtrn boldtrn Mar 21, 2017 •

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.

The purpose is to find everything within radius. The maxIterations is there to bound to a certain amounts of tiles - as discussed in MapMatching 65.

Sorry, I still don't really get it. Shouldn't this be the default? If we actually want something like this, could we create a boolean like: "includeSurroundingTiles". This would increase the requiredRadius by one. I think this parameter requires too much knowledge about the actual method. Since this is a public method and is intended to be used by everyone, we should keep it simple.

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.

We have to do that even for the normal version but this parameter allows for even longer searches. See #221

I would also prefer to avoid this extra parameter in the method - isn't this also more a static configuration like 'maximum allowed visited nodes' and not per request?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I dont know = ) I think I may have confused it with how I interpreted maxIterations originally - it's not "iterate through no more than this many tiles" it's "iterate through this many tiles". On second thought, I now think it should really just be making sure that we always include the extra surrounding tiles - which should have nothing to do with the user.

}

final double returnAllResultsWithin = distCalc.calcNormalizedDist(radius);
// implement a cheap priority queue via List, sublist and Collections.sort

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.

Why aren't we using a PriorityQueue?
I assume there is a reason, maybe we could add the reason to the comment?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

No idea - that's how it was done in map-matching, and I was just shifting it across.

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, thanks. Any idea @karussell?

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.

Good question. Lets address in a different issue or via discuss :)

// check the max iterations:
int requiredIterationsForThisRadius = (int) Math.ceil(radius / this.minResolutionInMeter)
+ 1; // note that this is a conservative upper bound
int _maxIterations;

@boldtrn boldtrn Mar 21, 2017 •

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.

Why do we prefix with _? Wouldn't be a good name for the variable better?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Just my convention - something like tmpMaxIterations?

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.

Not sure, as it's not really tmp. I would favor removing maxIterations from the parameter list :). But let's discuss this in the comment above.

final double returnAllResultsWithin = distCalc.calcNormalizedDist(radius);
// implement a cheap priority queue via List, sublist and Collections.sort
final List<QueryResult> queryResults = new ArrayList<QueryResult>();
GHIntHashSet set = new GHIntHashSet();

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.

What does the set contain? Needed check the code again to see where it was passed.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

See third argument of findNetworkEntries - I can't remember now, but I believe it's all of the nodes found up to and including that iteration of findNetworkEntries.

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.

Sorry, I should have written the comment differently. I would just prefer a different name, as set can contain everything.

@Override
protected boolean check(int node, double normedDist, int wayIndex,
EdgeIteratorState edge, QueryResult.Position pos) {
if (normedDist < returnAllResultsWithin || queryResults.isEmpty()

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.

isEmpty check to make sure that at least one result is returned? Should we remove that result later?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

That's how the current code is structured. I agree with you - see my comments. Maybe it should be removed, as it doesn't behave as described. @karussell - is there a reason we need to keep this? Should we make it optional?

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.

The idea was to have at least one candidate even if outside, so maybe this is not necessary, yes.

Here again let us try to minimize this issue and create improvement issues separately to make merging this faster.

}

// avoid duplicate edges
if (qr.getClosestEdge().getEdge() == edge.getEdge()) {

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.

Why aren't we using a set/map here and filter potential unwanted results after the for loop?

With for loop I mean:
for (int iteration = 0; iteration < _maxIterations; iteration++) {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I agree (I think) as per my comment about 'clean[ing] up the index tracking business'. However, again, I wanted to port the functionality and tests before tweaking. We can rewrite if you prefer ...

@kodonnell

Copy link
Copy Markdown
Contributor Author

As a general comment, there's possibly cause for a more thorough rewrite. I feel a lot of the machinery is aligned with the GH use cases, which are to find the nearest candidates to a point. If we want to be fully generic, then we could probably e.g. make better use of the quadtree (as opposed to always looping through leaf nodes - I think that's the term).

@boldtrn

boldtrn commented Mar 21, 2017

Copy link
Copy Markdown
Member

As a general comment, there's possibly cause for a more thorough rewrite.

Yes maybe. Sorry for being so petty about the implementation, as this PR is only about moving the method :).

@karussell

Copy link
Copy Markdown
Member

Thanks a lot @kodonnell !

To make merging this a no-brainer would you mind to remove the maxIterations parameter? We can then discuss the improvements or rewrite in a separate issue or in a dev discuss topic

make better use of the quadtree (as opposed to always looping through leaf nodes

What do you mean here?

@kodonnell kodonnell changed the title moved LocationIndexMatch.findNClosest from map-matching to LocationIndexTree.findWithinRadius moved LocationIndexMatch.findNClosest from map-matching to LocationIndexTree Mar 21, 2017
@kodonnell

kodonnell commented Mar 21, 2017 •

Copy link
Copy Markdown
Contributor Author

To make merging this a no-brainer

Done - and to remove the brain completely, I've just reverted to the original map-matching code - I didn't change the name as it'd be wrong too, and it probably makes sense to clarify the purpose before editing it further.

What do you mean here?

At the moment we're manually iterating through every leaf node (I think). If we've got a radius of 8km, with leaf node size of 1km, then (heuristically) we'd first find the four covering 8km-radius nodes, and then drill down through each using the quadtree until we get to the leaf nodes that contain stuff (which might be e.g. the 4km one in some cases, the 2km in others, etc.). It's a more natural use of a quadtree, and could have better performance in the case where a lot of the search radius is empty (i.e. we know the parent is empty, so no need to check the children). That'd also be especially true if we're traversing the quadtree every time we seach a leaf node. That all said, I've got no idea how the GH quadtree is implemented, so the above might all be baloney = )

Thanks a lot @kodonnell

No worries - though I feel like me doing PR's actually takes you guys more time that if you did it yourselves! Hopefully I'll get better.

@karussell

Copy link
Copy Markdown
Member

That'd also be especially true if we're traversing the quadtree every time we seach a leaf node.

Aha, makes sense. Still the quadtree implementation is very fast so even if we do this a couple of times without success I guess the improvements will be not really comparable to the routing time.

No worries - though I feel like me doing PR's actually takes you guys more time that if you did it yourselves! Hopefully I'll get better

no no :). I'm/we're thankfully for every work being done from others, as the repository activity is nicely/overwhelming increasing :)

@karussell karussell added this to the 0.9 milestone Mar 22, 2017
@karussell
karussell merged commit 5c15c03 into graphhopper:master Mar 22, 2017
@kodonnell
kodonnell deleted the issue_971_and_993 branch March 22, 2017 18:07
@kodonnell

Copy link
Copy Markdown
Contributor Author

Thanks @karussell = ) Opened #997 for the issues discussed above.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

make LocationIndexTree.XFirstSearchCheck public

3 participants