Unpack paths and return total distance in matrix plugin for CH (#4990)

This commit is contained in:
Kajari Ghosh
2018-04-20 18:18:55 -04:00
committed by GitHub
parent 9970b7d580
commit 14860b62e9
38 changed files with 1886 additions and 538 deletions
+24 -19
View File
@@ -131,7 +131,7 @@ void ManipulateTableForFSE(const std::size_t source_id,
result_table.SetValue(destination_id, i, INVALID_EDGE_WEIGHT);
}
// set destination->source to zero so rountrip treats source and
// set destination->source to zero so roundtrip treats source and
// destination as one location
result_table.SetValue(destination_id, source_id, 0);
@@ -216,61 +216,66 @@ Status TripPlugin::HandleRequest(const RoutingAlgorithmsInterface &algorithms,
BOOST_ASSERT(snapped_phantoms.size() == number_of_locations);
// compute the duration table of all phantom nodes
auto result_table = util::DistTableWrapper<EdgeWeight>(
algorithms.ManyToManySearch(snapped_phantoms, {}, {}), number_of_locations);
auto result_duration_table = util::DistTableWrapper<EdgeWeight>(
algorithms
.ManyToManySearch(
snapped_phantoms, {}, {}, /*requestDistance*/ false, /*requestDuration*/ true)
.first,
number_of_locations);
if (result_table.size() == 0)
if (result_duration_table.size() == 0)
{
return Status::Error;
}
const constexpr std::size_t BF_MAX_FEASABLE = 10;
BOOST_ASSERT_MSG(result_table.size() == number_of_locations * number_of_locations,
BOOST_ASSERT_MSG(result_duration_table.size() == number_of_locations * number_of_locations,
"Distance Table has wrong size");
if (!IsStronglyConnectedComponent(result_table))
if (!IsStronglyConnectedComponent(result_duration_table))
{
return Error("NoTrips", "No trip visiting all destinations possible.", json_result);
}
if (fixed_start && fixed_end)
{
ManipulateTableForFSE(source_id, destination_id, result_table);
ManipulateTableForFSE(source_id, destination_id, result_duration_table);
}
std::vector<NodeID> trip;
trip.reserve(number_of_locations);
std::vector<NodeID> duration_trip;
duration_trip.reserve(number_of_locations);
// get an optimized order in which the destinations should be visited
if (number_of_locations < BF_MAX_FEASABLE)
{
trip = trip::BruteForceTrip(number_of_locations, result_table);
duration_trip = trip::BruteForceTrip(number_of_locations, result_duration_table);
}
else
{
trip = trip::FarthestInsertionTrip(number_of_locations, result_table);
duration_trip = trip::FarthestInsertionTrip(number_of_locations, result_duration_table);
}
// rotate result such that roundtrip starts at node with index 0
// thist first if covers scenarios: !fixed_end || fixed_start || (fixed_start && fixed_end)
if (!fixed_end || fixed_start)
{
auto desired_start_index = std::find(std::begin(trip), std::end(trip), 0);
BOOST_ASSERT(desired_start_index != std::end(trip));
std::rotate(std::begin(trip), desired_start_index, std::end(trip));
auto desired_start_index = std::find(std::begin(duration_trip), std::end(duration_trip), 0);
BOOST_ASSERT(desired_start_index != std::end(duration_trip));
std::rotate(std::begin(duration_trip), desired_start_index, std::end(duration_trip));
}
else if (fixed_end && !fixed_start && parameters.roundtrip)
{
auto desired_start_index = std::find(std::begin(trip), std::end(trip), destination_id);
BOOST_ASSERT(desired_start_index != std::end(trip));
std::rotate(std::begin(trip), desired_start_index, std::end(trip));
auto desired_start_index =
std::find(std::begin(duration_trip), std::end(duration_trip), destination_id);
BOOST_ASSERT(desired_start_index != std::end(duration_trip));
std::rotate(std::begin(duration_trip), desired_start_index, std::end(duration_trip));
}
// get the route when visiting all destinations in optimized order
InternalRouteResult route =
ComputeRoute(algorithms, snapped_phantoms, trip, parameters.roundtrip);
ComputeRoute(algorithms, snapped_phantoms, duration_trip, parameters.roundtrip);
// get api response
const std::vector<std::vector<NodeID>> trips = {trip};
const std::vector<std::vector<NodeID>> trips = {duration_trip};
const std::vector<InternalRouteResult> routes = {route};
api::TripAPI trip_api{facade, parameters};
trip_api.MakeResponse(trips, routes, snapped_phantoms, json_result);