add assertions

This commit is contained in:
Huyen Chau Nguyen
2015-08-20 15:32:28 +02:00
parent 47fbd2a2b5
commit 8429a1e792
5 changed files with 71 additions and 12 deletions
+9
View File
@@ -52,6 +52,7 @@ namespace osrm
namespace tsp
{
// computes the distance of a given permutation
EdgeWeight ReturnDistance(const DistTableWrapper<EdgeWeight> & dist_table,
const std::vector<NodeID> & location_order,
const EdgeWeight min_route_dist,
@@ -62,9 +63,13 @@ EdgeWeight ReturnDistance(const DistTableWrapper<EdgeWeight> & dist_table,
route_dist += dist_table(location_order[i], location_order[(i+1) % component_size]);
++i;
}
BOOST_ASSERT_MSG(route_dist != INVALID_EDGE_WEIGHT, "invalid route found");
return route_dist;
}
// computes the route by computing all permutations and selecting the shortest
template <typename NodeIDIterator>
std::vector<NodeID> BruteForceTSP(const NodeIDIterator start,
const NodeIDIterator end,
@@ -79,6 +84,10 @@ std::vector<NodeID> BruteForceTSP(const NodeIDIterator start,
EdgeWeight min_route_dist = INVALID_EDGE_WEIGHT;
// check length of all possible permutation of the component ids
BOOST_ASSERT_MSG(*(std::max_element(std::begin(perm), std::end(perm))) < number_of_locations, "invalid node id");
BOOST_ASSERT_MSG(*(std::min_element(std::begin(perm), std::end(perm))) >= 0, "invalid node id");
do {
const auto new_distance = ReturnDistance(dist_table, perm, min_route_dist, component_size);
if (new_distance <= min_route_dist) {
+21 -3
View File
@@ -73,12 +73,21 @@ std::pair<EdgeWeight, NodeIDIter> GetShortestRoundTrip(const NodeID new_loc,
const auto dist_to = dist_table(new_loc, *to_node);
const auto trip_dist = dist_from + dist_to - dist_table(*from_node, *to_node);;
BOOST_ASSERT_MSG(dist_from != INVALID_EDGE_WEIGHT,
"distance has invalid edge weight");
BOOST_ASSERT_MSG(dist_to != INVALID_EDGE_WEIGHT,
"distance has invalid edge weight");
BOOST_ASSERT_MSG(trip_dist >= 0,
"previous trip was not minimal. something's wrong");
// from all possible insertions to the current trip, choose the shortest of all insertions
if (trip_dist < min_trip_distance) {
min_trip_distance = trip_dist;
next_insert_point_candidate = to_node;
}
}
BOOST_ASSERT_MSG(min_trip_distance != INVALID_EDGE_WEIGHT,
"trip has invalid edge weight");
return std::make_pair(min_trip_distance, next_insert_point_candidate);
}
@@ -86,12 +95,15 @@ std::pair<EdgeWeight, NodeIDIter> GetShortestRoundTrip(const NodeID new_loc,
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::size_t & component_size,
const NodeIDIterator & start,
const NodeIDIterator & end,
const DistTableWrapper<EdgeWeight> & dist_table,
const NodeID & start1,
const NodeID & start2) {
BOOST_ASSERT_MSG(number_of_locations >= component_size,
"component size bigger than total number of locations");
std::vector<NodeID> route;
route.reserve(number_of_locations);
@@ -104,7 +116,7 @@ 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 (std::size_t j = 2; j < size_of_component; ++j) {
for (std::size_t j = 2; j < component_size; ++j) {
auto farthest_distance = 0;
auto next_node = -1;
@@ -116,6 +128,9 @@ std::vector<NodeID> FindRoute(const std::size_t & number_of_locations,
if (!visited[*i]) {
auto insert_candidate = GetShortestRoundTrip(*i, dist_table, number_of_locations, route);
BOOST_ASSERT_MSG(insert_candidate.first != INVALID_EDGE_WEIGHT,
"shortest round trip is invalid");
// 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;
@@ -125,6 +140,8 @@ std::vector<NodeID> FindRoute(const std::size_t & number_of_locations,
}
}
BOOST_ASSERT_MSG(next_node >= 0, "next node to visit is invalid");
// mark as visited and insert node
visited[next_node] = true;
route.insert(next_insert_point, next_node);
@@ -169,7 +186,8 @@ std::vector<NodeID> FarthestInsertionTSP(const NodeIDIterator & start,
}
}
}
BOOST_ASSERT_MSG(max_from < number_of_locations && max_from >= 0, "start node");
BOOST_ASSERT_MSG(max_to < number_of_locations && max_to >= 0, "start node");
return FindRoute(number_of_locations, component_size, start, end, dist_table, max_from, max_to);
}
+5 -1
View File
@@ -84,7 +84,7 @@ std::vector<NodeID> NearestNeighbourTSP(const NodeIDIterator & start,
// 3. REPEAT FOR EVERY UNVISITED NODE
EdgeWeight trip_dist = 0;
for(auto via_point = 1; via_point < component_size; ++via_point)
for(std::size_t via_point = 1; via_point < component_size; ++via_point)
{
EdgeWeight min_dist = INVALID_EDGE_WEIGHT;
NodeID min_id = SPECIAL_NODEID;
@@ -92,12 +92,16 @@ std::vector<NodeID> NearestNeighbourTSP(const NodeIDIterator & start,
// 2. FIND NEAREST NEIGHBOUR
for (auto next = start; next != end; ++next) {
auto curr_dist = dist_table(curr_node, *next);
BOOST_ASSERT_MSG(curr_dist != INVALID_EDGE_WEIGHT, "invalid distance found");
if(!visited[*next] &&
curr_dist < min_dist) {
min_dist = curr_dist;
min_id = *next;
}
}
BOOST_ASSERT_MSG(min_id != SPECIAL_NODEID, "no next node found");
visited[min_id] = true;
curr_route.push_back(min_id);
trip_dist += min_dist;