2015-04-22 11:19:54 -04:00
|
|
|
/*
|
|
|
|
|
|
|
|
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_HPP
|
|
|
|
#define ROUND_TRIP_HPP
|
|
|
|
|
|
|
|
#include "plugin_base.hpp"
|
|
|
|
|
|
|
|
#include "../algorithms/object_encoder.hpp"
|
2015-08-19 04:04:36 -04:00
|
|
|
#include "../algorithms/tarjan_scc.hpp"
|
2015-06-16 17:20:38 -04:00
|
|
|
#include "../routing_algorithms/tsp_nearest_neighbour.hpp"
|
|
|
|
#include "../routing_algorithms/tsp_farthest_insertion.hpp"
|
|
|
|
#include "../routing_algorithms/tsp_brute_force.hpp"
|
2015-04-22 11:19:54 -04:00
|
|
|
#include "../data_structures/query_edge.hpp"
|
|
|
|
#include "../data_structures/search_engine.hpp"
|
2015-07-04 18:15:55 -04:00
|
|
|
#include "../data_structures/matrix_graph_wrapper.hpp"
|
2015-08-19 04:04:36 -04:00
|
|
|
#include "../data_structures/restriction.hpp"
|
|
|
|
#include "../data_structures/restriction_map.hpp"
|
2015-04-22 11:19:54 -04:00
|
|
|
#include "../descriptors/descriptor_base.hpp"
|
2015-04-23 10:30:55 -04:00
|
|
|
#include "../descriptors/json_descriptor.hpp"
|
2015-04-22 11:19:54 -04:00
|
|
|
#include "../util/json_renderer.hpp"
|
|
|
|
#include "../util/make_unique.hpp"
|
|
|
|
#include "../util/string_util.hpp"
|
|
|
|
#include "../util/timing_util.hpp"
|
2015-04-23 10:30:55 -04:00
|
|
|
#include "../util/simple_logger.hpp"
|
2015-08-19 08:04:59 -04:00
|
|
|
#include "../util/dist_table_wrapper.hpp"
|
2015-04-22 11:19:54 -04:00
|
|
|
|
|
|
|
#include <osrm/json_container.hpp>
|
2015-08-18 07:48:12 -04:00
|
|
|
#include <boost/assert.hpp>
|
2015-04-22 11:19:54 -04:00
|
|
|
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <algorithm>
|
|
|
|
#include <memory>
|
|
|
|
#include <unordered_map>
|
|
|
|
#include <string>
|
2015-08-19 08:04:59 -04:00
|
|
|
#include <utility>
|
2015-04-22 11:19:54 -04:00
|
|
|
#include <vector>
|
2015-05-27 05:47:48 -04:00
|
|
|
#include <limits>
|
2015-08-19 19:41:36 -04:00
|
|
|
#include <map>
|
2015-08-18 07:48:12 -04:00
|
|
|
#include <iterator>
|
|
|
|
|
|
|
|
#include <iostream>
|
2015-04-22 11:19:54 -04:00
|
|
|
|
|
|
|
template <class DataFacadeT> class RoundTripPlugin final : public BasePlugin
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
std::string descriptor_string;
|
|
|
|
DataFacadeT *facade;
|
|
|
|
std::unique_ptr<SearchEngine<DataFacadeT>> search_engine_ptr;
|
|
|
|
|
2015-05-26 19:32:59 -04:00
|
|
|
public:
|
|
|
|
explicit RoundTripPlugin(DataFacadeT *facade)
|
|
|
|
: descriptor_string("trip"), facade(facade)
|
|
|
|
{
|
|
|
|
search_engine_ptr = osrm::make_unique<SearchEngine<DataFacadeT>>(facade);
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::string GetDescriptor() const override final { return descriptor_string; }
|
|
|
|
|
2015-07-01 08:07:25 -04:00
|
|
|
void GetPhantomNodes(const RouteParameters &route_parameters, PhantomNodeArray & phantom_node_vector) {
|
2015-05-26 19:32:59 -04:00
|
|
|
const bool checksum_OK = (route_parameters.check_sum == facade->GetCheckSum());
|
|
|
|
|
|
|
|
// find phantom nodes for all input coords
|
2015-07-01 08:07:25 -04:00
|
|
|
for (const auto i : osrm::irange<std::size_t>(0, route_parameters.coordinates.size())) {
|
2015-05-26 19:32:59 -04:00
|
|
|
// if client hints are helpful, encode hints
|
|
|
|
if (checksum_OK && i < route_parameters.hints.size() &&
|
2015-07-01 08:07:25 -04:00
|
|
|
!route_parameters.hints[i].empty()) {
|
2015-05-26 19:32:59 -04:00
|
|
|
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);
|
2015-07-01 08:07:25 -04:00
|
|
|
if (phantom_node_vector[i].size() > 1) {
|
2015-08-19 08:04:59 -04:00
|
|
|
phantom_node_vector[i].erase(std::begin(phantom_node_vector[i]));
|
2015-05-26 19:32:59 -04:00
|
|
|
}
|
|
|
|
BOOST_ASSERT(phantom_node_vector[i].front().is_valid(facade->GetNumberOfNodes()));
|
|
|
|
}
|
2015-07-01 08:07:25 -04:00
|
|
|
}
|
2015-05-26 19:32:59 -04:00
|
|
|
|
2015-08-19 19:41:36 -04:00
|
|
|
struct SCC_Component{
|
|
|
|
SCC_Component(std::vector<NodeID> in_component,
|
|
|
|
std::vector<size_t> in_component_range)
|
|
|
|
: component(in_component), component_range(in_component_range) {
|
|
|
|
component_range.push_back(in_component.size());
|
|
|
|
};
|
|
|
|
|
|
|
|
SCC_Component(std::vector<NodeID> in_component)
|
|
|
|
: component(in_component), component_range({0, in_component.size()}) {
|
|
|
|
};
|
|
|
|
|
|
|
|
std::size_t GetNumberOfComponents() const{
|
|
|
|
return component_range.size() - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::vector<NodeID> component;
|
|
|
|
std::vector<size_t> component_range;
|
|
|
|
};
|
|
|
|
|
|
|
|
SCC_Component SplitUnaccessibleLocations(const std::size_t number_of_locations,
|
|
|
|
const DistTableWrapper<EdgeWeight> & result_table) {
|
2015-07-01 05:32:23 -04:00
|
|
|
|
2015-07-04 18:15:55 -04:00
|
|
|
// Run TarjanSCC
|
2015-08-19 08:04:59 -04:00
|
|
|
auto wrapper = std::make_shared<MatrixGraphWrapper<EdgeWeight>>(result_table.GetTable(), number_of_locations);
|
2015-08-19 04:04:36 -04:00
|
|
|
auto scc = TarjanSCC<MatrixGraphWrapper<EdgeWeight>>(wrapper);
|
2015-07-04 18:15:55 -04:00
|
|
|
scc.run();
|
|
|
|
|
2015-08-19 19:41:36 -04:00
|
|
|
std::vector<size_t> range_insertion;
|
|
|
|
std::vector<size_t> component_range;
|
|
|
|
range_insertion.reserve(scc.get_number_of_components());
|
|
|
|
component_range.reserve(scc.get_number_of_components());
|
|
|
|
|
|
|
|
std::vector<NodeID> components(number_of_locations, 0);
|
|
|
|
|
|
|
|
auto prefix = 0;
|
2015-08-19 04:04:36 -04:00
|
|
|
for (size_t j = 0; j < scc.get_number_of_components(); ++j){
|
2015-08-19 19:41:36 -04:00
|
|
|
range_insertion.push_back(prefix);
|
|
|
|
component_range.push_back(prefix);
|
|
|
|
prefix += scc.get_component_size(j);
|
2015-07-04 18:15:55 -04:00
|
|
|
}
|
2015-07-01 05:32:23 -04:00
|
|
|
|
2015-08-19 04:04:36 -04:00
|
|
|
for (size_t i = 0; i < number_of_locations; ++i) {
|
2015-08-19 19:41:36 -04:00
|
|
|
components[range_insertion[scc.get_component_id(i)]] = i;
|
|
|
|
++range_insertion[scc.get_component_id(i)];
|
2015-07-01 05:32:23 -04:00
|
|
|
}
|
2015-08-19 19:41:36 -04:00
|
|
|
|
|
|
|
return SCC_Component(components, component_range);
|
2015-07-01 08:07:25 -04:00
|
|
|
}
|
2015-06-22 14:09:00 -04:00
|
|
|
|
2015-08-19 19:41:36 -04:00
|
|
|
void SetLocPermutationOutput(const std::vector<NodeID> & loc_permutation, osrm::json::Object & json_result){
|
2015-06-22 14:09:00 -04:00
|
|
|
osrm::json::Array json_loc_permutation;
|
2015-08-19 08:04:59 -04:00
|
|
|
json_loc_permutation.values.insert(std::end(json_loc_permutation.values), std::begin(loc_permutation), std::end(loc_permutation));
|
2015-06-22 14:09:00 -04:00
|
|
|
json_result.values["loc_permutation"] = json_loc_permutation;
|
2015-07-04 18:15:55 -04:00
|
|
|
}
|
2015-06-16 17:20:38 -04:00
|
|
|
|
2015-07-04 18:15:55 -04:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2015-08-19 19:41:36 -04:00
|
|
|
void SetGeometry(const RouteParameters &route_parameters,
|
|
|
|
const InternalRouteResult & min_route,
|
|
|
|
osrm::json::Object & json_result) {
|
2015-06-16 17:20:38 -04:00
|
|
|
// return geometry result to json
|
2015-04-22 11:19:54 -04:00
|
|
|
std::unique_ptr<BaseDescriptor<DataFacadeT>> descriptor;
|
|
|
|
descriptor = osrm::make_unique<JSONDescriptor<DataFacadeT>>(facade);
|
2015-05-27 05:47:48 -04:00
|
|
|
|
2015-04-22 11:19:54 -04:00
|
|
|
descriptor->SetConfig(route_parameters);
|
2015-06-22 14:09:00 -04:00
|
|
|
descriptor->Run(min_route, json_result);
|
2015-07-01 08:07:25 -04:00
|
|
|
}
|
|
|
|
|
2015-08-18 07:48:12 -04:00
|
|
|
void ComputeRoute(const PhantomNodeArray & phantom_node_vector,
|
|
|
|
const RouteParameters & route_parameters,
|
2015-08-19 04:04:36 -04:00
|
|
|
const std::vector<NodeID> & trip,
|
2015-08-18 07:48:12 -04:00
|
|
|
InternalRouteResult & min_route) {
|
|
|
|
// given he final trip, compute total distance and return the route and location permutation
|
|
|
|
PhantomNodes viapoint;
|
2015-08-19 19:41:36 -04:00
|
|
|
for (auto it = std::begin(trip); it != std::end(trip); ++it) {
|
|
|
|
const auto from_node = *it;
|
|
|
|
const auto to_node = std::next(it) != std::end(trip) ? *std::next(it) : *std::begin(trip);
|
2015-08-18 07:48:12 -04:00
|
|
|
viapoint = PhantomNodes{phantom_node_vector[from_node][0], phantom_node_vector[to_node][0]};
|
|
|
|
min_route.segment_end_coordinates.emplace_back(viapoint);
|
|
|
|
}
|
|
|
|
|
2015-08-19 19:41:36 -04:00
|
|
|
search_engine_ptr->shortest_path(min_route.segment_end_coordinates, route_parameters.uturns, min_route);
|
2015-08-18 07:48:12 -04:00
|
|
|
}
|
|
|
|
|
2015-07-01 08:07:25 -04:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2015-08-19 19:41:36 -04:00
|
|
|
// get phantom nodes
|
2015-07-01 08:07:25 -04:00
|
|
|
PhantomNodeArray phantom_node_vector(route_parameters.coordinates.size());
|
|
|
|
GetPhantomNodes(route_parameters, phantom_node_vector);
|
2015-08-19 08:04:59 -04:00
|
|
|
auto number_of_locations = phantom_node_vector.size();
|
2015-07-01 08:07:25 -04:00
|
|
|
|
|
|
|
// compute the distance table of all phantom nodes
|
2015-08-19 08:04:59 -04:00
|
|
|
const auto result_table = DistTableWrapper<EdgeWeight>(*search_engine_ptr->distance_table(phantom_node_vector),
|
|
|
|
number_of_locations);
|
|
|
|
if (result_table.size() == 0){
|
2015-07-01 08:07:25 -04:00
|
|
|
return 400;
|
|
|
|
}
|
|
|
|
|
2015-08-19 04:04:36 -04:00
|
|
|
const constexpr std::size_t BF_MAX_FEASABLE = 10;
|
2015-08-19 08:04:59 -04:00
|
|
|
BOOST_ASSERT_MSG(result_table.size() > 0, "Distance Table is empty.");
|
2015-08-19 05:35:36 -04:00
|
|
|
|
2015-08-19 19:41:36 -04:00
|
|
|
// get scc components
|
|
|
|
SCC_Component scc = [&](){
|
|
|
|
if (*std::max_element(result_table.begin(), result_table.end()) == INVALID_EDGE_WEIGHT) {
|
|
|
|
// compute all scc with tarjan
|
|
|
|
return SplitUnaccessibleLocations(number_of_locations, result_table);
|
|
|
|
} else {
|
|
|
|
// whole graph is one scc
|
|
|
|
std::vector<NodeID> location_ids(number_of_locations);
|
|
|
|
std::iota(std::begin(location_ids), std::end(location_ids), 0);
|
|
|
|
return SCC_Component(location_ids);
|
|
|
|
}
|
|
|
|
}();
|
|
|
|
|
|
|
|
|
|
|
|
using NodeIDIterator = typename std::vector<NodeID>::const_iterator;
|
2015-08-19 05:35:36 -04:00
|
|
|
|
|
|
|
std::vector<std::vector<NodeID>> res_route;
|
|
|
|
TIMER_START(tsp);
|
|
|
|
//run TSP computation for every SCC
|
2015-08-19 19:41:36 -04:00
|
|
|
for(auto k = 0; k < scc.GetNumberOfComponents(); ++k) {
|
|
|
|
const auto component_size = scc.component_range[k+1] - scc.component_range[k];
|
|
|
|
if (component_size > 1) {
|
2015-08-19 05:35:36 -04:00
|
|
|
std::vector<NodeID> scc_route;
|
2015-08-19 19:41:36 -04:00
|
|
|
NodeIDIterator start = std::begin(scc.component) + scc.component_range[k];
|
|
|
|
NodeIDIterator end = std::begin(scc.component) + scc.component_range[k+1];
|
2015-08-19 05:35:36 -04:00
|
|
|
|
|
|
|
// Compute the TSP with the given algorithm
|
|
|
|
if (route_parameters.tsp_algo == "BF" && route_parameters.coordinates.size() < BF_MAX_FEASABLE) {
|
|
|
|
SimpleLogger().Write() << "Running brute force";
|
2015-08-19 19:41:36 -04:00
|
|
|
scc_route = osrm::tsp::BruteForceTSP(start, end, number_of_locations, result_table);
|
2015-08-19 05:35:36 -04:00
|
|
|
res_route.push_back(scc_route);
|
|
|
|
} else if (route_parameters.tsp_algo == "NN") {
|
|
|
|
SimpleLogger().Write() << "Running nearest neighbour";
|
2015-08-19 19:41:36 -04:00
|
|
|
scc_route = osrm::tsp::NearestNeighbourTSP(start, end, number_of_locations, result_table);
|
2015-08-19 05:35:36 -04:00
|
|
|
res_route.push_back(scc_route);
|
|
|
|
} else if (route_parameters.tsp_algo == "FI") {
|
|
|
|
SimpleLogger().Write() << "Running farthest insertion";
|
2015-08-19 19:41:36 -04:00
|
|
|
scc_route = osrm::tsp::FarthestInsertionTSP(start, end, number_of_locations, result_table);
|
2015-08-19 05:35:36 -04:00
|
|
|
res_route.push_back(scc_route);
|
|
|
|
} else{
|
|
|
|
SimpleLogger().Write() << "Running farthest insertion";
|
2015-08-19 19:41:36 -04:00
|
|
|
scc_route = osrm::tsp::FarthestInsertionTSP(start, end, number_of_locations, result_table);
|
2015-08-19 05:35:36 -04:00
|
|
|
res_route.push_back(scc_route);
|
2015-07-04 18:15:55 -04:00
|
|
|
}
|
2015-08-19 19:41:36 -04:00
|
|
|
|
|
|
|
SimpleLogger().Write() << "Route #"
|
|
|
|
<< k << ": "
|
|
|
|
<< [&scc_route](){
|
|
|
|
std::string s = "";
|
|
|
|
for (auto x : scc_route) {
|
|
|
|
s += std::to_string(x) + " ";
|
|
|
|
}
|
|
|
|
return s;
|
|
|
|
}();
|
2015-07-04 18:15:55 -04:00
|
|
|
}
|
|
|
|
}
|
2015-08-19 19:41:36 -04:00
|
|
|
std::vector<InternalRouteResult> comp_route (res_route.size());
|
|
|
|
for (auto r = 0; r < res_route.size(); ++r) {
|
|
|
|
ComputeRoute(phantom_node_vector, route_parameters, res_route[r], comp_route[r]);
|
|
|
|
}
|
2015-08-19 05:35:36 -04:00
|
|
|
TIMER_STOP(tsp);
|
2015-08-19 19:41:36 -04:00
|
|
|
|
2015-08-19 05:35:36 -04:00
|
|
|
SetRuntimeOutput(TIMER_MSEC(tsp), json_result);
|
2015-08-19 19:41:36 -04:00
|
|
|
//TODO
|
|
|
|
SetLocPermutationOutput(res_route[0], json_result);
|
2015-08-19 05:35:36 -04:00
|
|
|
|
2015-08-19 19:41:36 -04:00
|
|
|
std::unique_ptr<BaseDescriptor<DataFacadeT>> descriptor;
|
|
|
|
descriptor = osrm::make_unique<JSONDescriptor<DataFacadeT>>(facade);
|
|
|
|
descriptor->SetConfig(route_parameters);
|
2015-08-19 05:35:36 -04:00
|
|
|
auto dist = 0;
|
2015-08-19 19:41:36 -04:00
|
|
|
for (auto r : comp_route) {
|
|
|
|
dist += r.shortest_path_length;
|
|
|
|
// SetGeometry(route_parameters, r, json_result);
|
|
|
|
descriptor->Run(r, json_result);
|
2015-08-19 05:35:36 -04:00
|
|
|
}
|
|
|
|
SetDistanceOutput(dist, json_result);
|
2015-05-27 05:47:48 -04:00
|
|
|
|
2015-04-22 11:19:54 -04:00
|
|
|
|
2015-04-23 10:30:55 -04:00
|
|
|
|
2015-04-22 11:19:54 -04:00
|
|
|
return 200;
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif // ROUND_TRIP_HPP
|