Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use weight instead of duration for trip api #5930

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
- Windows:
- FIXED: Fix bit-shift overflow in MLD partition step. [#5878](https://github.com/Project-OSRM/osrm-backend/pull/5878)
- FIXED: Fix vector bool permutation in graph contraction step [#5882](https://github.com/Project-OSRM/osrm-backend/pull/5882)
- FIXED: Fix trip api to use weight instead of duration internally [#5930](https://github.com/Project-OSRM/osrm-backend/pull/5930)


# 5.23.0
Expand Down
24 changes: 13 additions & 11 deletions include/engine/routing_algorithms.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,12 @@ class RoutingAlgorithmsInterface
virtual InternalRouteResult
DirectShortestPathSearch(const PhantomNodes &phantom_node_pair) const = 0;

virtual std::pair<std::vector<EdgeDuration>, std::vector<EdgeDistance>>
ManyToManySearch(const std::vector<PhantomNode> &phantom_nodes,
const std::vector<std::size_t> &source_indices,
const std::vector<std::size_t> &target_indices,
const bool calculate_distance) const = 0;
virtual std::
tuple<std::vector<EdgeWeight>, std::vector<EdgeDuration>, std::vector<EdgeDistance>>
ManyToManySearch(const std::vector<PhantomNode> &phantom_nodes,
const std::vector<std::size_t> &source_indices,
const std::vector<std::size_t> &target_indices,
const bool calculate_distance) const = 0;

virtual routing_algorithms::SubMatchingList
MapMatching(const routing_algorithms::CandidateLists &candidates_list,
Expand Down Expand Up @@ -83,11 +84,12 @@ template <typename Algorithm> class RoutingAlgorithms final : public RoutingAlgo
InternalRouteResult
DirectShortestPathSearch(const PhantomNodes &phantom_nodes) const final override;

virtual std::pair<std::vector<EdgeDuration>, std::vector<EdgeDistance>>
ManyToManySearch(const std::vector<PhantomNode> &phantom_nodes,
const std::vector<std::size_t> &source_indices,
const std::vector<std::size_t> &target_indices,
const bool calculate_distance) const final override;
virtual std::
tuple<std::vector<EdgeWeight>, std::vector<EdgeDuration>, std::vector<EdgeDistance>>
ManyToManySearch(const std::vector<PhantomNode> &phantom_nodes,
const std::vector<std::size_t> &source_indices,
const std::vector<std::size_t> &target_indices,
const bool calculate_distance) const final override;

routing_algorithms::SubMatchingList
MapMatching(const routing_algorithms::CandidateLists &candidates_list,
Expand Down Expand Up @@ -192,7 +194,7 @@ inline routing_algorithms::SubMatchingList RoutingAlgorithms<Algorithm>::MapMatc
}

template <typename Algorithm>
std::pair<std::vector<EdgeDuration>, std::vector<EdgeDistance>>
std::tuple<std::vector<EdgeWeight>, std::vector<EdgeDuration>, std::vector<EdgeDistance>>
RoutingAlgorithms<Algorithm>::ManyToManySearch(const std::vector<PhantomNode> &phantom_nodes,
const std::vector<std::size_t> &_source_indices,
const std::vector<std::size_t> &_target_indices,
Expand Down
2 changes: 1 addition & 1 deletion include/engine/routing_algorithms/many_to_many.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ struct NodeBucket
} // namespace

template <typename Algorithm>
std::pair<std::vector<EdgeDuration>, std::vector<EdgeDistance>>
std::tuple<std::vector<EdgeWeight>, std::vector<EdgeDuration>, std::vector<EdgeDistance>>
manyToManySearch(SearchEngineData<Algorithm> &engine_working_data,
const DataFacade<Algorithm> &facade,
const std::vector<PhantomNode> &phantom_nodes,
Expand Down
36 changes: 20 additions & 16 deletions src/engine/plugins/table.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,13 @@ Status TablePlugin::HandleRequest(const RoutingAlgorithmsInterface &algorithms,
bool request_distance = params.annotations & api::TableParameters::AnnotationsType::Distance;
bool request_duration = params.annotations & api::TableParameters::AnnotationsType::Duration;

auto result_tables_pair = algorithms.ManyToManySearch(
auto result_tables_tuple = algorithms.ManyToManySearch(
snapped_phantoms, params.sources, params.destinations, request_distance);
auto &durations_table = std::get<1>(result_tables_tuple);
auto &distances_table = std::get<2>(result_tables_tuple);

if ((request_duration && result_tables_pair.first.empty()) ||
(request_distance && result_tables_pair.second.empty()))
if ((request_duration && durations_table.empty()) ||
(request_distance && distances_table.empty()))
{
return Error("NoTable", "No table found", result);
}
Expand All @@ -103,9 +105,9 @@ Status TablePlugin::HandleRequest(const RoutingAlgorithmsInterface &algorithms,
for (std::size_t column = 0; column < num_destinations; column++)
{
const auto &table_index = row * num_destinations + column;
BOOST_ASSERT(table_index < result_tables_pair.first.size());
BOOST_ASSERT(table_index < durations_table.size());
if (params.fallback_speed != INVALID_FALLBACK_SPEED && params.fallback_speed > 0 &&
result_tables_pair.first[table_index] == MAXIMAL_EDGE_DURATION)
durations_table[table_index] == MAXIMAL_EDGE_DURATION)
{
const auto &source =
snapped_phantoms[params.sources.empty() ? row : params.sources[row]];
Expand All @@ -121,38 +123,40 @@ Status TablePlugin::HandleRequest(const RoutingAlgorithmsInterface &algorithms,
: util::coordinate_calculation::fccApproximateDistance(
source.location, destination.location);

result_tables_pair.first[table_index] =
durations_table[table_index] =
distance_estimate / (double)params.fallback_speed;
if (!result_tables_pair.second.empty())
if (!distances_table.empty())
{
result_tables_pair.second[table_index] = distance_estimate;
distances_table[table_index] = distance_estimate;
}

estimated_pairs.emplace_back(row, column);
}
if (params.scale_factor > 0 && params.scale_factor != 1 &&
result_tables_pair.first[table_index] != MAXIMAL_EDGE_DURATION &&
result_tables_pair.first[table_index] != 0)
durations_table[table_index] != MAXIMAL_EDGE_DURATION &&
durations_table[table_index] != 0)
{
EdgeDuration diff =
MAXIMAL_EDGE_DURATION / result_tables_pair.first[table_index];
EdgeDuration diff = MAXIMAL_EDGE_DURATION / durations_table[table_index];

if (params.scale_factor >= diff)
{
result_tables_pair.first[table_index] = MAXIMAL_EDGE_DURATION - 1;
durations_table[table_index] = MAXIMAL_EDGE_DURATION - 1;
}
else
{
result_tables_pair.first[table_index] = std::lround(
result_tables_pair.first[table_index] * params.scale_factor);
durations_table[table_index] =
std::lround(durations_table[table_index] * params.scale_factor);
}
}
}
}
}

api::TableAPI table_api{facade, params};
table_api.MakeResponse(result_tables_pair, snapped_phantoms, estimated_pairs, result);
table_api.MakeResponse(std::make_pair(std::move(durations_table), std::move(distances_table)),
snapped_phantoms,
estimated_pairs,
result);

return Status::Ok;
}
Expand Down
41 changes: 21 additions & 20 deletions src/engine/plugins/trip.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -215,64 +215,65 @@ Status TripPlugin::HandleRequest(const RoutingAlgorithmsInterface &algorithms,

BOOST_ASSERT(snapped_phantoms.size() == number_of_locations);

// compute the duration table of all phantom nodes
auto result_duration_table = util::DistTableWrapper<EdgeWeight>(
algorithms.ManyToManySearch(snapped_phantoms, {}, {}, /*requestDistance*/ false).first,
// compute the weight table of all phantom nodes
auto result_weight_table = util::DistTableWrapper<EdgeWeight>(
std::get<0>(
algorithms.ManyToManySearch(snapped_phantoms, {}, {}, /*requestDistance*/ false)),
number_of_locations);

if (result_duration_table.size() == 0)
if (result_weight_table.size() == 0)
{
return Status::Error;
}

const constexpr std::size_t BF_MAX_FEASABLE = 10;
BOOST_ASSERT_MSG(result_duration_table.size() == number_of_locations * number_of_locations,
"Distance Table has wrong size");
BOOST_ASSERT_MSG(result_weight_table.size() == number_of_locations * number_of_locations,
"Weight Table has wrong size");

if (!IsStronglyConnectedComponent(result_duration_table))
if (!IsStronglyConnectedComponent(result_weight_table))
{
return Error("NoTrips", "No trip visiting all destinations possible.", result);
}

if (fixed_start && fixed_end)
{
ManipulateTableForFSE(source_id, destination_id, result_duration_table);
ManipulateTableForFSE(source_id, destination_id, result_weight_table);
}

std::vector<NodeID> duration_trip;
duration_trip.reserve(number_of_locations);
std::vector<NodeID> weight_trip;
weight_trip.reserve(number_of_locations);
// get an optimized order in which the destinations should be visited
if (number_of_locations < BF_MAX_FEASABLE)
{
duration_trip = trip::BruteForceTrip(number_of_locations, result_duration_table);
weight_trip = trip::BruteForceTrip(number_of_locations, result_weight_table);
}
else
{
duration_trip = trip::FarthestInsertionTrip(number_of_locations, result_duration_table);
weight_trip = trip::FarthestInsertionTrip(number_of_locations, result_weight_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(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));
auto desired_start_index = std::find(std::begin(weight_trip), std::end(weight_trip), 0);
BOOST_ASSERT(desired_start_index != std::end(weight_trip));
std::rotate(std::begin(weight_trip), desired_start_index, std::end(weight_trip));
}
else if (fixed_end && !fixed_start && parameters.roundtrip)
{
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));
std::find(std::begin(weight_trip), std::end(weight_trip), destination_id);
BOOST_ASSERT(desired_start_index != std::end(weight_trip));
std::rotate(std::begin(weight_trip), desired_start_index, std::end(weight_trip));
}

// get the route when visiting all destinations in optimized order
InternalRouteResult route =
ComputeRoute(algorithms, snapped_phantoms, duration_trip, parameters.roundtrip);
ComputeRoute(algorithms, snapped_phantoms, weight_trip, parameters.roundtrip);

// get api response
const std::vector<std::vector<NodeID>> trips = {duration_trip};
const std::vector<std::vector<NodeID>> trips = {std::move(weight_trip)};
const std::vector<InternalRouteResult> routes = {route};
api::TripAPI trip_api{facade, parameters};
trip_api.MakeResponse(trips, routes, snapped_phantoms, result);
Expand Down
5 changes: 3 additions & 2 deletions src/engine/routing_algorithms/many_to_many_ch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ void backwardRoutingStep(const DataFacade<Algorithm> &facade,
} // namespace ch

template <>
std::pair<std::vector<EdgeDuration>, std::vector<EdgeDistance>>
std::tuple<std::vector<EdgeWeight>, std::vector<EdgeDuration>, std::vector<EdgeDistance>>
manyToManySearch(SearchEngineData<ch::Algorithm> &engine_working_data,
const DataFacade<ch::Algorithm> &facade,
const std::vector<PhantomNode> &phantom_nodes,
Expand Down Expand Up @@ -248,7 +248,8 @@ manyToManySearch(SearchEngineData<ch::Algorithm> &engine_working_data,
}
}

return std::make_pair(std::move(durations_table), std::move(distances_table));
return std::make_tuple(
std::move(weights_table), std::move(durations_table), std::move(distances_table));
}

} // namespace routing_algorithms
Expand Down
12 changes: 7 additions & 5 deletions src/engine/routing_algorithms/many_to_many_mld.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ void relaxOutgoingEdges(
// Unidirectional multi-layer Dijkstra search for 1-to-N and N-to-1 matrices
//
template <bool DIRECTION>
std::pair<std::vector<EdgeDuration>, std::vector<EdgeDistance>>
std::tuple<std::vector<EdgeWeight>, std::vector<EdgeDuration>, std::vector<EdgeDistance>>
oneToManySearch(SearchEngineData<Algorithm> &engine_working_data,
const DataFacade<Algorithm> &facade,
const std::vector<PhantomNode> &phantom_nodes,
Expand Down Expand Up @@ -392,7 +392,8 @@ oneToManySearch(SearchEngineData<Algorithm> &engine_working_data,
facade, heapNode, query_heap, phantom_nodes, phantom_index, phantom_indices);
}

return std::make_pair(std::move(durations_table), std::move(distances_table));
return std::make_tuple(
std::move(weights_table), std::move(durations_table), std::move(distances_table));
}

//
Expand Down Expand Up @@ -521,7 +522,7 @@ void retrievePackedPathFromSearchSpace(NodeID middle_node_id,
}

template <bool DIRECTION>
std::pair<std::vector<EdgeDuration>, std::vector<EdgeDistance>>
std::tuple<std::vector<EdgeWeight>, std::vector<EdgeDuration>, std::vector<EdgeDistance>>
manyToManySearch(SearchEngineData<Algorithm> &engine_working_data,
const DataFacade<Algorithm> &facade,
const std::vector<PhantomNode> &phantom_nodes,
Expand Down Expand Up @@ -601,7 +602,8 @@ manyToManySearch(SearchEngineData<Algorithm> &engine_working_data,
}
}

return std::make_pair(std::move(durations_table), std::move(distances_table));
return std::make_tuple(
std::move(weights_table), std::move(durations_table), std::move(distances_table));
}

} // namespace mld
Expand All @@ -619,7 +621,7 @@ manyToManySearch(SearchEngineData<Algorithm> &engine_working_data,
// then search is performed on a reversed graph with phantom nodes with flipped roles and
// returning a transposed matrix.
template <>
std::pair<std::vector<EdgeDuration>, std::vector<EdgeDistance>>
std::tuple<std::vector<EdgeWeight>, std::vector<EdgeDuration>, std::vector<EdgeDistance>>
manyToManySearch(SearchEngineData<mld::Algorithm> &engine_working_data,
const DataFacade<mld::Algorithm> &facade,
const std::vector<PhantomNode> &phantom_nodes,
Expand Down