change input param for tsp algos from a vector to a begin and an end iterator

This commit is contained in:
Huyen Chau Nguyen
2015-08-20 01:41:36 +02:00
parent 2de3fc9f6f
commit 93835b9b94
4 changed files with 157 additions and 165 deletions
+16 -14
View File
@@ -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
+28 -82
View File
@@ -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
+19 -18
View File
@@ -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