change input param for tsp algos from a vector to a begin and an end iterator
This commit is contained in:
parent
2de3fc9f6f
commit
93835b9b94
@ -60,6 +60,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
#include <limits>
|
||||
#include <map>
|
||||
#include <iterator>
|
||||
|
||||
#include <iostream>
|
||||
@ -105,26 +106,56 @@ template <class DataFacadeT> class RoundTripPlugin final : public BasePlugin
|
||||
}
|
||||
}
|
||||
|
||||
void SplitUnaccessibleLocations(const std::size_t number_of_locations,
|
||||
const DistTableWrapper<EdgeWeight> & result_table,
|
||||
std::vector<std::vector<NodeID>> & components) {
|
||||
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) {
|
||||
|
||||
// Run TarjanSCC
|
||||
auto wrapper = std::make_shared<MatrixGraphWrapper<EdgeWeight>>(result_table.GetTable(), number_of_locations);
|
||||
auto scc = TarjanSCC<MatrixGraphWrapper<EdgeWeight>>(wrapper);
|
||||
scc.run();
|
||||
|
||||
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;
|
||||
for (size_t j = 0; j < scc.get_number_of_components(); ++j){
|
||||
components.push_back(std::vector<NodeID>());
|
||||
range_insertion.push_back(prefix);
|
||||
component_range.push_back(prefix);
|
||||
prefix += scc.get_component_size(j);
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < number_of_locations; ++i) {
|
||||
components[scc.get_component_id(i)].push_back(i);
|
||||
components[range_insertion[scc.get_component_id(i)]] = i;
|
||||
++range_insertion[scc.get_component_id(i)];
|
||||
}
|
||||
|
||||
return SCC_Component(components, component_range);
|
||||
}
|
||||
|
||||
template <typename number>
|
||||
void SetLocPermutationOutput(const std::vector<number> & loc_permutation, osrm::json::Object & json_result){
|
||||
void SetLocPermutationOutput(const std::vector<NodeID> & loc_permutation, osrm::json::Object & json_result){
|
||||
osrm::json::Array json_loc_permutation;
|
||||
json_loc_permutation.values.insert(std::end(json_loc_permutation.values), std::begin(loc_permutation), std::end(loc_permutation));
|
||||
json_result.values["loc_permutation"] = json_loc_permutation;
|
||||
@ -137,7 +168,9 @@ template <class DataFacadeT> class RoundTripPlugin final : public BasePlugin
|
||||
json_result.values["runtime"] = runtime;
|
||||
}
|
||||
|
||||
void SetGeometry(const RouteParameters &route_parameters, const InternalRouteResult & min_route, osrm::json::Object & json_result) {
|
||||
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);
|
||||
@ -152,28 +185,14 @@ template <class DataFacadeT> class RoundTripPlugin final : public BasePlugin
|
||||
InternalRouteResult & min_route) {
|
||||
// given he final trip, compute total distance and return the route and location permutation
|
||||
PhantomNodes viapoint;
|
||||
for (auto it = std::begin(trip); it != std::prev(std::end(trip)); ++it) {
|
||||
auto from_node = *it;
|
||||
auto to_node = *std::next(it);
|
||||
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);
|
||||
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(std::end(trip))][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<NodeID>> & 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());
|
||||
}
|
||||
search_engine_ptr->shortest_path(min_route.segment_end_coordinates, route_parameters.uturns, min_route);
|
||||
}
|
||||
|
||||
int HandleRequest(const RouteParameters &route_parameters,
|
||||
@ -184,6 +203,7 @@ template <class DataFacadeT> class RoundTripPlugin final : public BasePlugin
|
||||
return 400;
|
||||
}
|
||||
|
||||
// get phantom nodes
|
||||
PhantomNodeArray phantom_node_vector(route_parameters.coordinates.size());
|
||||
GetPhantomNodes(route_parameters, phantom_node_vector);
|
||||
auto number_of_locations = phantom_node_vector.size();
|
||||
@ -197,58 +217,81 @@ template <class DataFacadeT> class RoundTripPlugin final : public BasePlugin
|
||||
|
||||
const constexpr std::size_t BF_MAX_FEASABLE = 10;
|
||||
BOOST_ASSERT_MSG(result_table.size() > 0, "Distance Table is empty.");
|
||||
std::vector<std::vector<NodeID>> components;
|
||||
|
||||
//check if locations are in different strongly connected components (SCC)
|
||||
const auto maxint = INVALID_EDGE_WEIGHT;
|
||||
if (*std::max_element(result_table.begin(), result_table.end()) == maxint) {
|
||||
// Compute all SCC
|
||||
SplitUnaccessibleLocations(number_of_locations, result_table, components);
|
||||
} else {
|
||||
// fill a vector with node ids
|
||||
std::vector<NodeID> location_ids(number_of_locations);
|
||||
std::iota(std::begin(location_ids), std::end(location_ids), 0);
|
||||
components.push_back(std::move(location_ids));
|
||||
// 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;
|
||||
|
||||
std::vector<std::vector<NodeID>> res_route;
|
||||
TIMER_START(tsp);
|
||||
//run TSP computation for every SCC
|
||||
for(auto k = 0; k < components.size(); ++k) {
|
||||
if (components[k].size() > 1) {
|
||||
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) {
|
||||
std::vector<NodeID> scc_route;
|
||||
NodeIDIterator start = std::begin(scc.component) + scc.component_range[k];
|
||||
NodeIDIterator end = std::begin(scc.component) + scc.component_range[k+1];
|
||||
|
||||
// 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";
|
||||
scc_route = osrm::tsp::BruteForceTSP(components[k], number_of_locations, result_table);
|
||||
scc_route = osrm::tsp::BruteForceTSP(start, end, number_of_locations, result_table);
|
||||
res_route.push_back(scc_route);
|
||||
} else if (route_parameters.tsp_algo == "NN") {
|
||||
SimpleLogger().Write() << "Running nearest neighbour";
|
||||
scc_route = osrm::tsp::NearestNeighbourTSP(components[k], number_of_locations, result_table);
|
||||
scc_route = osrm::tsp::NearestNeighbourTSP(start, end, number_of_locations, result_table);
|
||||
res_route.push_back(scc_route);
|
||||
} else if (route_parameters.tsp_algo == "FI") {
|
||||
SimpleLogger().Write() << "Running farthest insertion";
|
||||
scc_route = osrm::tsp::FarthestInsertionTSP(components[k], number_of_locations, result_table);
|
||||
scc_route = osrm::tsp::FarthestInsertionTSP(start, end, number_of_locations, result_table);
|
||||
res_route.push_back(scc_route);
|
||||
} else{
|
||||
SimpleLogger().Write() << "Running farthest insertion";
|
||||
scc_route = osrm::tsp::FarthestInsertionTSP(components[k], number_of_locations, result_table);
|
||||
scc_route = osrm::tsp::FarthestInsertionTSP(start, end, number_of_locations, result_table);
|
||||
res_route.push_back(scc_route);
|
||||
}
|
||||
|
||||
SimpleLogger().Write() << "Route #"
|
||||
<< k << ": "
|
||||
<< [&scc_route](){
|
||||
std::string s = "";
|
||||
for (auto x : scc_route) {
|
||||
s += std::to_string(x) + " ";
|
||||
}
|
||||
return s;
|
||||
}();
|
||||
}
|
||||
}
|
||||
std::vector<InternalRouteResult> route;
|
||||
ComputeRoute(phantom_node_vector, route_parameters, res_route, route);
|
||||
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]);
|
||||
}
|
||||
TIMER_STOP(tsp);
|
||||
SetRuntimeOutput(TIMER_MSEC(tsp), json_result);
|
||||
SimpleLogger().Write() << "Computed roundtrip in " << TIMER_MSEC(tsp) << "ms";
|
||||
|
||||
SetRuntimeOutput(TIMER_MSEC(tsp), json_result);
|
||||
//TODO
|
||||
SetLocPermutationOutput(res_route[0], json_result);
|
||||
|
||||
std::unique_ptr<BaseDescriptor<DataFacadeT>> descriptor;
|
||||
descriptor = osrm::make_unique<JSONDescriptor<DataFacadeT>>(facade);
|
||||
descriptor->SetConfig(route_parameters);
|
||||
auto dist = 0;
|
||||
for (auto curr_route : route) {
|
||||
dist += curr_route.shortest_path_length;
|
||||
SetGeometry(route_parameters, curr_route, json_result);
|
||||
for (auto r : comp_route) {
|
||||
dist += r.shortest_path_length;
|
||||
// SetGeometry(route_parameters, r, json_result);
|
||||
descriptor->Run(r, json_result);
|
||||
}
|
||||
SetDistanceOutput(dist, json_result);
|
||||
|
||||
|
@ -52,42 +52,44 @@ namespace osrm
|
||||
namespace tsp
|
||||
{
|
||||
|
||||
template <typename number>
|
||||
int ReturnDistance(const DistTableWrapper<EdgeWeight> & dist_table,
|
||||
const std::vector<number> & location_order,
|
||||
const EdgeWeight min_route_dist,
|
||||
const std::size_t number_of_locations) {
|
||||
EdgeWeight ReturnDistance(const DistTableWrapper<EdgeWeight> & dist_table,
|
||||
const std::vector<NodeID> & location_order,
|
||||
const EdgeWeight min_route_dist,
|
||||
const std::size_t component_size) {
|
||||
EdgeWeight route_dist = 0;
|
||||
int i = 0;
|
||||
std::size_t i = 0;
|
||||
while (i < location_order.size()) {
|
||||
route_dist += dist_table(location_order[i], location_order[(i+1) % number_of_locations]);
|
||||
route_dist += dist_table(location_order[i], location_order[(i+1) % component_size]);
|
||||
++i;
|
||||
}
|
||||
return route_dist;
|
||||
}
|
||||
|
||||
std::vector<NodeID> BruteForceTSP(std::vector<NodeID> & component,
|
||||
template <typename NodeIDIterator>
|
||||
std::vector<NodeID> BruteForceTSP(const NodeIDIterator start,
|
||||
const NodeIDIterator end,
|
||||
const std::size_t number_of_locations,
|
||||
const DistTableWrapper<EdgeWeight> & dist_table) {
|
||||
const auto component_size = std::distance(start, end);
|
||||
|
||||
std::vector<NodeID> perm(start, end);
|
||||
std::vector<NodeID> route;
|
||||
route.reserve(number_of_locations);
|
||||
|
||||
route.reserve(component_size);
|
||||
|
||||
EdgeWeight min_route_dist = INVALID_EDGE_WEIGHT;
|
||||
|
||||
// check length of all possible permutation of the component ids
|
||||
do {
|
||||
const auto new_distance = ReturnDistance(dist_table, component, min_route_dist, number_of_locations);
|
||||
const auto new_distance = ReturnDistance(dist_table, perm, min_route_dist, component_size);
|
||||
if (new_distance <= min_route_dist) {
|
||||
min_route_dist = new_distance;
|
||||
route = component;
|
||||
route = perm;
|
||||
}
|
||||
} while(std::next_permutation(std::begin(component), std::end(component)));
|
||||
} while(std::next_permutation(std::begin(perm), std::end(perm)));
|
||||
|
||||
return route;
|
||||
}
|
||||
|
||||
} //end namespace osrm
|
||||
} //end namespace tsp
|
||||
} //end namespace osrm
|
||||
#endif // TSP_BRUTE_FORCE_HPP
|
@ -50,17 +50,16 @@ namespace osrm
|
||||
namespace tsp
|
||||
{
|
||||
|
||||
using NodeIterator = typename std::vector<NodeID>::iterator;
|
||||
|
||||
// given a route and a new location, find the best place of insertion and
|
||||
// check the distance of roundtrip when the new location is additionally visited
|
||||
std::pair<EdgeWeight, NodeIterator> GetShortestRoundTrip(const int new_loc,
|
||||
const DistTableWrapper<EdgeWeight> & dist_table,
|
||||
const int number_of_locations,
|
||||
std::vector<NodeID> & route){
|
||||
using NodeIDIter = typename std::vector<NodeID>::iterator;
|
||||
std::pair<EdgeWeight, NodeIDIter> GetShortestRoundTrip(const NodeID new_loc,
|
||||
const DistTableWrapper<EdgeWeight> & dist_table,
|
||||
const std::size_t number_of_locations,
|
||||
std::vector<NodeID> & route){
|
||||
|
||||
auto min_trip_distance = INVALID_EDGE_WEIGHT;
|
||||
NodeIterator next_insert_point_candidate;
|
||||
NodeIDIter next_insert_point_candidate;
|
||||
|
||||
// for all nodes in the current trip find the best insertion resulting in the shortest path
|
||||
// assert min 2 nodes in route
|
||||
@ -84,10 +83,12 @@ std::pair<EdgeWeight, NodeIterator> GetShortestRoundTrip(const int new_loc,
|
||||
return std::make_pair(min_trip_distance, next_insert_point_candidate);
|
||||
}
|
||||
|
||||
template <typename NodeIDIterator>
|
||||
// given two initial start nodes, find a roundtrip route using the farthest insertion algorithm
|
||||
std::vector<NodeID> FindRoute(const std::size_t & number_of_locations,
|
||||
const std::size_t & size_of_component,
|
||||
const std::vector<NodeID> & locations,
|
||||
const NodeIDIterator & start,
|
||||
const NodeIDIterator & end,
|
||||
const DistTableWrapper<EdgeWeight> & dist_table,
|
||||
const NodeID & start1,
|
||||
const NodeID & start2) {
|
||||
@ -103,22 +104,22 @@ std::vector<NodeID> FindRoute(const std::size_t & number_of_locations,
|
||||
route.push_back(start2);
|
||||
|
||||
// add all other nodes missing (two nodes are already in the initial start trip)
|
||||
for (int j = 2; j < size_of_component; ++j) {
|
||||
for (std::size_t j = 2; j < size_of_component; ++j) {
|
||||
|
||||
auto farthest_distance = 0;
|
||||
auto next_node = -1;
|
||||
NodeIterator next_insert_point;
|
||||
NodeIDIter next_insert_point;
|
||||
|
||||
// find unvisited loc i that is the farthest away from all other visited locs
|
||||
for (auto i : locations) {
|
||||
for (auto i = start; i != end; ++i) {
|
||||
// find the shortest distance from i to all visited nodes
|
||||
if (!visited[i]) {
|
||||
auto insert_candidate = GetShortestRoundTrip(i, dist_table, number_of_locations, route);
|
||||
if (!visited[*i]) {
|
||||
auto insert_candidate = GetShortestRoundTrip(*i, dist_table, number_of_locations, route);
|
||||
|
||||
// add the location to the current trip such that it results in the shortest total tour
|
||||
if (insert_candidate.first >= farthest_distance) {
|
||||
farthest_distance = insert_candidate.first;
|
||||
next_node = i;
|
||||
next_node = *i;
|
||||
next_insert_point = insert_candidate.second;
|
||||
}
|
||||
}
|
||||
@ -131,7 +132,9 @@ std::vector<NodeID> FindRoute(const std::size_t & number_of_locations,
|
||||
return route;
|
||||
}
|
||||
|
||||
std::vector<NodeID> FarthestInsertionTSP(const std::vector<NodeID> & locations,
|
||||
template <typename NodeIDIterator>
|
||||
std::vector<NodeID> FarthestInsertionTSP(const NodeIDIterator & start,
|
||||
const NodeIDIterator & end,
|
||||
const std::size_t number_of_locations,
|
||||
const DistTableWrapper<EdgeWeight> & dist_table) {
|
||||
//////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
@ -143,92 +146,35 @@ std::vector<NodeID> FarthestInsertionTSP(const std::vector<NodeID> & locations,
|
||||
// 5. DONE!
|
||||
//////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
const auto size_of_component = locations.size();
|
||||
const auto component_size = std::distance(start, end);
|
||||
auto max_from = -1;
|
||||
auto max_to = -1;
|
||||
|
||||
if (size_of_component == number_of_locations) {
|
||||
if (component_size == number_of_locations) {
|
||||
// find the pair of location with the biggest distance and make the pair the initial start trip
|
||||
const auto index = std::distance(dist_table.begin(), std::max_element(dist_table.begin(), dist_table.end()));
|
||||
const auto index = std::distance(std::begin(dist_table), std::max_element(std::begin(dist_table), std::end(dist_table)));
|
||||
max_from = index / number_of_locations;
|
||||
max_to = index % number_of_locations;
|
||||
|
||||
} else {
|
||||
auto max_dist = 0;
|
||||
for (auto x : locations) {
|
||||
for (auto y : locations) {
|
||||
auto xy_dist = dist_table(x, y);
|
||||
for (auto x = start; x != end; ++x) {
|
||||
for (auto y = start; y != end; ++y) {
|
||||
const auto xy_dist = dist_table(*x, *y);
|
||||
if (xy_dist > max_dist) {
|
||||
max_dist = xy_dist;
|
||||
max_from = x;
|
||||
max_to = y;
|
||||
max_from = *x;
|
||||
max_to = *y;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return FindRoute(number_of_locations, size_of_component, locations, dist_table, max_from, max_to);
|
||||
return FindRoute(number_of_locations, component_size, start, end, dist_table, max_from, max_to);
|
||||
}
|
||||
|
||||
// std::vector<NodeID> FarthestInsertionTSP(const std::size_t number_of_locations,
|
||||
// const std::vector<EdgeWeight> & dist_table) {
|
||||
// //////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// // START FARTHEST INSERTION HERE
|
||||
// // 1. start at a random round trip of 2 locations
|
||||
// // 2. find the location that is the farthest away from the visited locations and whose insertion will make the round trip the longest
|
||||
// // 3. add the found location to the current round trip such that round trip is the shortest
|
||||
// // 4. repeat 2-3 until all locations are visited
|
||||
// // 5. DONE!
|
||||
// //////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
// std::vector<NodeID> route;
|
||||
// route.reserve(number_of_locations);
|
||||
|
||||
// // tracks which nodes have been already visited
|
||||
// std::vector<bool> visited(number_of_locations, false);
|
||||
|
||||
// // find the pair of location with the biggest distance and make the pair the initial start trip
|
||||
// const auto index = std::distance(dist_table.begin(), std::max_element(dist_table.begin(), dist_table.end()));
|
||||
// const int max_from = index / number_of_locations;
|
||||
// const int max_to = index % number_of_locations;
|
||||
// visited[max_from] = true;
|
||||
// visited[max_to] = true;
|
||||
// route.push_back(max_from);
|
||||
// route.push_back(max_to);
|
||||
|
||||
// // add all other nodes missing (two nodes are already in the initial start trip)
|
||||
// for (int j = 2; j < number_of_locations; ++j) {
|
||||
// auto farthest_distance = 0;
|
||||
// auto next_node = -1;
|
||||
// //todo move out of loop and overwrite
|
||||
// NodeIterator next_insert_point;
|
||||
|
||||
// // find unvisited loc i that is the farthest away from all other visited locs
|
||||
// for (int i = 0; i < number_of_locations; ++i) {
|
||||
// if (!visited[i]) {
|
||||
// auto min_trip_distance = INVALID_EDGE_WEIGHT;
|
||||
// NodeIterator next_insert_point_candidate;
|
||||
|
||||
// GetShortestRoundTrip(i, dist_table, number_of_locations, route, min_trip_distance, next_insert_point_candidate);
|
||||
|
||||
// // add the location to the current trip such that it results in the shortest total tour
|
||||
// if (min_trip_distance >= farthest_distance) {
|
||||
// farthest_distance = min_trip_distance;
|
||||
// next_node = i;
|
||||
// next_insert_point = next_insert_point_candidate;
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
// // mark as visited and insert node
|
||||
// visited[next_node] = true;
|
||||
// route.insert(next_insert_point, next_node);
|
||||
// }
|
||||
// return route;
|
||||
// }
|
||||
|
||||
|
||||
} //end namespace osrm
|
||||
} //end namespace tsp
|
||||
} //end namespace osrm
|
||||
|
||||
#endif // TSP_FARTHEST_INSERTION_HPP
|
@ -48,8 +48,9 @@ namespace osrm
|
||||
{
|
||||
namespace tsp
|
||||
{
|
||||
|
||||
std::vector<NodeID> NearestNeighbourTSP(const std::vector<NodeID> & locations,
|
||||
template <typename NodeIDIterator>
|
||||
std::vector<NodeID> NearestNeighbourTSP(const NodeIDIterator & start,
|
||||
const NodeIDIterator & end,
|
||||
const std::size_t number_of_locations,
|
||||
const DistTableWrapper<EdgeWeight> & dist_table) {
|
||||
//////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
@ -65,36 +66,36 @@ std::vector<NodeID> NearestNeighbourTSP(const std::vector<NodeID> & locations,
|
||||
std::vector<NodeID> route;
|
||||
route.reserve(number_of_locations);
|
||||
|
||||
const int component_size = locations.size();
|
||||
int shortest_trip_distance = INVALID_EDGE_WEIGHT;
|
||||
const auto component_size = std::distance(start, end);
|
||||
auto shortest_trip_distance = INVALID_EDGE_WEIGHT;
|
||||
|
||||
// ALWAYS START AT ANOTHER STARTING POINT
|
||||
for(auto start_node : locations)
|
||||
for(auto start_node = start; start_node != end; ++start_node)
|
||||
{
|
||||
int curr_node = start_node;
|
||||
NodeID curr_node = *start_node;
|
||||
|
||||
std::vector<NodeID> curr_route;
|
||||
curr_route.reserve(component_size);
|
||||
curr_route.push_back(start_node);
|
||||
curr_route.push_back(*start_node);
|
||||
|
||||
// visited[i] indicates whether node i was already visited by the salesman
|
||||
std::vector<bool> visited(number_of_locations, false);
|
||||
visited[start_node] = true;
|
||||
visited[*start_node] = true;
|
||||
|
||||
// 3. REPEAT FOR EVERY UNVISITED NODE
|
||||
int trip_dist = 0;
|
||||
for(int via_point = 1; via_point < component_size; ++via_point)
|
||||
EdgeWeight trip_dist = 0;
|
||||
for(auto via_point = 1; via_point < component_size; ++via_point)
|
||||
{
|
||||
int min_dist = INVALID_EDGE_WEIGHT;
|
||||
int min_id = -1;
|
||||
EdgeWeight min_dist = INVALID_EDGE_WEIGHT;
|
||||
NodeID min_id = SPECIAL_NODEID;
|
||||
|
||||
// 2. FIND NEAREST NEIGHBOUR
|
||||
for (auto next : locations) {
|
||||
auto curr_dist = dist_table(curr_node, next);
|
||||
if(!visited[next] &&
|
||||
for (auto next = start; next != end; ++next) {
|
||||
auto curr_dist = dist_table(curr_node, *next);
|
||||
if(!visited[*next] &&
|
||||
curr_dist < min_dist) {
|
||||
min_dist = curr_dist;
|
||||
min_id = next;
|
||||
min_id = *next;
|
||||
}
|
||||
}
|
||||
visited[min_id] = true;
|
||||
@ -112,6 +113,6 @@ std::vector<NodeID> NearestNeighbourTSP(const std::vector<NodeID> & locations,
|
||||
return route;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
} //end namespace tsp
|
||||
} //end namespace osrm
|
||||
#endif // TSP_NEAREST_NEIGHBOUR_HPP
|
Loading…
Reference in New Issue
Block a user