fix json output such that each trip returns a json object with all information of the trip
This commit is contained in:
parent
93835b9b94
commit
47fbd2a2b5
@ -106,25 +106,41 @@ template <class DataFacadeT> class RoundTripPlugin final : public BasePlugin
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Object to hold all strongly connected components (scc) of a graph
|
||||||
|
// to access all graphs with component ID i, get the iterators by:
|
||||||
|
// auto start = std::begin(scc_component.component) + scc_component.range[i];
|
||||||
|
// auto end = std::begin(scc_component.component) + scc_component.range[i+1];
|
||||||
struct SCC_Component{
|
struct SCC_Component{
|
||||||
|
// in_component: all NodeIDs sorted by component ID
|
||||||
|
// in_range: index where a new component starts
|
||||||
|
//
|
||||||
|
// example: NodeID 0, 1, 2, 4, 5 are in component 0
|
||||||
|
// NodeID 3, 6, 7, 8 are in component 1
|
||||||
|
// => in_component = [0, 1, 2, 4, 5, 3, 6, 7, 8]
|
||||||
|
// => in_range = [0, 5]
|
||||||
SCC_Component(std::vector<NodeID> in_component,
|
SCC_Component(std::vector<NodeID> in_component,
|
||||||
std::vector<size_t> in_component_range)
|
std::vector<size_t> in_range)
|
||||||
: component(in_component), component_range(in_component_range) {
|
: component(in_component), range(in_range) {
|
||||||
component_range.push_back(in_component.size());
|
range.push_back(in_component.size());
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// constructor to use when whole graph is one single scc
|
||||||
SCC_Component(std::vector<NodeID> in_component)
|
SCC_Component(std::vector<NodeID> in_component)
|
||||||
: component(in_component), component_range({0, in_component.size()}) {
|
: component(in_component), range({0, in_component.size()}) {
|
||||||
};
|
};
|
||||||
|
|
||||||
std::size_t GetNumberOfComponents() const{
|
std::size_t GetNumberOfComponents() const{
|
||||||
return component_range.size() - 1;
|
return range.size() - 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
const std::vector<NodeID> component;
|
const std::vector<NodeID> component;
|
||||||
std::vector<size_t> component_range;
|
// component range = in_range + [component.size()]
|
||||||
|
std::vector<size_t> range;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// takes the number of locations and its distance matrix,
|
||||||
|
// identifies and splits the graph in its strongly connected components (scc)
|
||||||
|
// and returns an SCC_Component
|
||||||
SCC_Component SplitUnaccessibleLocations(const std::size_t number_of_locations,
|
SCC_Component SplitUnaccessibleLocations(const std::size_t number_of_locations,
|
||||||
const DistTableWrapper<EdgeWeight> & result_table) {
|
const DistTableWrapper<EdgeWeight> & result_table) {
|
||||||
|
|
||||||
@ -134,16 +150,16 @@ template <class DataFacadeT> class RoundTripPlugin final : public BasePlugin
|
|||||||
scc.run();
|
scc.run();
|
||||||
|
|
||||||
std::vector<size_t> range_insertion;
|
std::vector<size_t> range_insertion;
|
||||||
std::vector<size_t> component_range;
|
std::vector<size_t> range;
|
||||||
range_insertion.reserve(scc.get_number_of_components());
|
range_insertion.reserve(scc.get_number_of_components());
|
||||||
component_range.reserve(scc.get_number_of_components());
|
range.reserve(scc.get_number_of_components());
|
||||||
|
|
||||||
std::vector<NodeID> components(number_of_locations, 0);
|
std::vector<NodeID> components(number_of_locations, 0);
|
||||||
|
|
||||||
auto prefix = 0;
|
auto prefix = 0;
|
||||||
for (size_t j = 0; j < scc.get_number_of_components(); ++j){
|
for (size_t j = 0; j < scc.get_number_of_components(); ++j){
|
||||||
range_insertion.push_back(prefix);
|
range_insertion.push_back(prefix);
|
||||||
component_range.push_back(prefix);
|
range.push_back(prefix);
|
||||||
prefix += scc.get_component_size(j);
|
prefix += scc.get_component_size(j);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -152,31 +168,16 @@ template <class DataFacadeT> class RoundTripPlugin final : public BasePlugin
|
|||||||
++range_insertion[scc.get_component_id(i)];
|
++range_insertion[scc.get_component_id(i)];
|
||||||
}
|
}
|
||||||
|
|
||||||
return SCC_Component(components, component_range);
|
return SCC_Component(components, range);
|
||||||
}
|
}
|
||||||
|
|
||||||
void SetLocPermutationOutput(const std::vector<NodeID> & loc_permutation, osrm::json::Object & json_result){
|
void SetLocPermutationOutput(const std::vector<NodeID> & permutation,
|
||||||
osrm::json::Array json_loc_permutation;
|
osrm::json::Object & json_result){
|
||||||
json_loc_permutation.values.insert(std::end(json_loc_permutation.values), std::begin(loc_permutation), std::end(loc_permutation));
|
osrm::json::Array json_permutation;
|
||||||
json_result.values["loc_permutation"] = json_loc_permutation;
|
json_permutation.values.insert(std::end(json_permutation.values),
|
||||||
}
|
std::begin(permutation),
|
||||||
|
std::end(permutation));
|
||||||
void SetDistanceOutput(const int distance, osrm::json::Object & json_result) {
|
json_result.values["permutation"] = json_permutation;
|
||||||
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<BaseDescriptor<DataFacadeT>> descriptor;
|
|
||||||
descriptor = osrm::make_unique<JSONDescriptor<DataFacadeT>>(facade);
|
|
||||||
|
|
||||||
descriptor->SetConfig(route_parameters);
|
|
||||||
descriptor->Run(min_route, json_result);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void ComputeRoute(const PhantomNodeArray & phantom_node_vector,
|
void ComputeRoute(const PhantomNodeArray & phantom_node_vector,
|
||||||
@ -187,6 +188,7 @@ template <class DataFacadeT> class RoundTripPlugin final : public BasePlugin
|
|||||||
PhantomNodes viapoint;
|
PhantomNodes viapoint;
|
||||||
for (auto it = std::begin(trip); it != std::end(trip); ++it) {
|
for (auto it = std::begin(trip); it != std::end(trip); ++it) {
|
||||||
const auto from_node = *it;
|
const auto from_node = *it;
|
||||||
|
// if from_node is the last node, compute the route from the last to the first location
|
||||||
const auto to_node = std::next(it) != std::end(trip) ? *std::next(it) : *std::begin(trip);
|
const auto to_node = std::next(it) != std::end(trip) ? *std::next(it) : *std::begin(trip);
|
||||||
viapoint = PhantomNodes{phantom_node_vector[from_node][0], phantom_node_vector[to_node][0]};
|
viapoint = PhantomNodes{phantom_node_vector[from_node][0], phantom_node_vector[to_node][0]};
|
||||||
min_route.segment_end_coordinates.emplace_back(viapoint);
|
min_route.segment_end_coordinates.emplace_back(viapoint);
|
||||||
@ -234,33 +236,34 @@ template <class DataFacadeT> class RoundTripPlugin final : public BasePlugin
|
|||||||
|
|
||||||
using NodeIDIterator = typename std::vector<NodeID>::const_iterator;
|
using NodeIDIterator = typename std::vector<NodeID>::const_iterator;
|
||||||
|
|
||||||
std::vector<std::vector<NodeID>> res_route;
|
std::vector<std::vector<NodeID>> route_result;
|
||||||
|
route_result.reserve(scc.GetNumberOfComponents());
|
||||||
TIMER_START(tsp);
|
TIMER_START(tsp);
|
||||||
//run TSP computation for every SCC
|
//run TSP computation for every SCC
|
||||||
for(auto k = 0; k < scc.GetNumberOfComponents(); ++k) {
|
for(auto k = 0; k < scc.GetNumberOfComponents(); ++k) {
|
||||||
const auto component_size = scc.component_range[k+1] - scc.component_range[k];
|
const auto component_size = scc.range[k+1] - scc.range[k];
|
||||||
if (component_size > 1) {
|
if (component_size > 1) {
|
||||||
std::vector<NodeID> scc_route;
|
std::vector<NodeID> scc_route;
|
||||||
NodeIDIterator start = std::begin(scc.component) + scc.component_range[k];
|
NodeIDIterator start = std::begin(scc.component) + scc.range[k];
|
||||||
NodeIDIterator end = std::begin(scc.component) + scc.component_range[k+1];
|
NodeIDIterator end = std::begin(scc.component) + scc.range[k+1];
|
||||||
|
|
||||||
// Compute the TSP with the given algorithm
|
// Compute the TSP with the given algorithm
|
||||||
if (route_parameters.tsp_algo == "BF" && route_parameters.coordinates.size() < BF_MAX_FEASABLE) {
|
if (route_parameters.tsp_algo == "BF" && route_parameters.coordinates.size() < BF_MAX_FEASABLE) {
|
||||||
SimpleLogger().Write() << "Running brute force";
|
SimpleLogger().Write() << "Running brute force";
|
||||||
scc_route = osrm::tsp::BruteForceTSP(start, end, number_of_locations, result_table);
|
scc_route = osrm::tsp::BruteForceTSP(start, end, number_of_locations, result_table);
|
||||||
res_route.push_back(scc_route);
|
route_result.push_back(scc_route);
|
||||||
} else if (route_parameters.tsp_algo == "NN") {
|
} else if (route_parameters.tsp_algo == "NN") {
|
||||||
SimpleLogger().Write() << "Running nearest neighbour";
|
SimpleLogger().Write() << "Running nearest neighbour";
|
||||||
scc_route = osrm::tsp::NearestNeighbourTSP(start, end, number_of_locations, result_table);
|
scc_route = osrm::tsp::NearestNeighbourTSP(start, end, number_of_locations, result_table);
|
||||||
res_route.push_back(scc_route);
|
route_result.push_back(scc_route);
|
||||||
} else if (route_parameters.tsp_algo == "FI") {
|
} else if (route_parameters.tsp_algo == "FI") {
|
||||||
SimpleLogger().Write() << "Running farthest insertion";
|
SimpleLogger().Write() << "Running farthest insertion";
|
||||||
scc_route = osrm::tsp::FarthestInsertionTSP(start, end, number_of_locations, result_table);
|
scc_route = osrm::tsp::FarthestInsertionTSP(start, end, number_of_locations, result_table);
|
||||||
res_route.push_back(scc_route);
|
route_result.push_back(scc_route);
|
||||||
} else{
|
} else{
|
||||||
SimpleLogger().Write() << "Running farthest insertion";
|
SimpleLogger().Write() << "Running farthest insertion";
|
||||||
scc_route = osrm::tsp::FarthestInsertionTSP(start, end, number_of_locations, result_table);
|
scc_route = osrm::tsp::FarthestInsertionTSP(start, end, number_of_locations, result_table);
|
||||||
res_route.push_back(scc_route);
|
route_result.push_back(scc_route);
|
||||||
}
|
}
|
||||||
|
|
||||||
SimpleLogger().Write() << "Route #"
|
SimpleLogger().Write() << "Route #"
|
||||||
@ -272,28 +275,40 @@ template <class DataFacadeT> class RoundTripPlugin final : public BasePlugin
|
|||||||
}
|
}
|
||||||
return s;
|
return s;
|
||||||
}();
|
}();
|
||||||
|
} else {
|
||||||
|
route_result.push_back({scc.component[scc.range[k]]});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
std::vector<InternalRouteResult> comp_route (res_route.size());
|
|
||||||
for (auto r = 0; r < res_route.size(); ++r) {
|
// compute all round trip routes
|
||||||
ComputeRoute(phantom_node_vector, route_parameters, res_route[r], comp_route[r]);
|
std::vector<InternalRouteResult> comp_route (route_result.size());
|
||||||
|
for (auto r = 0; r < route_result.size(); ++r) {
|
||||||
|
ComputeRoute(phantom_node_vector, route_parameters, route_result[r], comp_route[r]);
|
||||||
}
|
}
|
||||||
|
|
||||||
TIMER_STOP(tsp);
|
TIMER_STOP(tsp);
|
||||||
|
|
||||||
SetRuntimeOutput(TIMER_MSEC(tsp), json_result);
|
// prepare JSON output
|
||||||
//TODO
|
|
||||||
SetLocPermutationOutput(res_route[0], json_result);
|
|
||||||
|
|
||||||
|
|
||||||
|
// create a json object for every trip
|
||||||
|
osrm::json::Array trip;
|
||||||
|
for (auto i = 0; i < route_result.size(); ++i) {
|
||||||
std::unique_ptr<BaseDescriptor<DataFacadeT>> descriptor;
|
std::unique_ptr<BaseDescriptor<DataFacadeT>> descriptor;
|
||||||
descriptor = osrm::make_unique<JSONDescriptor<DataFacadeT>>(facade);
|
descriptor = osrm::make_unique<JSONDescriptor<DataFacadeT>>(facade);
|
||||||
descriptor->SetConfig(route_parameters);
|
descriptor->SetConfig(route_parameters);
|
||||||
auto dist = 0;
|
|
||||||
for (auto r : comp_route) {
|
osrm::json::Object scc_trip;
|
||||||
dist += r.shortest_path_length;
|
|
||||||
// SetGeometry(route_parameters, r, json_result);
|
// set permutation output
|
||||||
descriptor->Run(r, json_result);
|
SetLocPermutationOutput(route_result[i], scc_trip);
|
||||||
|
// set viaroute output
|
||||||
|
descriptor->Run(comp_route[i], scc_trip);
|
||||||
|
|
||||||
|
trip.values.push_back(scc_trip);
|
||||||
}
|
}
|
||||||
SetDistanceOutput(dist, json_result);
|
|
||||||
|
json_result.values["trips"] = trip;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user