From 6191b6bee230ef345a216353318d77bf64c0ab3b Mon Sep 17 00:00:00 2001 From: Chau Nguyen Date: Fri, 10 Jul 2015 12:25:35 -0400 Subject: [PATCH] add parameter to choose algorithm for tsp calculation and remove redundant code --- data_structures/route_parameters.cpp | 5 + include/osrm/route_parameters.hpp | 3 + library/osrm_impl.cpp | 6 - plugins/round_trip.hpp | 37 +++-- plugins/round_trip_BF.hpp | 217 ------------------------- plugins/round_trip_FI.hpp | 207 ----------------------- plugins/round_trip_NN.hpp | 211 ------------------------ routing_algorithms/tsp_brute_force.hpp | 1 - server/api_grammar.hpp | 7 +- 9 files changed, 39 insertions(+), 655 deletions(-) delete mode 100644 plugins/round_trip_BF.hpp delete mode 100644 plugins/round_trip_FI.hpp delete mode 100644 plugins/round_trip_NN.hpp diff --git a/data_structures/route_parameters.cpp b/data_structures/route_parameters.cpp index 86b955358..31e91c1fa 100644 --- a/data_structures/route_parameters.cpp +++ b/data_structures/route_parameters.cpp @@ -122,6 +122,11 @@ void RouteParameters::setLanguage(const std::string &language_string) language = language_string; } +void RouteParameters::setTSPAlgo(const std::string &tsp_algo_string) +{ + tsp_algo = tsp_algo_string; +} + void RouteParameters::setGeometryFlag(const bool flag) { geometry = flag; } void RouteParameters::setCompressionFlag(const bool flag) { compression = flag; } diff --git a/include/osrm/route_parameters.hpp b/include/osrm/route_parameters.hpp index bc413d73b..94ed3a336 100644 --- a/include/osrm/route_parameters.hpp +++ b/include/osrm/route_parameters.hpp @@ -81,6 +81,8 @@ struct RouteParameters void getCoordinatesFromGeometry(const std::string geometry_string); + void setTSPAlgo(const std::string &tsp_algo); + short zoom_level; bool print_instructions; bool alternate_route; @@ -97,6 +99,7 @@ struct RouteParameters std::string output_format; std::string jsonp_parameter; std::string language; + std::string tsp_algo; std::vector hints; std::vector timestamps; std::vector uturns; diff --git a/library/osrm_impl.cpp b/library/osrm_impl.cpp index 26ba83e53..8057069eb 100644 --- a/library/osrm_impl.cpp +++ b/library/osrm_impl.cpp @@ -42,9 +42,6 @@ class named_mutex; #include "../plugins/nearest.hpp" #include "../plugins/timestamp.hpp" #include "../plugins/round_trip.hpp" -#include "../plugins/round_trip_NN.hpp" -#include "../plugins/round_trip_BF.hpp" -#include "../plugins/round_trip_FI.hpp" #include "../plugins/viaroute.hpp" #include "../plugins/match.hpp" #include "../server/data_structures/datafacade_base.hpp" @@ -91,9 +88,6 @@ OSRM_impl::OSRM_impl(libosrm_config &lib_config) RegisterPlugin(new TimestampPlugin>(query_data_facade)); RegisterPlugin(new ViaRoutePlugin>(query_data_facade)); RegisterPlugin(new RoundTripPlugin>(query_data_facade)); - RegisterPlugin(new RoundTripPluginNN>(query_data_facade)); - RegisterPlugin(new RoundTripPluginBF>(query_data_facade)); - RegisterPlugin(new RoundTripPluginFI>(query_data_facade)); } OSRM_impl::~OSRM_impl() diff --git a/plugins/round_trip.hpp b/plugins/round_trip.hpp index bdb42a6ef..03840824a 100644 --- a/plugins/round_trip.hpp +++ b/plugins/round_trip.hpp @@ -158,9 +158,10 @@ template class RoundTripPlugin final : public BasePlugin return 400; } - + //check if locations are in different strongly connected components (SCC) const auto maxint = std::numeric_limits::max(); if (*std::max_element(result_table->begin(), result_table->end()) == maxint) { + //run TSP computation for every SCC std::unique_ptr> descriptor; descriptor = osrm::make_unique>(facade); descriptor->SetConfig(route_parameters); @@ -170,35 +171,49 @@ template class RoundTripPlugin final : public BasePlugin SplitUnaccessibleLocations(phantom_node_vector, *result_table, components); auto number_of_locations = phantom_node_vector.size(); std::vector min_loc_permutation(number_of_locations, -1); + auto min_dist = 0; for(auto k = 0; k < components.size(); ++k) { if (components[k].size() > 1) { + // Compute the TSP with the given algorithm InternalRouteResult min_route; - osrm::tsp::FarthestInsertionTSP(components[k], phantom_node_vector, *result_table, min_route, min_loc_permutation); + if (route_parameters.tsp_algo == "BF" && route_parameters.coordinates.size() < 14) { + osrm::tsp::BruteForceTSP(components[k], phantom_node_vector, *result_table, min_route, min_loc_permutation); + } else if (route_parameters.tsp_algo == "NN") { + osrm::tsp::NearestNeighbourTSP(components[k], phantom_node_vector, *result_table, min_route, min_loc_permutation); + } else if (route_parameters.tsp_algo == "FI") { + osrm::tsp::FarthestInsertionTSP(components[k], phantom_node_vector, *result_table, min_route, min_loc_permutation); + } else{ + osrm::tsp::FarthestInsertionTSP(components[k], phantom_node_vector, *result_table, min_route, min_loc_permutation); + } search_engine_ptr->shortest_path(min_route.segment_end_coordinates, route_parameters.uturns, min_route); min_dist += min_route.shortest_path_length; descriptor->Run(min_route, json_result); } } - TIMER_STOP(tsp); + TIMER_STOP(tsp); SetRuntimeOutput(TIMER_MSEC(tsp), json_result); SetDistanceOutput(min_dist, json_result); SetLocPermutationOutput(min_loc_permutation, json_result); - } else { + } else { //run TSP computation for all locations auto number_of_locations = phantom_node_vector.size(); InternalRouteResult min_route; std::vector min_loc_permutation(number_of_locations, -1); - //######################## FARTHEST INSERTION ###############################// + + // Compute the TSP with the given algorithm TIMER_START(tsp); - osrm::tsp::FarthestInsertionTSP(phantom_node_vector, *result_table, min_route, min_loc_permutation); + if (route_parameters.tsp_algo == "BF" && route_parameters.coordinates.size() < 14) { + osrm::tsp::BruteForceTSP(phantom_node_vector, *result_table, min_route, min_loc_permutation); + } else if (route_parameters.tsp_algo == "NN") { + osrm::tsp::NearestNeighbourTSP(phantom_node_vector, *result_table, min_route, min_loc_permutation); + } else if (route_parameters.tsp_algo == "FI") { + osrm::tsp::FarthestInsertionTSP(phantom_node_vector, *result_table, min_route, min_loc_permutation); + } else { + osrm::tsp::FarthestInsertionTSP(phantom_node_vector, *result_table, min_route, min_loc_permutation); + } search_engine_ptr->shortest_path(min_route.segment_end_coordinates, route_parameters.uturns, min_route); TIMER_STOP(tsp); - // //######################### NEAREST NEIGHBOUR ###############################// - // TIMER_START(tsp); - // osrm::tsp::NearestNeighbourTSP(phantom_node_vector, *result_table, min_route, min_loc_permutation); - // search_engine_ptr->shortest_path(min_route.segment_end_coordinates, route_parameters.uturns, min_route); - // TIMER_STOP(tsp); BOOST_ASSERT(min_route.segment_end_coordinates.size() == route_parameters.coordinates.size()); SetLocPermutationOutput(min_loc_permutation, json_result); SetDistanceOutput(min_route.shortest_path_length, json_result); diff --git a/plugins/round_trip_BF.hpp b/plugins/round_trip_BF.hpp deleted file mode 100644 index 10510aa03..000000000 --- a/plugins/round_trip_BF.hpp +++ /dev/null @@ -1,217 +0,0 @@ - -/* - -Copyright (c) 2015, Project OSRM contributors -All rights reserved. - -Redistribution and use in source and binary forms, with or without modification, -are permitted provided that the following conditions are met: - -Redistributions of source code must retain the above copyright notice, this list -of conditions and the following disclaimer. -Redistributions in binary form must reproduce the above copyright notice, this -list of conditions and the following disclaimer in the documentation and/or -other materials provided with the distribution. - -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND -ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED -WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE -DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR -ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES -(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; -LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON -ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS -SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - -*/ - -#ifndef ROUND_TRIP_BF_HPP -#define ROUND_TRIP_BF_HPP - -#include "plugin_base.hpp" - -#include "../algorithms/object_encoder.hpp" -#include "../routing_algorithms/tsp_nearest_neighbour.hpp" -#include "../routing_algorithms/tsp_farthest_insertion.hpp" -#include "../routing_algorithms/tsp_brute_force.hpp" -#include "../data_structures/query_edge.hpp" -#include "../data_structures/search_engine.hpp" -#include "../descriptors/descriptor_base.hpp" -#include "../descriptors/json_descriptor.hpp" -#include "../util/json_renderer.hpp" -#include "../util/make_unique.hpp" -#include "../util/string_util.hpp" -#include "../util/timing_util.hpp" -#include "../util/simple_logger.hpp" - -#include - -#include -#include -#include -#include -#include -#include -#include - -template class RoundTripPluginBF final : public BasePlugin -{ - private: - std::string descriptor_string; - DataFacadeT *facade; - std::unique_ptr> search_engine_ptr; - - public: - explicit RoundTripPluginBF(DataFacadeT *facade) - : descriptor_string("tripBF"), facade(facade) - { - search_engine_ptr = osrm::make_unique>(facade); - } - - const std::string GetDescriptor() const override final { return descriptor_string; } - - void GetPhantomNodes(const RouteParameters &route_parameters, PhantomNodeArray & phantom_node_vector) { - const bool checksum_OK = (route_parameters.check_sum == facade->GetCheckSum()); - - // find phantom nodes for all input coords - for (const auto i : osrm::irange(0, route_parameters.coordinates.size())) { - // if client hints are helpful, encode hints - if (checksum_OK && i < route_parameters.hints.size() && - !route_parameters.hints[i].empty()) { - PhantomNode current_phantom_node; - ObjectEncoder::DecodeFromBase64(route_parameters.hints[i], current_phantom_node); - if (current_phantom_node.is_valid(facade->GetNumberOfNodes())) - { - phantom_node_vector[i].emplace_back(std::move(current_phantom_node)); - continue; - } - } - facade->IncrementalFindPhantomNodeForCoordinate(route_parameters.coordinates[i], - phantom_node_vector[i], 1); - if (phantom_node_vector[i].size() > 1) { - phantom_node_vector[i].erase(phantom_node_vector[i].begin()); - } - BOOST_ASSERT(phantom_node_vector[i].front().is_valid(facade->GetNumberOfNodes())); - } - } - - void SplitUnaccessibleLocations(PhantomNodeArray & phantom_node_vector, - std::vector & result_table, - std::vector> & components) { - // Run TarjanSCC - auto number_of_locations = phantom_node_vector.size(); - auto wrapper = std::make_shared>(result_table, number_of_locations); - auto empty_restriction = RestrictionMap(std::vector()); - auto empty_vector = std::vector(); - auto scc = TarjanSCC>(wrapper, empty_restriction, empty_vector); - scc.run(); - - for (int j = 0; j < scc.get_number_of_components(); ++j){ - components.push_back(std::vector()); - } - - for (int i = 0; i < number_of_locations; ++i) { - components[scc.get_component_id(i)].push_back(i); - } - } - - void SetLocPermutationOutput(const std::vector & loc_permutation, osrm::json::Object & json_result){ - osrm::json::Array json_loc_permutation; - json_loc_permutation.values.insert(json_loc_permutation.values.end(), loc_permutation.begin(), loc_permutation.end()); - json_result.values["loc_permutation"] = json_loc_permutation; - } - - void SetDistanceOutput(const int distance, osrm::json::Object & json_result) { - json_result.values["distance"] = distance; - } - - void SetRuntimeOutput(const float runtime, osrm::json::Object & json_result) { - json_result.values["runtime"] = runtime; - } - - void SetGeometry(const RouteParameters &route_parameters, const InternalRouteResult & min_route, osrm::json::Object & json_result) { - // return geometry result to json - std::unique_ptr> descriptor; - descriptor = osrm::make_unique>(facade); - - descriptor->SetConfig(route_parameters); - descriptor->Run(min_route, json_result); - } - - - int HandleRequest(const RouteParameters &route_parameters, - osrm::json::Object &json_result) override final - { - // check if all inputs are coordinates - if (!check_all_coordinates(route_parameters.coordinates)) { - return 400; - } - - PhantomNodeArray phantom_node_vector(route_parameters.coordinates.size()); - GetPhantomNodes(route_parameters, phantom_node_vector); - - // compute the distance table of all phantom nodes - const std::shared_ptr> result_table = - search_engine_ptr->distance_table(phantom_node_vector); - if (!result_table){ - return 400; - } - - if (route_parameters.coordinates.size() < 14) { - const auto maxint = std::numeric_limits::max(); - if (*std::max_element(result_table->begin(), result_table->end()) == maxint) { - std::unique_ptr> descriptor; - descriptor = osrm::make_unique>(facade); - descriptor->SetConfig(route_parameters); - - std::vector> components; - TIMER_START(tsp); - SplitUnaccessibleLocations(phantom_node_vector, *result_table, components); - - std::vector min_loc_permutation(phantom_node_vector.size(), -1); - auto min_dist = 0; - for(auto k = 0; k < components.size(); ++k) { - if (components[k].size() > 1) { - InternalRouteResult min_route; - //run nearest neighbour - osrm::tsp::BruteForceTSP(components[k], phantom_node_vector, *result_table, min_route, min_loc_permutation); - //compute route - search_engine_ptr->shortest_path(min_route.segment_end_coordinates, route_parameters.uturns, min_route); - //return geometry - min_dist += min_route.shortest_path_length; - descriptor->Run(min_route, json_result); - } - } - TIMER_STOP(tsp); - - SetRuntimeOutput(TIMER_MSEC(tsp), json_result); - SetDistanceOutput(min_dist, json_result); - SetLocPermutationOutput(min_loc_permutation, json_result); - } else { - auto number_of_locations = phantom_node_vector.size(); - InternalRouteResult min_route; - std::vector min_loc_permutation(number_of_locations, -1); - //########################### BRUTE FORCE ####################################// - TIMER_START(tsp); - osrm::tsp::BruteForceTSP(phantom_node_vector, *result_table, min_route, min_loc_permutation); - search_engine_ptr->shortest_path(min_route.segment_end_coordinates, route_parameters.uturns, min_route); - TIMER_STOP(tsp); - - BOOST_ASSERT(min_route.segment_end_coordinates.size() == route_parameters.coordinates.size()); - - SetLocPermutationOutput(min_loc_permutation, json_result); - SetDistanceOutput(min_route.shortest_path_length, json_result); - SetRuntimeOutput(TIMER_MSEC(tsp), json_result); - SetGeometry(route_parameters, min_route, json_result); - } - } else { - SetRuntimeOutput(-1, json_result); - SetDistanceOutput(-1, json_result); - } - return 200; - } - -}; - -#endif // ROUND_TRIP_BF_HPP diff --git a/plugins/round_trip_FI.hpp b/plugins/round_trip_FI.hpp deleted file mode 100644 index c52075bd3..000000000 --- a/plugins/round_trip_FI.hpp +++ /dev/null @@ -1,207 +0,0 @@ -/* - -Copyright (c) 2015, Project OSRM contributors -All rights reserved. - -Redistribution and use in source and binary forms, with or without modification, -are permitted provided that the following conditions are met: - -Redistributions of source code must retain the above copyright notice, this list -of conditions and the following disclaimer. -Redistributions in binary form must reproduce the above copyright notice, this -list of conditions and the following disclaimer in the documentation and/or -other materials provided with the distribution. - -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND -ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED -WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE -DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR -ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES -(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; -LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON -ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS -SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - -*/ - -#ifndef ROUND_TRIP_FI_HPP -#define ROUND_TRIP_FI_HPP - -#include "plugin_base.hpp" - -#include "../algorithms/object_encoder.hpp" -#include "../routing_algorithms/tsp_nearest_neighbour.hpp" -#include "../routing_algorithms/tsp_farthest_insertion.hpp" -#include "../routing_algorithms/tsp_brute_force.hpp" -#include "../data_structures/query_edge.hpp" -#include "../data_structures/search_engine.hpp" -#include "../descriptors/descriptor_base.hpp" -#include "../descriptors/json_descriptor.hpp" -#include "../util/json_renderer.hpp" -#include "../util/make_unique.hpp" -#include "../util/string_util.hpp" -#include "../util/timing_util.hpp" -#include "../util/simple_logger.hpp" - -#include - -#include -#include -#include -#include -#include -#include -#include - -template class RoundTripPluginFI final : public BasePlugin -{ - private: - std::string descriptor_string; - DataFacadeT *facade; - std::unique_ptr> search_engine_ptr; - - public: - explicit RoundTripPluginFI(DataFacadeT *facade) - : descriptor_string("tripFI"), facade(facade) - { - search_engine_ptr = osrm::make_unique>(facade); - } - - const std::string GetDescriptor() const override final { return descriptor_string; } - - void GetPhantomNodes(const RouteParameters &route_parameters, PhantomNodeArray & phantom_node_vector) { - const bool checksum_OK = (route_parameters.check_sum == facade->GetCheckSum()); - - // find phantom nodes for all input coords - for (const auto i : osrm::irange(0, route_parameters.coordinates.size())) { - // if client hints are helpful, encode hints - if (checksum_OK && i < route_parameters.hints.size() && - !route_parameters.hints[i].empty()) { - PhantomNode current_phantom_node; - ObjectEncoder::DecodeFromBase64(route_parameters.hints[i], current_phantom_node); - if (current_phantom_node.is_valid(facade->GetNumberOfNodes())) - { - phantom_node_vector[i].emplace_back(std::move(current_phantom_node)); - continue; - } - } - facade->IncrementalFindPhantomNodeForCoordinate(route_parameters.coordinates[i], - phantom_node_vector[i], 1); - if (phantom_node_vector[i].size() > 1) { - phantom_node_vector[i].erase(phantom_node_vector[i].begin()); - } - BOOST_ASSERT(phantom_node_vector[i].front().is_valid(facade->GetNumberOfNodes())); - } - } - - void SplitUnaccessibleLocations(PhantomNodeArray & phantom_node_vector, - std::vector & result_table, - std::vector> & components) { - // Run TarjanSCC - auto number_of_locations = phantom_node_vector.size(); - auto wrapper = std::make_shared>(result_table, number_of_locations); - auto empty_restriction = RestrictionMap(std::vector()); - auto empty_vector = std::vector(); - auto scc = TarjanSCC>(wrapper, empty_restriction, empty_vector); - scc.run(); - - for (int j = 0; j < scc.get_number_of_components(); ++j){ - components.push_back(std::vector()); - } - - for (int i = 0; i < number_of_locations; ++i) { - components[scc.get_component_id(i)].push_back(i); - } - } - - void SetLocPermutationOutput(const std::vector & loc_permutation, osrm::json::Object & json_result){ - osrm::json::Array json_loc_permutation; - json_loc_permutation.values.insert(json_loc_permutation.values.end(), loc_permutation.begin(), loc_permutation.end()); - json_result.values["loc_permutation"] = json_loc_permutation; - } - - void SetDistanceOutput(const int distance, osrm::json::Object & json_result) { - json_result.values["distance"] = distance; - } - - void SetRuntimeOutput(const float runtime, osrm::json::Object & json_result) { - json_result.values["runtime"] = runtime; - } - - void SetGeometry(const RouteParameters &route_parameters, const InternalRouteResult & min_route, osrm::json::Object & json_result) { - // return geometry result to json - std::unique_ptr> descriptor; - descriptor = osrm::make_unique>(facade); - - descriptor->SetConfig(route_parameters); - descriptor->Run(min_route, json_result); - } - - int HandleRequest(const RouteParameters &route_parameters, - osrm::json::Object &json_result) override final - { - // check if all inputs are coordinates - if (!check_all_coordinates(route_parameters.coordinates)) { - return 400; - } - - PhantomNodeArray phantom_node_vector(route_parameters.coordinates.size()); - GetPhantomNodes(route_parameters, phantom_node_vector); - - // compute the distance table of all phantom nodes - const std::shared_ptr> result_table = - search_engine_ptr->distance_table(phantom_node_vector); - if (!result_table){ - return 400; - } - - const auto maxint = std::numeric_limits::max(); - if (*std::max_element(result_table->begin(), result_table->end()) == maxint) { - std::unique_ptr> descriptor; - descriptor = osrm::make_unique>(facade); - descriptor->SetConfig(route_parameters); - - std::vector> components; - TIMER_START(tsp); - SplitUnaccessibleLocations(phantom_node_vector, *result_table, components); - auto number_of_locations = phantom_node_vector.size(); - std::vector min_loc_permutation(number_of_locations, -1); - auto min_dist = 0; - for(auto k = 0; k < components.size(); ++k) { - if (components[k].size() > 1) { - InternalRouteResult min_route; - osrm::tsp::FarthestInsertionTSP(components[k], phantom_node_vector, *result_table, min_route, min_loc_permutation); - search_engine_ptr->shortest_path(min_route.segment_end_coordinates, route_parameters.uturns, min_route); - min_dist += min_route.shortest_path_length; - descriptor->Run(min_route, json_result); - } - } - TIMER_STOP(tsp); - - SetRuntimeOutput(TIMER_MSEC(tsp), json_result); - SetDistanceOutput(min_dist, json_result); - SetLocPermutationOutput(min_loc_permutation, json_result); - } else { - auto number_of_locations = phantom_node_vector.size(); - InternalRouteResult min_route; - std::vector min_loc_permutation(number_of_locations, -1); - //######################## FARTHEST INSERTION ###############################// - TIMER_START(tsp); - osrm::tsp::FarthestInsertionTSP(phantom_node_vector, *result_table, min_route, min_loc_permutation); - search_engine_ptr->shortest_path(min_route.segment_end_coordinates, route_parameters.uturns, min_route); - TIMER_STOP(tsp); - - BOOST_ASSERT(min_route.segment_end_coordinates.size() == route_parameters.coordinates.size()); - SetLocPermutationOutput(min_loc_permutation, json_result); - SetDistanceOutput(min_route.shortest_path_length, json_result); - SetRuntimeOutput(TIMER_MSEC(tsp), json_result); - SetGeometry(route_parameters, min_route, json_result); - } - - return 200; - } - -}; - -#endif // ROUND_TRIP_FI_HPP diff --git a/plugins/round_trip_NN.hpp b/plugins/round_trip_NN.hpp deleted file mode 100644 index cc88d899b..000000000 --- a/plugins/round_trip_NN.hpp +++ /dev/null @@ -1,211 +0,0 @@ -/* - -Copyright (c) 2015, Project OSRM contributors -All rights reserved. - -Redistribution and use in source and binary forms, with or without modification, -are permitted provided that the following conditions are met: - -Redistributions of source code must retain the above copyright notice, this list -of conditions and the following disclaimer. -Redistributions in binary form must reproduce the above copyright notice, this -list of conditions and the following disclaimer in the documentation and/or -other materials provided with the distribution. - -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND -ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED -WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE -DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR -ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES -(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; -LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON -ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS -SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - -*/ - -#ifndef ROUND_TRIP_NN_HPP -#define ROUND_TRIP_NN_HPP - -#include "plugin_base.hpp" - -#include "../algorithms/object_encoder.hpp" -#include "../routing_algorithms/tsp_nearest_neighbour.hpp" -#include "../routing_algorithms/tsp_farthest_insertion.hpp" -#include "../routing_algorithms/tsp_brute_force.hpp" -#include "../data_structures/query_edge.hpp" -#include "../data_structures/search_engine.hpp" -#include "../descriptors/descriptor_base.hpp" -#include "../descriptors/json_descriptor.hpp" -#include "../util/json_renderer.hpp" -#include "../util/make_unique.hpp" -#include "../util/string_util.hpp" -#include "../util/timing_util.hpp" -#include "../util/simple_logger.hpp" -#include "../tools/tsp_logs.hpp" - -#include - -#include -#include -#include -#include -#include -#include -#include - -template class RoundTripPluginNN final : public BasePlugin -{ - private: - std::string descriptor_string; - DataFacadeT *facade; - std::unique_ptr> search_engine_ptr; - - public: - explicit RoundTripPluginNN(DataFacadeT *facade) - : descriptor_string("tripNN"), facade(facade) - { - search_engine_ptr = osrm::make_unique>(facade); - } - - const std::string GetDescriptor() const override final { return descriptor_string; } - - void GetPhantomNodes(const RouteParameters &route_parameters, PhantomNodeArray & phantom_node_vector) { - const bool checksum_OK = (route_parameters.check_sum == facade->GetCheckSum()); - - // find phantom nodes for all input coords - for (const auto i : osrm::irange(0, route_parameters.coordinates.size())) { - // if client hints are helpful, encode hints - if (checksum_OK && i < route_parameters.hints.size() && - !route_parameters.hints[i].empty()) { - PhantomNode current_phantom_node; - ObjectEncoder::DecodeFromBase64(route_parameters.hints[i], current_phantom_node); - if (current_phantom_node.is_valid(facade->GetNumberOfNodes())) - { - phantom_node_vector[i].emplace_back(std::move(current_phantom_node)); - continue; - } - } - facade->IncrementalFindPhantomNodeForCoordinate(route_parameters.coordinates[i], - phantom_node_vector[i], 1); - if (phantom_node_vector[i].size() > 1) { - phantom_node_vector[i].erase(phantom_node_vector[i].begin()); - } - BOOST_ASSERT(phantom_node_vector[i].front().is_valid(facade->GetNumberOfNodes())); - } - } - - void SplitUnaccessibleLocations(PhantomNodeArray & phantom_node_vector, - std::vector & result_table, - std::vector> & components) { - // Run TarjanSCC - auto number_of_locations = phantom_node_vector.size(); - auto wrapper = std::make_shared>(result_table, number_of_locations); - auto empty_restriction = RestrictionMap(std::vector()); - auto empty_vector = std::vector(); - auto scc = TarjanSCC>(wrapper, empty_restriction, empty_vector); - scc.run(); - - for (int j = 0; j < scc.get_number_of_components(); ++j){ - components.push_back(std::vector()); - } - - for (int i = 0; i < number_of_locations; ++i) { - components[scc.get_component_id(i)].push_back(i); - } - } - - void SetLocPermutationOutput(const std::vector & loc_permutation, osrm::json::Object & json_result){ - osrm::json::Array json_loc_permutation; - json_loc_permutation.values.insert(json_loc_permutation.values.end(), loc_permutation.begin(), loc_permutation.end()); - json_result.values["loc_permutation"] = json_loc_permutation; - } - - void SetDistanceOutput(const int distance, osrm::json::Object & json_result) { - json_result.values["distance"] = distance; - } - - void SetRuntimeOutput(const float runtime, osrm::json::Object & json_result) { - json_result.values["runtime"] = runtime; - } - - void SetGeometry(const RouteParameters &route_parameters, const InternalRouteResult & min_route, osrm::json::Object & json_result) { - // return geometry result to json - std::unique_ptr> descriptor; - descriptor = osrm::make_unique>(facade); - - descriptor->SetConfig(route_parameters); - descriptor->Run(min_route, json_result); - } - - int HandleRequest(const RouteParameters &route_parameters, - osrm::json::Object &json_result) override final - { - // check if all inputs are coordinates - if (!check_all_coordinates(route_parameters.coordinates)) { - return 400; - } - - PhantomNodeArray phantom_node_vector(route_parameters.coordinates.size()); - GetPhantomNodes(route_parameters, phantom_node_vector); - - // compute the distance table of all phantom nodes - const std::shared_ptr> result_table = - search_engine_ptr->distance_table(phantom_node_vector); - if (!result_table){ - return 400; - } - - - const auto maxint = std::numeric_limits::max(); - if (*std::max_element(result_table->begin(), result_table->end()) == maxint) { - std::unique_ptr> descriptor; - descriptor = osrm::make_unique>(facade); - descriptor->SetConfig(route_parameters); - - std::vector> components; - TIMER_START(tsp); - SplitUnaccessibleLocations(phantom_node_vector, *result_table, components); - - std::vector min_loc_permutation(phantom_node_vector.size(), -1); - auto min_dist = 0; - for(auto k = 0; k < components.size(); ++k) { - if (components[k].size() > 1) { - InternalRouteResult min_route; - //run nearest neighbour - osrm::tsp::NearestNeighbourTSP(components[k], phantom_node_vector, *result_table, min_route, min_loc_permutation); - //compute route - search_engine_ptr->shortest_path(min_route.segment_end_coordinates, route_parameters.uturns, min_route); - //return geometry - min_dist += min_route.shortest_path_length; - descriptor->Run(min_route, json_result); - } - } - TIMER_STOP(tsp); - - SetRuntimeOutput(TIMER_MSEC(tsp), json_result); - SetDistanceOutput(min_dist, json_result); - SetLocPermutationOutput(min_loc_permutation, json_result); - } else { - auto number_of_locations = phantom_node_vector.size(); - InternalRouteResult min_route; - std::vector min_loc_permutation(number_of_locations, -1); - //######################### NEAREST NEIGHBOUR ###############################// - TIMER_START(tsp); - osrm::tsp::NearestNeighbourTSP(phantom_node_vector, *result_table, min_route, min_loc_permutation); - search_engine_ptr->shortest_path(min_route.segment_end_coordinates, route_parameters.uturns, min_route); - TIMER_STOP(tsp); - BOOST_ASSERT(min_route.segment_end_coordinates.size() == route_parameters.coordinates.size()); - SetLocPermutationOutput(min_loc_permutation, json_result); - SetDistanceOutput(min_route.shortest_path_length, json_result); - SetRuntimeOutput(TIMER_MSEC(tsp), json_result); - SetGeometry(route_parameters, min_route, json_result); - } - - return 200; - } - -}; - -#endif // ROUND_TRIP_NN_HPP diff --git a/routing_algorithms/tsp_brute_force.hpp b/routing_algorithms/tsp_brute_force.hpp index 9e0c266f2..eac32aa15 100644 --- a/routing_algorithms/tsp_brute_force.hpp +++ b/routing_algorithms/tsp_brute_force.hpp @@ -79,7 +79,6 @@ void BruteForceTSP(std::vector & location, InternalRouteResult & min_route, std::vector & min_loc_permutation) { - const auto number_of_location = phantom_node_vector.size(); const int component_size = location.size(); int min_route_dist = std::numeric_limits::max(); diff --git a/server/api_grammar.hpp b/server/api_grammar.hpp index 325f4c356..c93518a34 100644 --- a/server/api_grammar.hpp +++ b/server/api_grammar.hpp @@ -42,7 +42,7 @@ template struct APIGrammar : qi::grammar> -(uturns); query = ('?') >> (+(zoom | output | jsonp | checksum | location | hint | timestamp | u | cmp | language | instruction | geometry | alt_route | old_API | num_results | - matching_beta | gps_precision | classify | locs)); + matching_beta | gps_precision | classify | tsp_algo | locs)); zoom = (-qi::lit('&')) >> qi::lit('z') >> '=' >> qi::short_[boost::bind(&HandlerT::setZoomLevel, handler, ::_1)]; @@ -85,6 +85,8 @@ template struct APIGrammar : qi::grammar> qi::lit("locs") >> '=' >> stringforPolyline[boost::bind(&HandlerT::getCoordinatesFromGeometry, handler, ::_1)]; + tsp_algo = (-qi::lit('&')) >> qi::lit("tsp_algo") >> '=' >> + string[boost::bind(&HandlerT::setTSPAlgo, handler, ::_1)]; string = +(qi::char_("a-zA-Z")); stringwithDot = +(qi::char_("a-zA-Z0-9_.-")); @@ -96,7 +98,8 @@ template struct APIGrammar : qi::grammar api_call, query; qi::rule service, zoom, output, string, jsonp, checksum, location, hint, timestamp, stringwithDot, stringwithPercent, language, instruction, geometry, cmp, alt_route, u, - uturns, old_API, num_results, matching_beta, gps_precision, classify, locs, stringforPolyline; + uturns, old_API, num_results, matching_beta, gps_precision, classify, locs, stringforPolyline, tsp_algo; + uturns, old_API, num_results, matching_beta, gps_precision, classify, tsp_algo; HandlerT *handler; };