refactor and improve the round trip computation of multiple SCCs

Problem:
- old solution was slow
- depending on the result of TarjanSCC, new distance tables and new phantom node vectors were created to run tsp on it

Solution:
- dont create new distance tables and phantom node vectors
- pass an additional vector with the information which locations are in the same component and ignore all others

fix bug for scc split computation
This commit is contained in:
Chau Nguyen
2015-07-05 00:15:55 +02:00
committed by Huyen Chau Nguyen
parent 84c12793e8
commit b15f8f68e4
10 changed files with 634 additions and 287 deletions
+38 -5
View File
@@ -51,19 +51,19 @@ namespace osrm
namespace tsp
{
int ReturnDistance(const std::vector<EdgeWeight> & dist_table, const std::vector<int> location_order, const int min_route_dist, const int number_of_locations) {
template <typename number>
int ReturnDistance(const std::vector<EdgeWeight> & dist_table, const std::vector<number> & location_order, const int min_route_dist, const int number_of_locations, const int component_size) {
int i = 0;
int route_dist = 0;
// compute length and stop if length is longer than route already found
while (i < number_of_locations - 1 && route_dist < min_route_dist) {
while (i < component_size - 1 && route_dist < min_route_dist) {
//get distance from location i to location i+1
route_dist += *(dist_table.begin() + (location_order[i] * number_of_locations) + location_order[i+1]);
++i;
}
//get distance from last location to first location
route_dist += *(dist_table.begin() + (location_order[number_of_locations-1] * number_of_locations) + location_order[0]);
route_dist += *(dist_table.begin() + (location_order[component_size-1] * number_of_locations) + location_order[0]);
if (route_dist < min_route_dist) {
return route_dist;
@@ -73,6 +73,39 @@ int ReturnDistance(const std::vector<EdgeWeight> & dist_table, const std::vector
}
}
void BruteForceTSP(std::vector<unsigned> & location,
const PhantomNodeArray & phantom_node_vector,
const std::vector<EdgeWeight> & dist_table,
InternalRouteResult & min_route,
std::vector<int> & min_loc_permutation) {
const auto number_of_location = phantom_node_vector.size();
const int component_size = location.size();
int min_route_dist = std::numeric_limits<int>::max();
std::vector<unsigned> min_location;
// check length of all possible permutation of the location ids
do {
// int new_distance = ReturnDistance(dist_table, location, min_route_dist, number_of_location, component_size);
int new_distance = 4;
if (new_distance != -1) {
min_route_dist = new_distance;
min_location = location;
}
} while(std::next_permutation(location.begin(), location.end()));
PhantomNodes viapoint;
for (int i = 0; i < component_size - 1; ++i) {
viapoint = PhantomNodes{phantom_node_vector[min_location[i]][0], phantom_node_vector[min_location[i + 1]][0]};
min_route.segment_end_coordinates.emplace_back(viapoint);
min_loc_permutation[min_location[i]] = i;
}
min_loc_permutation[min_location[component_size - 1]] = component_size - 1;
viapoint = PhantomNodes{phantom_node_vector[min_location[component_size - 1]][0], phantom_node_vector[min_location[0]][0]};
min_route.segment_end_coordinates.emplace_back(viapoint);
}
void BruteForceTSP(const PhantomNodeArray & phantom_node_vector,
const std::vector<EdgeWeight> & dist_table,
InternalRouteResult & min_route,
@@ -87,7 +120,7 @@ void BruteForceTSP(const PhantomNodeArray & phantom_node_vector,
// check length of all possible permutation of the location ids
do {
int new_distance = ReturnDistance(dist_table, location_ids, min_route_dist, number_of_locations);
int new_distance = ReturnDistance(dist_table, location_ids, min_route_dist, number_of_locations, number_of_locations);
if (new_distance != -1) {
min_route_dist = new_distance;
//TODO: this gets copied right? fix this
+127 -47
View File
@@ -31,6 +31,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "../data_structures/search_engine.hpp"
#include "../util/string_util.hpp"
#include "../tools/tsp_logs.hpp"
#include <osrm/json_container.hpp>
@@ -41,12 +42,136 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include <vector>
#include <limits>
#include <iostream>
namespace osrm
{
namespace tsp
{
void GetLongestRoundTrip(const int current_loc,
std::list<int> & current_trip,
const std::vector<EdgeWeight> & dist_table,
const int number_of_locations,
int & longest_min_tour,
std::list<int>::iterator & following_loc){
// for all nodes in the current trip find the best insertion resulting in the shortest path
for (auto from_node = current_trip.begin(); from_node != std::prev(current_trip.end()); ++from_node) {
auto to_node = std::next(from_node);
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 trip_dist = dist_from + dist_to - *(dist_table.begin() + (*from_node * number_of_locations) + *to_node);
// from all possible insertions to the current trip, choose the longest of all minimal insertions
if (trip_dist < longest_min_tour) {
longest_min_tour = trip_dist;
following_loc = to_node;
}
}
{ // check insertion between last and first location too
auto from_node = std::prev(current_trip.end());
auto to_node = current_trip.begin();
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 trip_dist = dist_from + dist_to - *(dist_table.begin() + (*from_node * number_of_locations) + *to_node);
if (trip_dist < longest_min_tour) {
longest_min_tour = trip_dist;
following_loc = to_node;
}
}
}
void ComputeRouteAndPermutation(const PhantomNodeArray & phantom_node_vector,
std::list<int> & current_trip,
InternalRouteResult & min_route,
std::vector<int> & min_loc_permutation) {
// given he final trip, compute total distance and return the route and location permutation
PhantomNodes viapoint;
int perm = 0;
for (auto it = current_trip.begin(); it != std::prev(current_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);
min_loc_permutation[from_node] = perm;
++perm;
}
// check dist between last and first location too
viapoint = PhantomNodes{phantom_node_vector[*std::prev(current_trip.end())][0], phantom_node_vector[current_trip.front()][0]};
min_route.segment_end_coordinates.emplace_back(viapoint);
min_loc_permutation[*std::prev(current_trip.end())] = perm;
}
void FarthestInsertionTSP(const std::vector<unsigned> & locations,
const PhantomNodeArray & phantom_node_vector,
const std::vector<EdgeWeight> & dist_table,
InternalRouteResult & min_route,
std::vector<int> & min_loc_permutation) {
//////////////////////////////////////////////////////////////////////////////////////////////////
// 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!
//////////////////////////////////////////////////////////////////////////////////////////////////
const int number_of_locations = phantom_node_vector.size();
const int size_of_component = locations.size();
// list of the trip that will be found incrementally
std::list<int> current_trip;
// tracks which nodes have been already visited
std::vector<bool> visited(number_of_locations, false);
auto max_dist = 0;
auto index = -1;
for (auto x : locations) {
for (auto y : locations) {
if (*(dist_table.begin() + x * number_of_locations + y) > max_dist) {
max_dist = *(dist_table.begin() + x * number_of_locations + y);
index = x * number_of_locations + y;
}
}
}
const int max_from = index / number_of_locations;
const int max_to = index % number_of_locations;
visited[max_from] = true;
visited[max_to] = true;
current_trip.push_back(max_from);
current_trip.push_back(max_to);
// add all other nodes missing (two nodes are already in the initial start trip)
for (int j = 2; j < size_of_component; ++j) {
auto shortest_max_tour = -1;
int next_node = -1;
std::list<int>::iterator min_max_insert;
// find unvisited loc i that is the farthest away from all other visited locs
for (auto i : locations) {
if (!visited[i]) {
// longest_min_tour is the distance of the longest of all insertions with the minimal distance
auto longest_min_tour = std::numeric_limits<int>::max();
// following_loc is the location that comes after the location that is to be inserted
std::list<int>::iterator following_loc;
GetLongestRoundTrip(i, current_trip, dist_table, number_of_locations, longest_min_tour, following_loc);
// add the location to the current trip such that it results in the shortest total tour
if (longest_min_tour > shortest_max_tour) {
shortest_max_tour = longest_min_tour;
next_node = i;
min_max_insert = following_loc;
}
}
}
// mark as visited and insert node
visited[next_node] = true;
current_trip.insert(min_max_insert, next_node);
}
ComputeRouteAndPermutation(phantom_node_vector, current_trip, min_route, min_loc_permutation);
}
void FarthestInsertionTSP(const PhantomNodeArray & phantom_node_vector,
const std::vector<EdgeWeight> & dist_table,
InternalRouteResult & min_route,
@@ -66,14 +191,11 @@ void FarthestInsertionTSP(const PhantomNodeArray & phantom_node_vector,
// tracks which nodes have been already visited
std::vector<bool> visited(number_of_locations, false);
// PrintDistTable(dist_table, 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 int max_from = index / number_of_locations;
const int max_to = index % number_of_locations;
visited[max_from] = true;
visited[max_to] = true;
current_trip.push_back(max_from);
@@ -93,32 +215,7 @@ void FarthestInsertionTSP(const PhantomNodeArray & phantom_node_vector,
// following_loc is the location that comes after the location that is to be inserted
std::list<int>::iterator following_loc;
// for all nodes in the current trip find the best insertion resulting in the shortest path
for (auto from_node = current_trip.begin(); from_node != std::prev(current_trip.end()); ++from_node) {
auto to_node = std::next(from_node);
auto dist_from = *(dist_table.begin() + (*from_node * number_of_locations) + i);
auto dist_to = *(dist_table.begin() + (i * number_of_locations) + *to_node);
auto trip_dist = dist_from + dist_to - *(dist_table.begin() + (*from_node * number_of_locations) + *to_node);
// from all possible insertions to the current trip, choose the longest of all minimal insertions
if (trip_dist < longest_min_tour) {
longest_min_tour = trip_dist;
following_loc = to_node;
}
}
{ // check insertion between last and first location too
auto from_node = std::prev(current_trip.end());
auto to_node = current_trip.begin();
auto dist_from = *(dist_table.begin() + (*from_node * number_of_locations) + i);
auto dist_to = *(dist_table.begin() + (i * number_of_locations) + *to_node);
auto trip_dist = dist_from + dist_to - *(dist_table.begin() + (*from_node * number_of_locations) + *to_node);
if (trip_dist < longest_min_tour) {
longest_min_tour = trip_dist;
following_loc = to_node;
}
}
GetLongestRoundTrip(i, current_trip, dist_table, number_of_locations, longest_min_tour, following_loc);
// add the location to the current trip such that it results in the shortest total tour
if (longest_min_tour > shortest_max_tour) {
@@ -133,24 +230,7 @@ void FarthestInsertionTSP(const PhantomNodeArray & phantom_node_vector,
current_trip.insert(min_max_insert, next_node);
}
// given he final trip, compute total distance and return the route and location permutation
PhantomNodes viapoint;
int perm = 0;
for (auto it = current_trip.begin(); it != std::prev(current_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);
min_loc_permutation[from_node] = perm;
++perm;
}
{ // check dist between last and first location too
viapoint = PhantomNodes{phantom_node_vector[*std::prev(current_trip.end())][0], phantom_node_vector[current_trip.front()][0]};
min_route.segment_end_coordinates.emplace_back(viapoint);
min_loc_permutation[*std::prev(current_trip.end())] = perm;
}
ComputeRouteAndPermutation(phantom_node_vector, current_trip, min_route, min_loc_permutation);
}
@@ -48,6 +48,128 @@ namespace osrm
namespace tsp
{
void NearestNeighbourTSP(const std::vector<unsigned> & locations,
const PhantomNodeArray & phantom_node_vector,
const std::vector<EdgeWeight> & dist_table,
InternalRouteResult & min_route,
std::vector<int> & min_loc_permutation) {
//////////////////////////////////////////////////////////////////////////////////////////////////
// START GREEDY NEAREST NEIGHBOUR HERE
// 1. grab a random location and mark as starting point
// 2. find the nearest unvisited neighbour, set it as the current location and mark as visited
// 3. repeat 2 until there is no unvisited location
// 4. return route back to starting point
// 5. compute route
// 6. repeat 1-5 with different starting points and choose iteration with shortest trip
// 7. DONE!
//////////////////////////////////////////////////////////////////////////////////////////////////
const auto number_of_locations = phantom_node_vector.size();
const int size_of_component = locations.size();
min_route.shortest_path_length = std::numeric_limits<int>::max();
// ALWAYS START AT ANOTHER STARTING POINT
for(auto start_node : locations)
{
int curr_node = start_node;
InternalRouteResult raw_route;
//TODO: Should we always use the same vector or does it not matter at all because of loop scope?
std::vector<int> loc_permutation(number_of_locations, -1);
loc_permutation[start_node] = 0;
// visited[i] indicates whether node i was already visited by the salesman
std::vector<bool> visited(number_of_locations, false);
visited[start_node] = true;
PhantomNodes viapoint;
// 3. REPEAT FOR EVERY UNVISITED NODE
int trip_dist = 0;
for(int via_point = 1; via_point < size_of_component; ++via_point)
{
int min_dist = std::numeric_limits<int>::max();
int min_id = -1;
// 2. FIND NEAREST NEIGHBOUR
for (auto next : locations) {
if(!visited[next] &&
*(dist_table.begin() + curr_node * number_of_locations + next) < min_dist) {
min_dist = *(dist_table.begin() + curr_node * number_of_locations + next);
min_id = next;
}
}
loc_permutation[min_id] = via_point;
visited[min_id] = true;
viapoint = PhantomNodes{phantom_node_vector[curr_node][0], phantom_node_vector[min_id][0]};
raw_route.segment_end_coordinates.emplace_back(viapoint);
trip_dist += min_dist;
curr_node = min_id;
}
// 4. ROUTE BACK TO STARTING POINT
viapoint = PhantomNodes{raw_route.segment_end_coordinates.back().target_phantom, phantom_node_vector[start_node][0]};
raw_route.segment_end_coordinates.emplace_back(viapoint);
// check round trip with this starting point is shorter than the shortest round trip found till now
if (trip_dist < min_route.shortest_path_length) {
min_route = raw_route;
min_route.shortest_path_length = trip_dist;
//TODO: this gets copied right? fix this
min_loc_permutation = loc_permutation;
}
}
// // ALWAYS START AT ANOTHER STARTING POINT
// for(auto start_node : locations) {
// SimpleLogger().Write() << "STARTING AT " << start_node;
// int curr_node = start_node;
// InternalRouteResult raw_route;
// //TODO: Should we always use the same vector or does it not matter at all because of loop scope?
// std::vector<int> loc_permutation(number_of_locations, -1);
// // visited[i] indicates whether node i was already visited by the salesman
// std::vector<bool> visited(number_of_locations, false);
// visited[start_node] = true;
// loc_permutation[start_node] = 0;
// PhantomNodes viapoint;
// // 3. REPEAT FOR EVERY UNVISITED NODE
// int trip_dist = 0;
// for(int via_point = 1; via_point < size_of_component; ++via_point)
// {
// int min_dist = std::numeric_limits<int>::max();
// int min_id = -1;
// // 2. FIND NEAREST NEIGHBOUR
// for (auto next : locations) {
// if(!visited[next] &&
// *(dist_table.begin() + curr_node * number_of_locations + next) < min_dist) {
// min_dist = *(dist_table.begin() + curr_node * number_of_locations + next);
// min_id = next;
// }
// }
// loc_permutation[min_id] = via_point;
// visited[min_id] = true;
// SimpleLogger().Write() << "MOVING TO " << min_id;
// viapoint = PhantomNodes{phantom_node_vector[curr_node][0], phantom_node_vector[min_id][0]};
// raw_route.segment_end_coordinates.emplace_back(viapoint);
// trip_dist += min_dist;
// curr_node = min_id;
// }
// // 4. ROUTE BACK TO STARTING POINT
// viapoint = PhantomNodes{raw_route.segment_end_coordinates.back().target_phantom, phantom_node_vector[start_node][0]};
// raw_route.segment_end_coordinates.emplace_back(viapoint);
// // check round trip with this starting point is shorter than the shortest round trip found till now
// if (trip_dist < min_route.shortest_path_length) {
// min_route = raw_route;
// min_route.shortest_path_length = trip_dist;
// //TODO: this gets copied right? fix this
// min_loc_permutation = loc_permutation;
// }
// }
}
void NearestNeighbourTSP(const PhantomNodeArray & phantom_node_vector,
const std::vector<EdgeWeight> & dist_table,
InternalRouteResult & min_route,