You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Make EdgeAccess#INT_DIST_FACTOR configurable #1688
Currently, the max distance of an edge is roughly 1000km. There are cases where this is not enough, especially when using a different datasource than OSM.
Would it make sense to make the INT_DIST_FACTOR configurable?
Would it not suffice to set INT_DIST_FACTOR to 10 ? In this case the minimum distance that is not considered to be zero would be increased from 0.5mm to 5cm (should not be relevant ?) and the maximum distance would be increased from about 2147km to 214748km. The problem with changing the value lies mostly in adjusting the failing tests (I guess 99% rounding issues and/or hard coded expected distances that need to be adjusted), but this would be rather annoying if the conversion factor would not be fixed.
The problem with changing the value lies mostly in adjusting the failing tests
Now I thought about this again I am not sure anymore...
Could there be other downsides?
Likely there will be some more rounding issues, and we probably have to identify the places where the (rounded) distances are compared to the precise ones ? For example for AStar we might be comparing the (rounded) edge-distance with the precise result of calcDist(lat, lon). For CH we are storing the sum of several (rounded) edge-distances as (rounded) distance of the shortcut. For QueryGraph there can be a mismatch between the edge distance and the sum of the distances of the virtual edges. These issues already exist, but might get worse for a lower distance precision ? I am not sure to be honest. Maybe we need to try fixing the tests and see what complications arise.
Ultimately the best solution might be to use 0.1m as distance unit and integer values all across GH (already during data import, calcDist(lat,lon) etc.) and only convert it to meters where the result is shown to the user (?). I guess it is the 'do not use floating point numbers where exact answers are required' type of problem.
This one I do not understand as rounding issues can sum up even with integers (?)
At least with integers we have no rounding issues for summation (like in CH shortcuts), no ? For cases where we compare distances that were calculated with calcDist(lat, lon) it is indeed more complicated than just switching to int. For example when we split an edge during QueryGraph#lookup we might be able to first calculate the distances of the virtual edges states and then adjust them such that the sum is the same as the distance of the edge we split. For AStar distance comparisons we could use an offset, because it is ok to overestimate the distance (just not underestimate it), etc. It definitely depends on the situation, but with integers at least we get a chance to use the 'right' value whereas with floating point numbers there is always some uncertainty involved.
Could we round the results from calcDist? So that we are comparing two rounded distances?
Yes, but the question is sometimes how often we do the rounding. Lets say the real distance of an edge (according to calcDist) is 3.68m. Then we round it to 3.7m or 37 in units of 0.1m. When we split this edge in QueryGraph maybe the two virtual edges have (according to calcDist) distance 3.038m and 0.642m. When we apply the rounding this will be 3.0m and 0.6m so the sum would be 3.6m instead of 3.7m...
but with integers at least we get a chance to use the 'right' value whereas with floating point numbers there is always some uncertainty involved.
Can you explain what you mean here? Let us assume a simple graph where the task is to calc route from A to C:
A-B-C and A-D-C. Now if edge AB is 0.24, BC is 0.14, AD is 0.15 and DC is 0.15 the sum of rounded distance is always 0.3 i.e. it picks the first and longer result ABC. How will integers with 0.1m unit help to make something better (prefer ADC) than a double value that is rounded to 0.1?
When thinking about all this I'm now more hesitating if we really want to make rounding issues more likely only to tweak this internal factor. Instead maybe we should think about storing the distance in more than 31 bits? A simple fix would be to allow 32 bits. And the second step could be to introduce distance dependent EncodedValues (if perf is ok) that could be then configurable.
Do you think that there will be serious issues because of that? If I understand this correctly, for every waypoint the distance can be off by ~ .1m.
I think so, yes. Gets more problematic for longer routes, yes.
A-B-C and A-D-C. Now if edge AB is 0.24, BC is 0.14, AD is 0.15 and DC is 0.15 the sum of rounded distance is always 0.3 i.e. it picks the first and longer result ABC. How will integers with 0.1m unit help to make something better (prefer ADC) than a double value that is rounded to 0.1?
Maybe you have a typo here ? I am not sure what you mean
@boldtrn Would this suffice ? Can you explain in which situation the edges become longer than 2000km btw ?
And the second step could be to introduce distance dependent EncodedValues (if perf is ok) that could be then configurable.
Configurable sounds good, but as long as the correctness of the route calculations depends (in some ways) on the precision this is also a bit dangerous ?
When thinking about all this I'm now more hesitating if we really want to make rounding issues more likely only to tweak this internal factor.
I think I agree, but the real solution would be to properly deal with the situations where the rounding can be harmful (needs some analysis to find out which ones these are).
Ups. I meant 0.05 instead of 0.15 for DC. But this example is even better:
the algo will choose ABC (0.3 < 0.4) as best path although ADC is shorter when using full precision for the single edge distances (0.38 > 0.30).
Ok, no, units of 0.1m do not help to find the 'better' path ADC, but in this model ABC simply is the shortest path. If we store distances in units of 0.1m there is no way to distinguish between e.g. 0.15m and 0.20m anymore. As we see here, which path is 'better' depends on how we distribute the real distance over the single edges. For example another path AEFC = round(0.14) + round(0.02) + round(0.14) = 0.2 would have the same length as ADC in the real world, but come out as the shortest path in our model (with a 'wrong' distance of 0.2 instead of 0.3).
In any case, the conversion from real-world distances to our discrete model causes an error and whether or not this is acceptable depends on the precision we need. For example the distance of a path with 1000 edges can be off by 1000*0.05m=50m compared to its real-world counterpart (in the (unlikely) worst case). This error can only be controlled by the precision we use to represent distances in our model (and it does not play a role whether we use integers or doubles (that later get converted to 4byte numbers)).
However, in my opinion using integer distances internally is still advantageous, because at least after the initial conversion from the real-world distances to the internal units the calculations are (or at least can be made) exact. Right now we not only introduce an error when converting real-world distances to internal distances (in EdgeAccess#distToInt during import), but also when we do things like taking some distances from storage, 'casting' them to double, adding them and writing them back to storage (again using distToInt), like in CH shortcuts. When we stay in the internal int representation my hope would be that we can make all internal computations exact. For example if we split an edge into two virtual edges (see my example above) we can adjust the distances of the two virtual edges such that their sum exactly matches the distance of the original edge whereas with double values we can still never be sure that dist(virtualEdge1)+dist(virtualEdge2)==dist(edge), especially after writing the results back to storage (calling distToInt again).
@boldtrn Would this suffice ? Can you explain in which situation the edges become longer than 2000km btw ?
There are scenarios, other than regular OSM road routing that require longer edges. For example flights, ship routes, etc.
For example for flights we should be able to create edges that are ~20,000km (half the equator) long.
I think so, yes. Gets more problematic for longer routes, yes.
For example the distance of a path with 1000 edges can be off by 1000*0.05m=50m
Ah I see, thanks for the explanation. This would indeed be a bit unfortunate.
So maybe we could consider making the factor configurable? Maybe not even in the config.yml, but at least in a way that allows a 3rd party to change this when using GraphHopper as a lib. Right now, I think it wouldn't be possible to change this factor without forking?
That would be my least preferred option to be honest. For long straight routes, the curvature of the earth plays a major role (geodetic line). I am not 100% sure what happens if you split these lines. But usually, this results in multiple arcs, which would probably result in an incorrect result.
For long straight routes, the curvature of the earth plays a major role (geodetic line).
Ok, I see. It should def. be possible to do this correctly somehow, but sure this takes some effort as well.
Maybe we should really use some more bytes for distance and in turn remove distance from CH shortcuts. But also we should clarify what the problematic cases are when we use a higher/lower precision.
For long straight routes, the curvature of the earth plays a major role (geodetic line).
I think that earth(distA)+earth(distB) == earth(distAB). We already consider this "round earth stuff" in OSMReader via DistanceCalcEarth. So there should not a problem with splitting edges. Do you have examples where this fails @boldtrn?
So there should not a problem with splitting edges. Do you have examples where this fails @boldtrn?
I just checked, the splitting could work, if you calculate the coordinates on the arc precisely. I was mostly concerned about the visualization part when splitting edges, but that works as well.
At least for my case, this would make things complicated though. Most of the edges are > 1000km. So splitting would significantly raise the number of edges and nodes. Probably also slow down the route calculation, since there would be more edges.
Activity
easbar commentedon Aug 18, 2019
Would it not suffice to set
INT_DIST_FACTORto10? In this case the minimum distance that is not considered to be zero would be increased from0.5mmto5cm(should not be relevant ?) and the maximum distance would be increased from about2147kmto214748km. The problem with changing the value lies mostly in adjusting the failing tests (I guess 99% rounding issues and/or hard coded expected distances that need to be adjusted), but this would be rather annoying if the conversion factor would not be fixed.boldtrn commentedon Aug 18, 2019
This would fix at least my scenario perfectly @easbar 👍
boldtrn commentedon Aug 18, 2019
Should I provide a PR? Could there be other downsides? Probably there was a reason why the factor is so big?
[-]Make EdgeAccess#INT_DIST_FACTOR confirgurabel[/-][+]Make EdgeAccess#INT_DIST_FACTOR configurable[/+]easbar commentedon Aug 20, 2019
Now I thought about this again I am not sure anymore...
Likely there will be some more rounding issues, and we probably have to identify the places where the (rounded) distances are compared to the precise ones ? For example for AStar we might be comparing the (rounded) edge-distance with the precise result of
calcDist(lat, lon). For CH we are storing the sum of several (rounded) edge-distances as (rounded) distance of the shortcut. For QueryGraph there can be a mismatch between the edge distance and the sum of the distances of the virtual edges. These issues already exist, but might get worse for a lower distance precision ? I am not sure to be honest. Maybe we need to try fixing the tests and see what complications arise.Ultimately the best solution might be to use
0.1mas distance unit and integer values all across GH (already during data import,calcDist(lat,lon)etc.) and only convert it to meters where the result is shown to the user (?). I guess it is the 'do not use floating point numbers where exact answers are required' type of problem.karussell commentedon Aug 20, 2019
This one I do not understand as rounding issues can sum up even with integers (?)
boldtrn commentedon Aug 20, 2019
Could we round the results from calcDist? So that we are comparing two rounded distances?
easbar commentedon Aug 20, 2019
At least with integers we have no rounding issues for summation (like in CH shortcuts), no ? For cases where we compare distances that were calculated with
calcDist(lat, lon)it is indeed more complicated than just switching to int. For example when we split an edge duringQueryGraph#lookupwe might be able to first calculate the distances of the virtual edges states and then adjust them such that the sum is the same as the distance of the edge we split. For AStar distance comparisons we could use an offset, because it is ok to overestimate the distance (just not underestimate it), etc. It definitely depends on the situation, but with integers at least we get a chance to use the 'right' value whereas with floating point numbers there is always some uncertainty involved.easbar commentedon Aug 20, 2019
Yes, but the question is sometimes how often we do the rounding. Lets say the real distance of an edge (according to
calcDist) is3.68m. Then we round it to3.7mor37in units of0.1m. When we split this edge inQueryGraphmaybe the two virtual edges have (according tocalcDist) distance3.038mand0.642m. When we apply the rounding this will be3.0mand0.6mso the sum would be3.6minstead of3.7m...boldtrn commentedon Aug 20, 2019
Do you think that there will be serious issues because of that? If I understand this correctly, for every waypoint the distance can be off by ~ .1m.
karussell commentedon Aug 20, 2019
Can you explain what you mean here? Let us assume a simple graph where the task is to calc route from A to C:
A-B-C and A-D-C. Now if edge AB is 0.24, BC is 0.14, AD is 0.15 and DC is 0.15 the sum of rounded distance is always 0.3 i.e. it picks the first and longer result ABC. How will integers with 0.1m unit help to make something better (prefer ADC) than a double value that is rounded to 0.1?
When thinking about all this I'm now more hesitating if we really want to make rounding issues more likely only to tweak this internal factor. Instead maybe we should think about storing the distance in more than 31 bits? A simple fix would be to allow 32 bits. And the second step could be to introduce distance dependent EncodedValues (if perf is ok) that could be then configurable.
I think so, yes. Gets more problematic for longer routes, yes.
easbar commentedon Aug 20, 2019
Maybe you have a typo here ? I am not sure what you mean
or
In both cases it is not
the sum of rounded distance is always 0.3?easbar commentedon Aug 20, 2019
@boldtrn Would this suffice ? Can you explain in which situation the edges become longer than
2000kmbtw ?Configurable sounds good, but as long as the correctness of the route calculations depends (in some ways) on the precision this is also a bit dangerous ?
I think I agree, but the real solution would be to properly deal with the situations where the rounding can be harmful (needs some analysis to find out which ones these are).
karussell commentedon Aug 20, 2019
Ups. I meant 0.05 instead of 0.15 for DC. But this example is even better:
the algo will choose ABC (
0.3 < 0.4) as best path although ADC is shorter when using full precision for the single edge distances (0.38 > 0.30).Would 0.1m units help in this case?
easbar commentedon Aug 20, 2019
Ok, no, units of
0.1mdo not help to find the 'better' path ADC, but in this model ABC simply is the shortest path. If we store distances in units of0.1mthere is no way to distinguish between e.g.0.15mand0.20manymore. As we see here, which path is 'better' depends on how we distribute the real distance over the single edges. For example another pathAEFC = round(0.14) + round(0.02) + round(0.14) = 0.2would have the same length as ADC in the real world, but come out as the shortest path in our model (with a 'wrong' distance of0.2instead of0.3).In any case, the conversion from real-world distances to our discrete model causes an error and whether or not this is acceptable depends on the precision we need. For example the distance of a path with 1000 edges can be off by
1000*0.05m=50mcompared to its real-world counterpart (in the (unlikely) worst case). This error can only be controlled by the precision we use to represent distances in our model (and it does not play a role whether we use integers or doubles (that later get converted to 4byte numbers)).However, in my opinion using integer distances internally is still advantageous, because at least after the initial conversion from the real-world distances to the internal units the calculations are (or at least can be made) exact. Right now we not only introduce an error when converting real-world distances to internal distances (in
EdgeAccess#distToIntduring import), but also when we do things like taking some distances from storage, 'casting' them to double, adding them and writing them back to storage (again usingdistToInt), like in CH shortcuts. When we stay in the internal int representation my hope would be that we can make all internal computations exact. For example if we split an edge into two virtual edges (see my example above) we can adjust the distances of the two virtual edges such that their sum exactly matches the distance of the original edge whereas with double values we can still never be sure thatdist(virtualEdge1)+dist(virtualEdge2)==dist(edge), especially after writing the results back to storage (callingdistToIntagain).easbar commentedon Aug 20, 2019
I am not sure tbh.
boldtrn commentedon Aug 20, 2019
There are scenarios, other than regular OSM road routing that require longer edges. For example flights, ship routes, etc.
For example for flights we should be able to create edges that are ~20,000km (half the equator) long.
Ah I see, thanks for the explanation. This would indeed be a bit unfortunate.
So maybe we could consider making the factor configurable? Maybe not even in the config.yml, but at least in a way that allows a 3rd party to change this when using GraphHopper as a lib. Right now, I think it wouldn't be possible to change this factor without forking?
easbar commentedon Aug 20, 2019
Can you not split these edges into smaller pieces #435 ?
boldtrn commentedon Aug 20, 2019
That would be my least preferred option to be honest. For long straight routes, the curvature of the earth plays a major role (geodetic line). I am not 100% sure what happens if you split these lines. But usually, this results in multiple arcs, which would probably result in an incorrect result.
easbar commentedon Aug 20, 2019
Ok, I see. It should def. be possible to do this correctly somehow, but sure this takes some effort as well.
Maybe we should really use some more bytes for distance and in turn remove distance from CH shortcuts. But also we should clarify what the problematic cases are when we use a higher/lower precision.
karussell commentedon Aug 20, 2019
I think that
earth(distA)+earth(distB) == earth(distAB). We already consider this "round earth stuff" in OSMReader via DistanceCalcEarth. So there should not a problem with splitting edges. Do you have examples where this fails @boldtrn?boldtrn commentedon Aug 20, 2019
I just checked, the splitting could work, if you calculate the coordinates on the arc precisely. I was mostly concerned about the visualization part when splitting edges, but that works as well.
At least for my case, this would make things complicated though. Most of the edges are > 1000km. So splitting would significantly raise the number of edges and nodes. Probably also slow down the route calculation, since there would be more edges.
easbar commentedon Sep 10, 2019
CH shortcut distances are gone so no need to worry about them here anymore, #1719