and add todos of code review session with daniel-j-h
This commit is contained in:
Huyen Chau Nguyen
2015-08-18 13:48:12 +02:00
parent 6191b6bee2
commit 77e9e95067
5 changed files with 302 additions and 268 deletions
+118 -33
View File
@@ -47,6 +47,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "../util/simple_logger.hpp"
#include <osrm/json_container.hpp>
#include <boost/assert.hpp>
#include <cstdlib>
#include <algorithm>
@@ -55,6 +56,9 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include <string>
#include <vector>
#include <limits>
#include <iterator>
#include <iostream>
template <class DataFacadeT> class RoundTripPlugin final : public BasePlugin
{
@@ -102,10 +106,10 @@ template <class DataFacadeT> class RoundTripPlugin final : public BasePlugin
std::vector<std::vector<unsigned>> & components) {
// Run TarjanSCC
auto number_of_locations = phantom_node_vector.size();
const auto number_of_locations = phantom_node_vector.size();
auto wrapper = std::make_shared<MatrixGraphWrapper<EdgeWeight>>(result_table, number_of_locations);
auto empty_restriction = RestrictionMap(std::vector<TurnRestriction>());
auto empty_vector = std::vector<bool>();
std::vector<bool> empty_vector;
auto scc = TarjanSCC<MatrixGraphWrapper<EdgeWeight>>(wrapper, empty_restriction, empty_vector);
scc.run();
@@ -118,7 +122,8 @@ template <class DataFacadeT> class RoundTripPlugin final : public BasePlugin
}
}
void SetLocPermutationOutput(const std::vector<int> & loc_permutation, osrm::json::Object & json_result){
template <typename number>
void SetLocPermutationOutput(const std::vector<number> & 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;
@@ -140,6 +145,36 @@ template <class DataFacadeT> class RoundTripPlugin final : public BasePlugin
descriptor->Run(min_route, json_result);
}
void ComputeRoute(const PhantomNodeArray & phantom_node_vector,
const RouteParameters & route_parameters,
std::vector<unsigned> & trip,
InternalRouteResult & min_route) {
// given he final trip, compute total distance and return the route and location permutation
PhantomNodes viapoint;
for (auto it = trip.begin(); it != std::prev(trip.end()); ++it) {
auto from_node = *it;
auto to_node = *std::next(it);
viapoint = PhantomNodes{phantom_node_vector[from_node][0], phantom_node_vector[to_node][0]};
min_route.segment_end_coordinates.emplace_back(viapoint);
}
// check dist between last and first location too
viapoint = PhantomNodes{phantom_node_vector[*std::prev(trip.end())][0], phantom_node_vector[trip.front()][0]};
min_route.segment_end_coordinates.emplace_back(viapoint);
search_engine_ptr->shortest_path(min_route.segment_end_coordinates, route_parameters.uturns, min_route);
}
void ComputeRoute(const PhantomNodeArray & phantom_node_vector,
const RouteParameters & route_parameters,
std::vector<std::vector<unsigned>> & trip,
std::vector<InternalRouteResult> & route) {
for (const auto & curr_trip : trip) {
InternalRouteResult curr_route;
ComputeRoute(phantom_node_vector, route_parameters, curr_trip, curr_route);
route.push_back(curr_route);
search_engine_ptr->shortest_path(route.back().segment_end_coordinates, route_parameters.uturns, route.back());
}
}
int HandleRequest(const RouteParameters &route_parameters,
osrm::json::Object &json_result) override final
{
@@ -158,67 +193,117 @@ template <class DataFacadeT> class RoundTripPlugin final : public BasePlugin
return 400;
}
BOOST_ASSERT_MSG(result_table->size() > 0, "Distance Table is empty.");
//check if locations are in different strongly connected components (SCC)
const auto maxint = std::numeric_limits<int>::max();
if (*std::max_element(result_table->begin(), result_table->end()) == maxint) {
//run TSP computation for every SCC
const auto maxint = std::numeric_limits<EdgeWeight>::max();
if (*std::max_element(std::begin(*result_table), std::end(*result_table)) == maxint) {
//TODO DELETE
// JSON output related objects
std::unique_ptr<BaseDescriptor<DataFacadeT>> descriptor;
descriptor = osrm::make_unique<JSONDescriptor<DataFacadeT>>(facade);
descriptor->SetConfig(route_parameters);
std::vector<std::vector<unsigned>> components;
TIMER_START(tsp);
// Compute all SCC
std::vector<std::vector<unsigned>> components;
SplitUnaccessibleLocations(phantom_node_vector, *result_table, components);
auto number_of_locations = phantom_node_vector.size();
std::vector<int> min_loc_permutation(number_of_locations, -1);
// std::vector<std::vector<unsigned>> res_route (components.size()-1);
std::vector<std::vector<unsigned>> res_route;
const constexpr std::size_t BF_MAX_FEASABLE = 14;
auto min_dist = 0;
//run TSP computation for every SCC
for(auto k = 0; k < components.size(); ++k) {
if (components[k].size() > 1) {
std::vector<unsigned> scc_route;
scc_route.reserve(components[k].size());
// Compute the TSP with the given algorithm
InternalRouteResult min_route;
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);
if (route_parameters.tsp_algo == "BF" && route_parameters.coordinates.size() < BF_MAX_FEASABLE) {
SimpleLogger().Write() << "Running SCC BF";
osrm::tsp::BruteForceTSP(components[k], phantom_node_vector, *result_table, scc_route);
res_route.push_back(scc_route);
} else if (route_parameters.tsp_algo == "NN") {
osrm::tsp::NearestNeighbourTSP(components[k], phantom_node_vector, *result_table, min_route, min_loc_permutation);
SimpleLogger().Write() << "Running SCC NN";
osrm::tsp::NearestNeighbourTSP(components[k], phantom_node_vector, *result_table, scc_route);
res_route.push_back(scc_route);
} else if (route_parameters.tsp_algo == "FI") {
osrm::tsp::FarthestInsertionTSP(components[k], phantom_node_vector, *result_table, min_route, min_loc_permutation);
SimpleLogger().Write() << "Running SCC FI";
osrm::tsp::FarthestInsertionTSP(components[k], phantom_node_vector, *result_table, scc_route);
res_route.push_back(scc_route);
} else{
osrm::tsp::FarthestInsertionTSP(components[k], phantom_node_vector, *result_table, min_route, min_loc_permutation);
SimpleLogger().Write() << "Running SCC FI";
osrm::tsp::FarthestInsertionTSP(components[k], phantom_node_vector, *result_table, scc_route);
res_route.push_back(scc_route);
}
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);
}
}
SimpleLogger().Write() << "DONE";
std::vector<InternalRouteResult> route;
ComputeRoute(phantom_node_vector, route_parameters, res_route, route);
TIMER_STOP(tsp);
SetRuntimeOutput(TIMER_MSEC(tsp), json_result);
SetDistanceOutput(min_dist, json_result);
SetLocPermutationOutput(min_loc_permutation, json_result);
// SimpleLogger().Write() << "Route is";
// for (auto x : res_route) {
// for (auto y : x)
// std::cout << y << " ";
// }
// SimpleLogger().Write() << "";
auto dist = 0;
for (auto curr_route : route) {
dist += curr_route.shortest_path_length;
SetGeometry(route_parameters, curr_route, json_result);
}
SetDistanceOutput(dist, json_result);
} else { //run TSP computation for all locations
auto number_of_locations = phantom_node_vector.size();
InternalRouteResult min_route;
std::vector<int> min_loc_permutation(number_of_locations, -1);
std::vector<unsigned> res_route;
res_route.reserve(number_of_locations);
// Compute the TSP with the given algorithm
TIMER_START(tsp);
if (route_parameters.tsp_algo == "BF" && route_parameters.coordinates.size() < 14) {
osrm::tsp::BruteForceTSP(phantom_node_vector, *result_table, min_route, min_loc_permutation);
// TODO patrick nach userfreundlichkeit fragen, BF vs bf usw
if (route_parameters.tsp_algo == "BF" && route_parameters.coordinates.size() < BF_MAX_FEASABLE) {
SimpleLogger().Write() << "Running BF";
res_route = osrm::tsp::BruteForceTSP(phantom_node_vector, *result_table, res_route);
} else if (route_parameters.tsp_algo == "NN") {
osrm::tsp::NearestNeighbourTSP(phantom_node_vector, *result_table, min_route, min_loc_permutation);
SimpleLogger().Write() << "Running NN";
osrm::tsp::NearestNeighbourTSP(phantom_node_vector, *result_table, res_route);
} else if (route_parameters.tsp_algo == "FI") {
osrm::tsp::FarthestInsertionTSP(phantom_node_vector, *result_table, min_route, min_loc_permutation);
SimpleLogger().Write() << "Running FI";
osrm::tsp::FarthestInsertionTSP(phantom_node_vector, *result_table, res_route);
} else {
osrm::tsp::FarthestInsertionTSP(phantom_node_vector, *result_table, min_route, min_loc_permutation);
SimpleLogger().Write() << "Running FI";
osrm::tsp::FarthestInsertionTSP(phantom_node_vector, *result_table, res_route);
// osrm::tsp::NearestNeighbourTSP(phantom_node_vector, *result_table, res_route);
}
search_engine_ptr->shortest_path(min_route.segment_end_coordinates, route_parameters.uturns, min_route);
// TODO asserts numer of result blablabla size
// TODO std::is_permutation
// TODO boost range
SimpleLogger().Write() << "DONE";
InternalRouteResult min_route;
ComputeRoute(phantom_node_vector, route_parameters, res_route, 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);
// SimpleLogger().Write() << "Route is";
// for (auto x : res_route) {
// std::cout << x << " ";
// }
// SimpleLogger().Write() << "";
//TODO TIMER im LOGGER
SetRuntimeOutput(TIMER_MSEC(tsp), json_result);
SetLocPermutationOutput(res_route, json_result);
//TODO MEHR ASSERTIONS! :O
SetDistanceOutput(min_route.shortest_path_length, json_result);
SetGeometry(route_parameters, min_route, json_result);
BOOST_ASSERT(min_route.segment_end_coordinates.size() == route_parameters.coordinates.size());
}