Support snapping to multiple ways at an input location (#5953)

This PR improves routing results by adding support for snapping to
multiple ways at input locations.

This means all edges at the snapped location can act as source/target
candidates for routing search, ensuring we always find the best route,
and not the one dependent on the edge selected.
This commit is contained in:
Michael Bell
2022-08-27 11:36:20 +01:00
committed by GitHub
parent bb18a2b428
commit d74e7b66bd
59 changed files with 2820 additions and 1964 deletions
+116 -181
View File
@@ -99,64 +99,67 @@ class BasePlugin
return Status::Error;
}
// Decides whether to use the phantom node from a big or small component if both are found.
// Returns true if all phantom nodes are in the same component after snapping.
std::vector<PhantomNode>
SnapPhantomNodes(const std::vector<PhantomNodePair> &phantom_node_pair_list) const
// Decides whether to use the phantom candidates from big or small components if both are found.
std::vector<PhantomNodeCandidates>
SnapPhantomNodes(std::vector<PhantomCandidateAlternatives> alternatives_list) const
{
const auto check_component_id_is_tiny =
[](const std::pair<PhantomNode, PhantomNode> &phantom_pair) {
return phantom_pair.first.component.is_tiny;
};
// are all phantoms from a tiny cc?
const auto check_all_in_same_component =
[](const std::vector<std::pair<PhantomNode, PhantomNode>> &nodes) {
const auto component_id = nodes.front().first.component.id;
return std::all_of(std::begin(nodes),
std::end(nodes),
[component_id](const PhantomNodePair &phantom_pair) {
return component_id == phantom_pair.first.component.id;
});
const auto all_in_same_tiny_component =
[](const std::vector<PhantomCandidateAlternatives> &alts_list) {
return std::any_of(
alts_list.front().first.begin(),
alts_list.front().first.end(),
// For each of the first possible phantoms, check if all other
// positions in the list have a phantom from the same small component.
[&](const PhantomNode &phantom) {
if (!phantom.component.is_tiny)
{
return false;
}
const auto component_id = phantom.component.id;
return std::all_of(
std::next(alts_list.begin()),
std::end(alts_list),
[component_id](const PhantomCandidateAlternatives &alternatives) {
return candidatesHaveComponent(alternatives.first, component_id);
});
});
};
const auto fallback_to_big_component =
[](const std::pair<PhantomNode, PhantomNode> &phantom_pair) {
if (phantom_pair.first.component.is_tiny && phantom_pair.second.IsValid() &&
!phantom_pair.second.component.is_tiny)
{
return phantom_pair.second;
}
return phantom_pair.first;
};
// Move the alternative into the final list
const auto fallback_to_big_component = [](PhantomCandidateAlternatives &alternatives) {
auto no_big_alternative = alternatives.second.empty();
return no_big_alternative ? std::move(alternatives.first)
: std::move(alternatives.second);
};
const auto use_closed_phantom =
[](const std::pair<PhantomNode, PhantomNode> &phantom_pair) {
return phantom_pair.first;
};
// Move the alternative into the final list
const auto use_closed_phantom = [](PhantomCandidateAlternatives &alternatives) {
return std::move(alternatives.first);
};
const bool every_phantom_is_in_tiny_cc = std::all_of(std::begin(phantom_node_pair_list),
std::end(phantom_node_pair_list),
check_component_id_is_tiny);
auto all_in_same_component = check_all_in_same_component(phantom_node_pair_list);
std::vector<PhantomNode> snapped_phantoms;
snapped_phantoms.reserve(phantom_node_pair_list.size());
const auto no_alternatives =
std::all_of(alternatives_list.begin(),
alternatives_list.end(),
[](const PhantomCandidateAlternatives &alternatives) {
return alternatives.second.empty();
});
std::vector<PhantomNodeCandidates> snapped_phantoms;
snapped_phantoms.reserve(alternatives_list.size());
// The only case we don't snap to the big component if all phantoms are in the same small
// component
if (every_phantom_is_in_tiny_cc && all_in_same_component)
if (no_alternatives || all_in_same_tiny_component(alternatives_list))
{
std::transform(phantom_node_pair_list.begin(),
phantom_node_pair_list.end(),
std::transform(alternatives_list.begin(),
alternatives_list.end(),
std::back_inserter(snapped_phantoms),
use_closed_phantom);
}
else
{
std::transform(phantom_node_pair_list.begin(),
phantom_node_pair_list.end(),
std::transform(alternatives_list.begin(),
alternatives_list.end(),
std::back_inserter(snapped_phantoms),
fallback_to_big_component);
}
@@ -181,35 +184,26 @@ class BasePlugin
for (const auto i : util::irange<std::size_t>(0UL, parameters.coordinates.size()))
{
Approach approach = engine::Approach::UNRESTRICTED;
if (use_approaches && parameters.approaches[i])
approach = parameters.approaches[i].get();
if (use_hints && parameters.hints[i] &&
if (use_hints && parameters.hints[i] && !parameters.hints[i]->segment_hints.empty() &&
parameters.hints[i]->IsValid(parameters.coordinates[i], facade))
{
phantom_nodes[i].push_back(PhantomNodeWithDistance{
parameters.hints[i]->phantom,
util::coordinate_calculation::greatCircleDistance(
parameters.coordinates[i], parameters.hints[i]->phantom.location),
});
for (const auto &seg_hint : parameters.hints[i]->segment_hints)
{
phantom_nodes[i].push_back(PhantomNodeWithDistance{
seg_hint.phantom,
util::coordinate_calculation::greatCircleDistance(
parameters.coordinates[i], seg_hint.phantom.location)});
}
continue;
}
if (use_bearings && parameters.bearings[i])
{
phantom_nodes[i] =
facade.NearestPhantomNodesInRange(parameters.coordinates[i],
radiuses[i],
parameters.bearings[i]->bearing,
parameters.bearings[i]->range,
approach,
use_all_edges);
}
else
{
phantom_nodes[i] = facade.NearestPhantomNodesInRange(
parameters.coordinates[i], radiuses[i], approach, use_all_edges);
}
phantom_nodes[i] = facade.NearestPhantomNodesInRange(
parameters.coordinates[i],
radiuses[i],
use_bearings ? parameters.bearings[i] : boost::none,
use_approaches && parameters.approaches[i] ? parameters.approaches[i].get()
: engine::Approach::UNRESTRICTED,
use_all_edges);
}
return phantom_nodes;
@@ -218,7 +212,7 @@ class BasePlugin
std::vector<std::vector<PhantomNodeWithDistance>>
GetPhantomNodes(const datafacade::BaseDataFacade &facade,
const api::BaseParameters &parameters,
unsigned number_of_results) const
size_t number_of_results) const
{
std::vector<std::vector<PhantomNodeWithDistance>> phantom_nodes(
parameters.coordinates.size());
@@ -231,56 +225,26 @@ class BasePlugin
BOOST_ASSERT(parameters.IsValid());
for (const auto i : util::irange<std::size_t>(0UL, parameters.coordinates.size()))
{
Approach approach = engine::Approach::UNRESTRICTED;
if (use_approaches && parameters.approaches[i])
approach = parameters.approaches[i].get();
if (use_hints && parameters.hints[i] &&
if (use_hints && parameters.hints[i] && !parameters.hints[i]->segment_hints.empty() &&
parameters.hints[i]->IsValid(parameters.coordinates[i], facade))
{
phantom_nodes[i].push_back(PhantomNodeWithDistance{
parameters.hints[i]->phantom,
util::coordinate_calculation::greatCircleDistance(
parameters.coordinates[i], parameters.hints[i]->phantom.location),
});
for (const auto &seg_hint : parameters.hints[i]->segment_hints)
{
phantom_nodes[i].push_back(PhantomNodeWithDistance{
seg_hint.phantom,
util::coordinate_calculation::greatCircleDistance(
parameters.coordinates[i], seg_hint.phantom.location)});
}
continue;
}
if (use_bearings && parameters.bearings[i])
{
if (use_radiuses && parameters.radiuses[i])
{
phantom_nodes[i] = facade.NearestPhantomNodes(parameters.coordinates[i],
number_of_results,
*parameters.radiuses[i],
parameters.bearings[i]->bearing,
parameters.bearings[i]->range,
approach);
}
else
{
phantom_nodes[i] = facade.NearestPhantomNodes(parameters.coordinates[i],
number_of_results,
parameters.bearings[i]->bearing,
parameters.bearings[i]->range,
approach);
}
}
else
{
if (use_radiuses && parameters.radiuses[i])
{
phantom_nodes[i] = facade.NearestPhantomNodes(parameters.coordinates[i],
number_of_results,
*parameters.radiuses[i],
approach);
}
else
{
phantom_nodes[i] = facade.NearestPhantomNodes(
parameters.coordinates[i], number_of_results, approach);
}
}
phantom_nodes[i] = facade.NearestPhantomNodes(
parameters.coordinates[i],
number_of_results,
use_radiuses ? parameters.radiuses[i] : boost::none,
use_bearings ? parameters.bearings[i] : boost::none,
use_approaches && parameters.approaches[i] ? parameters.approaches[i].get()
: engine::Approach::UNRESTRICTED);
// we didn't find a fitting node, return error
if (phantom_nodes[i].empty())
@@ -291,10 +255,11 @@ class BasePlugin
return phantom_nodes;
}
std::vector<PhantomNodePair> GetPhantomNodes(const datafacade::BaseDataFacade &facade,
const api::BaseParameters &parameters) const
std::vector<PhantomCandidateAlternatives>
GetPhantomNodes(const datafacade::BaseDataFacade &facade,
const api::BaseParameters &parameters) const
{
std::vector<PhantomNodePair> phantom_node_pairs(parameters.coordinates.size());
std::vector<PhantomCandidateAlternatives> alternatives(parameters.coordinates.size());
const bool use_hints = !parameters.hints.empty();
const bool use_bearings = !parameters.bearings.empty();
@@ -305,87 +270,57 @@ class BasePlugin
BOOST_ASSERT(parameters.IsValid());
for (const auto i : util::irange<std::size_t>(0UL, parameters.coordinates.size()))
{
Approach approach = engine::Approach::UNRESTRICTED;
if (use_approaches && parameters.approaches[i])
approach = parameters.approaches[i].get();
if (use_hints && parameters.hints[i] &&
if (use_hints && parameters.hints[i] && !parameters.hints[i]->segment_hints.empty() &&
parameters.hints[i]->IsValid(parameters.coordinates[i], facade))
{
phantom_node_pairs[i].first = parameters.hints[i]->phantom;
std::transform(parameters.hints[i]->segment_hints.begin(),
parameters.hints[i]->segment_hints.end(),
std::back_inserter(alternatives[i].first),
[](const auto &seg_hint) { return seg_hint.phantom; });
// we don't set the second one - it will be marked as invalid
continue;
}
if (use_bearings && parameters.bearings[i])
{
if (use_radiuses && parameters.radiuses[i])
{
phantom_node_pairs[i] =
facade.NearestPhantomNodeWithAlternativeFromBigComponent(
parameters.coordinates[i],
*parameters.radiuses[i],
parameters.bearings[i]->bearing,
parameters.bearings[i]->range,
approach,
use_all_edges);
}
else
{
phantom_node_pairs[i] =
facade.NearestPhantomNodeWithAlternativeFromBigComponent(
parameters.coordinates[i],
parameters.bearings[i]->bearing,
parameters.bearings[i]->range,
approach,
use_all_edges);
}
}
else
{
if (use_radiuses && parameters.radiuses[i])
{
phantom_node_pairs[i] =
facade.NearestPhantomNodeWithAlternativeFromBigComponent(
parameters.coordinates[i],
*parameters.radiuses[i],
approach,
use_all_edges);
}
else
{
phantom_node_pairs[i] =
facade.NearestPhantomNodeWithAlternativeFromBigComponent(
parameters.coordinates[i], approach, use_all_edges);
}
}
alternatives[i] = facade.NearestCandidatesWithAlternativeFromBigComponent(
parameters.coordinates[i],
use_radiuses ? parameters.radiuses[i] : boost::none,
use_bearings ? parameters.bearings[i] : boost::none,
use_approaches && parameters.approaches[i] ? parameters.approaches[i].get()
: engine::Approach::UNRESTRICTED,
use_all_edges);
// we didn't find a fitting node, return error
if (!phantom_node_pairs[i].first.IsValid())
if (alternatives[i].first.empty())
{
// This ensures the list of phantom nodes only consists of valid nodes.
// We can use this on the call-site to detect an error.
phantom_node_pairs.pop_back();
alternatives.pop_back();
break;
}
BOOST_ASSERT(phantom_node_pairs[i].first.IsValid());
BOOST_ASSERT(phantom_node_pairs[i].second.IsValid());
BOOST_ASSERT(!alternatives[i].first.empty());
}
return phantom_node_pairs;
return alternatives;
}
std::string MissingPhantomErrorMessage(const std::vector<PhantomNodePair> &phantom_nodes,
const std::vector<util::Coordinate> &coordinates) const
std::string
MissingPhantomErrorMessage(const std::vector<PhantomCandidateAlternatives> &alternatives,
const std::vector<util::Coordinate> &coordinates) const
{
BOOST_ASSERT(phantom_nodes.size() < coordinates.size());
auto mismatch = std::mismatch(phantom_nodes.begin(),
phantom_nodes.end(),
coordinates.begin(),
coordinates.end(),
[](const auto &phantom_node, const auto &coordinate) {
return phantom_node.first.input_location == coordinate;
});
std::size_t missing_index = std::distance(phantom_nodes.begin(), mismatch.first);
BOOST_ASSERT(alternatives.size() < coordinates.size());
auto mismatch =
std::mismatch(alternatives.begin(),
alternatives.end(),
coordinates.begin(),
coordinates.end(),
[](const auto &candidates_pair, const auto &coordinate) {
return std::any_of(candidates_pair.first.begin(),
candidates_pair.first.end(),
[&](const auto &phantom) {
return phantom.input_location == coordinate;
});
});
std::size_t missing_index = std::distance(alternatives.begin(), mismatch.first);
return std::string("Could not find a matching segment for coordinate ") +
std::to_string(missing_index);
}
+1 -1
View File
@@ -31,7 +31,7 @@ class TripPlugin final : public BasePlugin
const int max_locations_trip;
InternalRouteResult ComputeRoute(const RoutingAlgorithmsInterface &algorithms,
const std::vector<PhantomNode> &phantom_node_list,
const std::vector<PhantomNodeCandidates> &candidates_list,
const std::vector<NodeID> &trip,
const bool roundtrip) const;