use typedefs from typedefs.h
return roundtrip result as a return parameter and not as an input parameter
This commit is contained in:
parent
3061c8b854
commit
7587e97d46
@ -103,12 +103,11 @@ template <class DataFacadeT> class RoundTripPlugin final : public BasePlugin
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void SplitUnaccessibleLocations(PhantomNodeArray & phantom_node_vector,
|
void SplitUnaccessibleLocations(const std::size_t number_of_locations,
|
||||||
std::vector<EdgeWeight> & result_table,
|
std::vector<EdgeWeight> & result_table,
|
||||||
std::vector<std::vector<NodeID>> & components) {
|
std::vector<std::vector<NodeID>> & components) {
|
||||||
|
|
||||||
// Run TarjanSCC
|
// Run TarjanSCC
|
||||||
const auto number_of_locations = phantom_node_vector.size();
|
|
||||||
auto wrapper = std::make_shared<MatrixGraphWrapper<EdgeWeight>>(result_table, number_of_locations);
|
auto wrapper = std::make_shared<MatrixGraphWrapper<EdgeWeight>>(result_table, number_of_locations);
|
||||||
// auto empty_restriction = RestrictionMap(std::vector<TurnRestriction>());
|
// auto empty_restriction = RestrictionMap(std::vector<TurnRestriction>());
|
||||||
// std::vector<bool> empty_vector;
|
// std::vector<bool> empty_vector;
|
||||||
@ -212,7 +211,7 @@ template <class DataFacadeT> class RoundTripPlugin final : public BasePlugin
|
|||||||
TIMER_START(tsp);
|
TIMER_START(tsp);
|
||||||
// Compute all SCC
|
// Compute all SCC
|
||||||
std::vector<std::vector<NodeID>> components;
|
std::vector<std::vector<NodeID>> components;
|
||||||
SplitUnaccessibleLocations(phantom_node_vector, *result_table, components);
|
SplitUnaccessibleLocations(number_of_locations, *result_table, components);
|
||||||
// std::vector<std::vector<NodeID>> res_route (components.size()-1);
|
// std::vector<std::vector<NodeID>> res_route (components.size()-1);
|
||||||
std::vector<std::vector<NodeID>> res_route;
|
std::vector<std::vector<NodeID>> res_route;
|
||||||
|
|
||||||
@ -221,24 +220,23 @@ template <class DataFacadeT> class RoundTripPlugin final : public BasePlugin
|
|||||||
for(auto k = 0; k < components.size(); ++k) {
|
for(auto k = 0; k < components.size(); ++k) {
|
||||||
if (components[k].size() > 1) {
|
if (components[k].size() > 1) {
|
||||||
std::vector<NodeID> scc_route;
|
std::vector<NodeID> scc_route;
|
||||||
scc_route.reserve(components[k].size());
|
|
||||||
|
|
||||||
// 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 SCC BF";
|
SimpleLogger().Write() << "Running brute force on multiple SCC";
|
||||||
osrm::tsp::BruteForceTSP(components[k], number_of_locations, *result_table, scc_route);
|
scc_route = osrm::tsp::BruteForceTSP(components[k], number_of_locations, *result_table);
|
||||||
res_route.push_back(scc_route);
|
res_route.push_back(scc_route);
|
||||||
} else if (route_parameters.tsp_algo == "NN") {
|
} else if (route_parameters.tsp_algo == "NN") {
|
||||||
SimpleLogger().Write() << "Running SCC NN";
|
SimpleLogger().Write() << "Running nearest neighbour on multiple SCC";
|
||||||
osrm::tsp::NearestNeighbourTSP(components[k], number_of_locations, *result_table, scc_route);
|
scc_route = osrm::tsp::NearestNeighbourTSP(components[k], number_of_locations, *result_table);
|
||||||
res_route.push_back(scc_route);
|
res_route.push_back(scc_route);
|
||||||
} else if (route_parameters.tsp_algo == "FI") {
|
} else if (route_parameters.tsp_algo == "FI") {
|
||||||
SimpleLogger().Write() << "Running SCC FI";
|
SimpleLogger().Write() << "Running farthest insertion on multiple SCC";
|
||||||
osrm::tsp::FarthestInsertionTSP(components[k], number_of_locations, *result_table, scc_route);
|
scc_route = osrm::tsp::FarthestInsertionTSP(components[k], number_of_locations, *result_table);
|
||||||
res_route.push_back(scc_route);
|
res_route.push_back(scc_route);
|
||||||
} else{
|
} else{
|
||||||
SimpleLogger().Write() << "Running SCC FI";
|
SimpleLogger().Write() << "Running farthest insertion on multiple SCC";
|
||||||
osrm::tsp::FarthestInsertionTSP(components[k], number_of_locations, *result_table, scc_route);
|
scc_route = osrm::tsp::FarthestInsertionTSP(components[k], number_of_locations, *result_table);
|
||||||
res_route.push_back(scc_route);
|
res_route.push_back(scc_route);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -263,23 +261,22 @@ template <class DataFacadeT> class RoundTripPlugin final : public BasePlugin
|
|||||||
SetDistanceOutput(dist, json_result);
|
SetDistanceOutput(dist, json_result);
|
||||||
} else { //run TSP computation for all locations
|
} else { //run TSP computation for all locations
|
||||||
std::vector<NodeID> res_route;
|
std::vector<NodeID> res_route;
|
||||||
res_route.reserve(number_of_locations);
|
|
||||||
|
|
||||||
// Compute the TSP with the given algorithm
|
// Compute the TSP with the given algorithm
|
||||||
TIMER_START(tsp);
|
TIMER_START(tsp);
|
||||||
// TODO patrick nach userfreundlichkeit fragen, BF vs bf usw
|
// TODO patrick nach userfreundlichkeit fragen, BF vs bf usw
|
||||||
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 BF";
|
SimpleLogger().Write() << "Running brute force";
|
||||||
osrm::tsp::BruteForceTSP(number_of_locations, *result_table, res_route);
|
res_route = osrm::tsp::BruteForceTSP(number_of_locations, *result_table);
|
||||||
} else if (route_parameters.tsp_algo == "NN") {
|
} else if (route_parameters.tsp_algo == "NN") {
|
||||||
SimpleLogger().Write() << "Running NN";
|
SimpleLogger().Write() << "Running nearest neighbour";
|
||||||
osrm::tsp::NearestNeighbourTSP(number_of_locations, *result_table, res_route);
|
res_route = osrm::tsp::NearestNeighbourTSP(number_of_locations, *result_table);
|
||||||
} else if (route_parameters.tsp_algo == "FI") {
|
} else if (route_parameters.tsp_algo == "FI") {
|
||||||
SimpleLogger().Write() << "Running FI";
|
SimpleLogger().Write() << "Running farthest insertion";
|
||||||
osrm::tsp::FarthestInsertionTSP(number_of_locations, *result_table, res_route);
|
res_route = osrm::tsp::FarthestInsertionTSP(number_of_locations, *result_table);
|
||||||
} else {
|
} else {
|
||||||
SimpleLogger().Write() << "Running FI";
|
SimpleLogger().Write() << "Running farthest insertion";
|
||||||
osrm::tsp::FarthestInsertionTSP(number_of_locations, *result_table, res_route);
|
res_route = osrm::tsp::FarthestInsertionTSP(number_of_locations, *result_table);
|
||||||
}
|
}
|
||||||
// TODO asserts numer of result blablabla size
|
// TODO asserts numer of result blablabla size
|
||||||
// TODO std::is_permutation
|
// TODO std::is_permutation
|
||||||
|
@ -54,11 +54,11 @@ namespace tsp
|
|||||||
template <typename number>
|
template <typename number>
|
||||||
int ReturnDistance(const std::vector<EdgeWeight> & dist_table,
|
int ReturnDistance(const std::vector<EdgeWeight> & dist_table,
|
||||||
const std::vector<number> & location_order,
|
const std::vector<number> & location_order,
|
||||||
const int min_route_dist,
|
const EdgeWeight min_route_dist,
|
||||||
const int number_of_locations) {
|
const int number_of_locations) {
|
||||||
int route_dist = 0;
|
int route_dist = 0;
|
||||||
int i = 0;
|
int i = 0;
|
||||||
while (i < location_order.size() - 1 && route_dist < min_route_dist) {
|
while (i < location_order.size() - 1) {
|
||||||
route_dist += *(dist_table.begin() + (location_order[i] * number_of_locations) + location_order[i+1]);
|
route_dist += *(dist_table.begin() + (location_order[i] * number_of_locations) + location_order[i+1]);
|
||||||
++i;
|
++i;
|
||||||
}
|
}
|
||||||
@ -67,42 +67,49 @@ int ReturnDistance(const std::vector<EdgeWeight> & dist_table,
|
|||||||
return route_dist;
|
return route_dist;
|
||||||
}
|
}
|
||||||
|
|
||||||
void BruteForceTSP(std::vector<unsigned> & component,
|
std::vector<NodeID> BruteForceTSP(std::vector<NodeID> & component,
|
||||||
const PhantomNodeArray & phantom_node_vector,
|
const std::size_t number_of_locations,
|
||||||
const std::vector<EdgeWeight> & dist_table,
|
const std::vector<EdgeWeight> & dist_table) {
|
||||||
std::vector<unsigned> & route) {
|
|
||||||
|
|
||||||
const unsigned component_size = component.size();
|
std::vector<NodeID> route;
|
||||||
unsigned min_route_dist = std::numeric_limits<unsigned>::max();
|
route.reserve(number_of_locations);
|
||||||
|
|
||||||
|
|
||||||
|
EdgeWeight min_route_dist = INVALID_EDGE_WEIGHT;
|
||||||
|
|
||||||
// check length of all possible permutation of the component ids
|
// check length of all possible permutation of the component ids
|
||||||
do {
|
do {
|
||||||
const auto new_distance = ReturnDistance(dist_table, component, min_route_dist, component_size);
|
const auto new_distance = ReturnDistance(dist_table, component, min_route_dist, number_of_locations);
|
||||||
if (new_distance < min_route_dist) {
|
if (new_distance <= min_route_dist) {
|
||||||
min_route_dist = new_distance;
|
min_route_dist = new_distance;
|
||||||
route = component;
|
route = component;
|
||||||
}
|
}
|
||||||
} while(std::next_permutation(component.begin(), component.end()));
|
} while(std::next_permutation(component.begin(), component.end()));
|
||||||
|
|
||||||
|
return route;
|
||||||
}
|
}
|
||||||
|
|
||||||
void BruteForceTSP(const PhantomNodeArray & phantom_node_vector,
|
std::vector<NodeID> BruteForceTSP(const std::size_t number_of_locations,
|
||||||
const std::vector<EdgeWeight> & dist_table,
|
const std::vector<EdgeWeight> & dist_table) {
|
||||||
std::vector<unsigned> & route) {
|
std::vector<NodeID> route;
|
||||||
const auto number_of_locations = phantom_node_vector.size();
|
route.reserve(number_of_locations);
|
||||||
|
|
||||||
// fill a vector with node ids
|
// fill a vector with node ids
|
||||||
std::vector<unsigned> location_ids(number_of_locations);
|
std::vector<NodeID> location_ids(number_of_locations);
|
||||||
std::iota(location_ids.begin(), location_ids.end(), 0);
|
std::iota(location_ids.begin(), location_ids.end(), 0);
|
||||||
|
|
||||||
unsigned min_route_dist = std::numeric_limits<unsigned>::max();
|
EdgeWeight min_route_dist = INVALID_EDGE_WEIGHT;
|
||||||
// check length of all possible permutation of the location ids
|
// check length of all possible permutation of the location ids
|
||||||
do {
|
do {
|
||||||
const auto new_distance = ReturnDistance(dist_table, location_ids, min_route_dist, number_of_locations);
|
const auto new_distance = ReturnDistance(dist_table, location_ids, min_route_dist, number_of_locations);
|
||||||
|
|
||||||
if (new_distance < min_route_dist) {
|
if (new_distance <= min_route_dist) {
|
||||||
min_route_dist = new_distance;
|
min_route_dist = new_distance;
|
||||||
route = location_ids;
|
route = location_ids;
|
||||||
}
|
}
|
||||||
} while(std::next_permutation(location_ids.begin(), location_ids.end()));
|
} while(std::next_permutation(location_ids.begin(), location_ids.end()));
|
||||||
|
|
||||||
|
return route;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -52,12 +52,12 @@ namespace tsp
|
|||||||
void GetShortestRoundTrip(const int current_loc,
|
void GetShortestRoundTrip(const int current_loc,
|
||||||
const std::vector<EdgeWeight> & dist_table,
|
const std::vector<EdgeWeight> & dist_table,
|
||||||
const int number_of_locations,
|
const int number_of_locations,
|
||||||
std::vector<unsigned> & current_trip,
|
std::vector<NodeID> & route,
|
||||||
int & min_trip_distance,
|
int & min_trip_distance,
|
||||||
std::vector<unsigned>::iterator & next_insert_point_candidate){
|
std::vector<NodeID>::iterator & next_insert_point_candidate){
|
||||||
// for all nodes in the current trip find the best insertion resulting in the shortest path
|
// for all nodes in the current trip find the best insertion resulting in the shortest path
|
||||||
// assert min 2 nodes in current_trip
|
// assert min 2 nodes in route
|
||||||
for (auto from_node = current_trip.begin(); from_node != std::prev(current_trip.end()); ++from_node) {
|
for (auto from_node = route.begin(); from_node != std::prev(route.end()); ++from_node) {
|
||||||
const auto to_node = std::next(from_node);
|
const auto to_node = std::next(from_node);
|
||||||
|
|
||||||
const auto dist_from = *(dist_table.begin() + (*from_node * number_of_locations) + current_loc);
|
const auto dist_from = *(dist_table.begin() + (*from_node * number_of_locations) + current_loc);
|
||||||
@ -71,8 +71,8 @@ void GetShortestRoundTrip(const int current_loc,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
// check insertion between last and first location too
|
// check insertion between last and first location too
|
||||||
auto from_node = std::prev(current_trip.end());
|
auto from_node = std::prev(route.end());
|
||||||
auto to_node = current_trip.begin();
|
auto to_node = route.begin();
|
||||||
|
|
||||||
auto dist_from = *(dist_table.begin() + (*from_node * number_of_locations) + current_loc);
|
auto dist_from = *(dist_table.begin() + (*from_node * number_of_locations) + current_loc);
|
||||||
auto dist_to = *(dist_table.begin() + (current_loc * number_of_locations) + *to_node);
|
auto dist_to = *(dist_table.begin() + (current_loc * number_of_locations) + *to_node);
|
||||||
@ -84,10 +84,9 @@ void GetShortestRoundTrip(const int current_loc,
|
|||||||
}
|
}
|
||||||
|
|
||||||
// osrm::tsp::FarthestInsertionTSP(components[k], phantom_node_vector, *result_table, scc_route);
|
// osrm::tsp::FarthestInsertionTSP(components[k], phantom_node_vector, *result_table, scc_route);
|
||||||
void FarthestInsertionTSP(const std::vector<unsigned> & locations,
|
std::vector<NodeID> FarthestInsertionTSP(const std::vector<NodeID> & locations,
|
||||||
const PhantomNodeArray & phantom_node_vector,
|
const std::size_t number_of_locations,
|
||||||
const std::vector<EdgeWeight> & dist_table,
|
const std::vector<EdgeWeight> & dist_table) {
|
||||||
std::vector<unsigned> & current_trip) {
|
|
||||||
//////////////////////////////////////////////////////////////////////////////////////////////////
|
//////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
// START FARTHEST INSERTION HERE
|
// START FARTHEST INSERTION HERE
|
||||||
// 1. start at a random round trip of 2 locations
|
// 1. start at a random round trip of 2 locations
|
||||||
@ -96,7 +95,10 @@ void FarthestInsertionTSP(const std::vector<unsigned> & locations,
|
|||||||
// 4. repeat 2-3 until all locations are visited
|
// 4. repeat 2-3 until all locations are visited
|
||||||
// 5. DONE!
|
// 5. DONE!
|
||||||
//////////////////////////////////////////////////////////////////////////////////////////////////
|
//////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
const int number_of_locations = phantom_node_vector.size();
|
|
||||||
|
std::vector<NodeID> route;
|
||||||
|
route.reserve(number_of_locations);
|
||||||
|
|
||||||
const int size_of_component = locations.size();
|
const int size_of_component = locations.size();
|
||||||
// tracks which nodes have been already visited
|
// tracks which nodes have been already visited
|
||||||
std::vector<bool> visited(number_of_locations, false);
|
std::vector<bool> visited(number_of_locations, false);
|
||||||
@ -119,24 +121,24 @@ void FarthestInsertionTSP(const std::vector<unsigned> & locations,
|
|||||||
|
|
||||||
visited[max_from] = true;
|
visited[max_from] = true;
|
||||||
visited[max_to] = true;
|
visited[max_to] = true;
|
||||||
current_trip.push_back(max_from);
|
route.push_back(max_from);
|
||||||
current_trip.push_back(max_to);
|
route.push_back(max_to);
|
||||||
// SimpleLogger().Write() << size_of_component;
|
// SimpleLogger().Write() << size_of_component;
|
||||||
// add all other nodes missing (two nodes are already in the initial start trip)
|
// add all other nodes missing (two nodes are already in the initial start trip)
|
||||||
for (int j = 2; j < size_of_component; ++j) {
|
for (int j = 2; j < size_of_component; ++j) {
|
||||||
// SimpleLogger().Write() << j << "/" << size_of_component;
|
// SimpleLogger().Write() << j << "/" << size_of_component;
|
||||||
auto farthest_distance = 0;
|
auto farthest_distance = 0;
|
||||||
auto next_node = -1;
|
auto next_node = -1;
|
||||||
std::vector<unsigned>::iterator next_insert_point;
|
std::vector<NodeID>::iterator next_insert_point;
|
||||||
|
|
||||||
// find unvisited loc i that is the farthest away from all other visited locs
|
// find unvisited loc i that is the farthest away from all other visited locs
|
||||||
for (auto i : locations) {
|
for (auto i : locations) {
|
||||||
// find the shortest distance from i to all visited nodes
|
// find the shortest distance from i to all visited nodes
|
||||||
if (!visited[i]) {
|
if (!visited[i]) {
|
||||||
auto min_trip_distance = std::numeric_limits<int>::max();
|
auto min_trip_distance = INVALID_EDGE_WEIGHT;
|
||||||
std::vector<unsigned>::iterator next_insert_point_candidate;
|
std::vector<NodeID>::iterator next_insert_point_candidate;
|
||||||
|
|
||||||
GetShortestRoundTrip(i, dist_table, number_of_locations, current_trip, min_trip_distance, 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
|
// add the location to the current trip such that it results in the shortest total tour
|
||||||
// SimpleLogger().Write() << "min_trip_distance " << min_trip_distance;
|
// SimpleLogger().Write() << "min_trip_distance " << min_trip_distance;
|
||||||
@ -150,13 +152,13 @@ void FarthestInsertionTSP(const std::vector<unsigned> & locations,
|
|||||||
// SimpleLogger().Write() << "next node " << next_node;
|
// SimpleLogger().Write() << "next node " << next_node;
|
||||||
// mark as visited and insert node
|
// mark as visited and insert node
|
||||||
visited[next_node] = true;
|
visited[next_node] = true;
|
||||||
current_trip.insert(next_insert_point, next_node);
|
route.insert(next_insert_point, next_node);
|
||||||
}
|
}
|
||||||
|
return route;
|
||||||
}
|
}
|
||||||
|
|
||||||
void FarthestInsertionTSP(const PhantomNodeArray & phantom_node_vector,
|
std::vector<NodeID> FarthestInsertionTSP(const std::size_t number_of_locations,
|
||||||
const std::vector<EdgeWeight> & dist_table,
|
const std::vector<EdgeWeight> & dist_table) {
|
||||||
std::vector<unsigned> & current_trip) {
|
|
||||||
//////////////////////////////////////////////////////////////////////////////////////////////////
|
//////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
// START FARTHEST INSERTION HERE
|
// START FARTHEST INSERTION HERE
|
||||||
// 1. start at a random round trip of 2 locations
|
// 1. start at a random round trip of 2 locations
|
||||||
@ -166,7 +168,9 @@ void FarthestInsertionTSP(const PhantomNodeArray & phantom_node_vector,
|
|||||||
// 5. DONE!
|
// 5. DONE!
|
||||||
//////////////////////////////////////////////////////////////////////////////////////////////////
|
//////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
const auto number_of_locations = phantom_node_vector.size();
|
std::vector<NodeID> route;
|
||||||
|
route.reserve(number_of_locations);
|
||||||
|
|
||||||
// tracks which nodes have been already visited
|
// tracks which nodes have been already visited
|
||||||
std::vector<bool> visited(number_of_locations, false);
|
std::vector<bool> visited(number_of_locations, false);
|
||||||
|
|
||||||
@ -176,23 +180,23 @@ void FarthestInsertionTSP(const PhantomNodeArray & phantom_node_vector,
|
|||||||
const int max_to = index % number_of_locations;
|
const int max_to = index % number_of_locations;
|
||||||
visited[max_from] = true;
|
visited[max_from] = true;
|
||||||
visited[max_to] = true;
|
visited[max_to] = true;
|
||||||
current_trip.push_back(max_from);
|
route.push_back(max_from);
|
||||||
current_trip.push_back(max_to);
|
route.push_back(max_to);
|
||||||
|
|
||||||
// add all other nodes missing (two nodes are already in the initial start trip)
|
// add all other nodes missing (two nodes are already in the initial start trip)
|
||||||
for (int j = 2; j < number_of_locations; ++j) {
|
for (int j = 2; j < number_of_locations; ++j) {
|
||||||
auto farthest_distance = 0;
|
auto farthest_distance = 0;
|
||||||
auto next_node = -1;
|
auto next_node = -1;
|
||||||
//todo move out of loop and overwrite
|
//todo move out of loop and overwrite
|
||||||
std::vector<unsigned>::iterator next_insert_point;
|
std::vector<NodeID>::iterator next_insert_point;
|
||||||
|
|
||||||
// find unvisited loc i that is the farthest away from all other visited locs
|
// find unvisited loc i that is the farthest away from all other visited locs
|
||||||
for (int i = 0; i < number_of_locations; ++i) {
|
for (int i = 0; i < number_of_locations; ++i) {
|
||||||
if (!visited[i]) {
|
if (!visited[i]) {
|
||||||
auto min_trip_distance = std::numeric_limits<EdgeWeight>::max();
|
auto min_trip_distance = INVALID_EDGE_WEIGHT;
|
||||||
std::vector<unsigned>::iterator next_insert_point_candidate;
|
std::vector<NodeID>::iterator next_insert_point_candidate;
|
||||||
|
|
||||||
GetShortestRoundTrip(i, dist_table, number_of_locations, current_trip, min_trip_distance, 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
|
// add the location to the current trip such that it results in the shortest total tour
|
||||||
if (min_trip_distance >= farthest_distance) {
|
if (min_trip_distance >= farthest_distance) {
|
||||||
@ -205,8 +209,9 @@ void FarthestInsertionTSP(const PhantomNodeArray & phantom_node_vector,
|
|||||||
|
|
||||||
// mark as visited and insert node
|
// mark as visited and insert node
|
||||||
visited[next_node] = true;
|
visited[next_node] = true;
|
||||||
current_trip.insert(next_insert_point, next_node);
|
route.insert(next_insert_point, next_node);
|
||||||
}
|
}
|
||||||
|
return route;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -48,10 +48,9 @@ namespace osrm
|
|||||||
namespace tsp
|
namespace tsp
|
||||||
{
|
{
|
||||||
|
|
||||||
void NearestNeighbourTSP(const std::vector<unsigned> & locations,
|
std::vector<NodeID> NearestNeighbourTSP(const std::vector<NodeID> & locations,
|
||||||
const PhantomNodeArray & phantom_node_vector,
|
const std::size_t number_of_locations,
|
||||||
const std::vector<EdgeWeight> & dist_table,
|
const std::vector<EdgeWeight> & dist_table) {
|
||||||
std::vector<unsigned> & route) {
|
|
||||||
//////////////////////////////////////////////////////////////////////////////////////////////////
|
//////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
// START GREEDY NEAREST NEIGHBOUR HERE
|
// START GREEDY NEAREST NEIGHBOUR HERE
|
||||||
// 1. grab a random location and mark as starting point
|
// 1. grab a random location and mark as starting point
|
||||||
@ -62,17 +61,18 @@ void NearestNeighbourTSP(const std::vector<unsigned> & locations,
|
|||||||
// 6. repeat 1-5 with different starting points and choose iteration with shortest trip
|
// 6. repeat 1-5 with different starting points and choose iteration with shortest trip
|
||||||
// 7. DONE!
|
// 7. DONE!
|
||||||
//////////////////////////////////////////////////////////////////////////////////////////////////
|
//////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
std::vector<NodeID> route;
|
||||||
|
route.reserve(number_of_locations);
|
||||||
|
|
||||||
const auto number_of_locations = phantom_node_vector.size();
|
|
||||||
const int component_size = locations.size();
|
const int component_size = locations.size();
|
||||||
int shortest_trip_distance = std::numeric_limits<int>::max();
|
int shortest_trip_distance = INVALID_EDGE_WEIGHT;
|
||||||
|
|
||||||
// ALWAYS START AT ANOTHER STARTING POINT
|
// ALWAYS START AT ANOTHER STARTING POINT
|
||||||
for(auto start_node : locations)
|
for(auto start_node : locations)
|
||||||
{
|
{
|
||||||
int curr_node = start_node;
|
int curr_node = start_node;
|
||||||
|
|
||||||
std::vector<unsigned> curr_route;
|
std::vector<NodeID> curr_route;
|
||||||
curr_route.reserve(component_size);
|
curr_route.reserve(component_size);
|
||||||
curr_route.push_back(start_node);
|
curr_route.push_back(start_node);
|
||||||
|
|
||||||
@ -84,7 +84,7 @@ void NearestNeighbourTSP(const std::vector<unsigned> & locations,
|
|||||||
int trip_dist = 0;
|
int trip_dist = 0;
|
||||||
for(int via_point = 1; via_point < component_size; ++via_point)
|
for(int via_point = 1; via_point < component_size; ++via_point)
|
||||||
{
|
{
|
||||||
int min_dist = std::numeric_limits<int>::max();
|
int min_dist = INVALID_EDGE_WEIGHT;
|
||||||
int min_id = -1;
|
int min_id = -1;
|
||||||
|
|
||||||
// 2. FIND NEAREST NEIGHBOUR
|
// 2. FIND NEAREST NEIGHBOUR
|
||||||
@ -107,11 +107,11 @@ void NearestNeighbourTSP(const std::vector<unsigned> & locations,
|
|||||||
route = curr_route;
|
route = curr_route;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return route;
|
||||||
}
|
}
|
||||||
|
|
||||||
void NearestNeighbourTSP(const PhantomNodeArray & phantom_node_vector,
|
std::vector<NodeID> NearestNeighbourTSP(const std::size_t number_of_locations,
|
||||||
const std::vector<EdgeWeight> & dist_table,
|
const std::vector<EdgeWeight> & dist_table) {
|
||||||
std::vector<unsigned> & route) {
|
|
||||||
//////////////////////////////////////////////////////////////////////////////////////////////////
|
//////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
// START GREEDY NEAREST NEIGHBOUR HERE
|
// START GREEDY NEAREST NEIGHBOUR HERE
|
||||||
// 1. grab a random location and mark as starting point
|
// 1. grab a random location and mark as starting point
|
||||||
@ -123,15 +123,17 @@ void NearestNeighbourTSP(const PhantomNodeArray & phantom_node_vector,
|
|||||||
// 7. DONE!
|
// 7. DONE!
|
||||||
//////////////////////////////////////////////////////////////////////////////////////////////////
|
//////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
const auto number_of_locations = phantom_node_vector.size();
|
std::vector<NodeID> route;
|
||||||
int shortest_trip_distance = std::numeric_limits<int>::max();
|
route.reserve(number_of_locations);
|
||||||
|
|
||||||
|
int shortest_trip_distance = INVALID_EDGE_WEIGHT;
|
||||||
|
|
||||||
// ALWAYS START AT ANOTHER STARTING POINT
|
// ALWAYS START AT ANOTHER STARTING POINT
|
||||||
for(int start_node = 0; start_node < number_of_locations; ++start_node)
|
for(int start_node = 0; start_node < number_of_locations; ++start_node)
|
||||||
{
|
{
|
||||||
int curr_node = start_node;
|
int curr_node = start_node;
|
||||||
|
|
||||||
std::vector<unsigned> curr_route;
|
std::vector<NodeID> curr_route;
|
||||||
curr_route.reserve(number_of_locations);
|
curr_route.reserve(number_of_locations);
|
||||||
curr_route.push_back(start_node);
|
curr_route.push_back(start_node);
|
||||||
|
|
||||||
@ -143,7 +145,7 @@ void NearestNeighbourTSP(const PhantomNodeArray & phantom_node_vector,
|
|||||||
int trip_dist = 0;
|
int trip_dist = 0;
|
||||||
for(int via_point = 1; via_point < number_of_locations; ++via_point)
|
for(int via_point = 1; via_point < number_of_locations; ++via_point)
|
||||||
{
|
{
|
||||||
int min_dist = std::numeric_limits<int>::max();
|
int min_dist = INVALID_EDGE_WEIGHT;
|
||||||
int min_id = -1;
|
int min_id = -1;
|
||||||
|
|
||||||
// 2. FIND NEAREST NEIGHBOUR
|
// 2. FIND NEAREST NEIGHBOUR
|
||||||
@ -169,6 +171,7 @@ void NearestNeighbourTSP(const PhantomNodeArray & phantom_node_vector,
|
|||||||
route = curr_route;
|
route = curr_route;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return route;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user