/* Copyright (c) 2015, Project OSRM contributors All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #ifndef TSP_FARTHEST_INSERTION_HPP #define TSP_FARTHEST_INSERTION_HPP #include "../data_structures/search_engine.hpp" #include "../util/string_util.hpp" #include "../tools/tsp_logs.hpp" #include #include #include #include #include #include #include namespace osrm { namespace tsp { void GetShortestRoundTrip(const int current_loc, const std::vector & dist_table, const int number_of_locations, std::vector & current_trip, int & min_trip_distance, std::vector::iterator & 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 current_trip for (auto from_node = current_trip.begin(); from_node != std::prev(current_trip.end()); ++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_to = *(dist_table.begin() + (current_loc * number_of_locations) + *to_node); const 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 shortest of all insertions if (trip_dist < min_trip_distance) { min_trip_distance = trip_dist; next_insert_point_candidate = 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 < min_trip_distance) { min_trip_distance = trip_dist; next_insert_point_candidate = to_node; } } // osrm::tsp::FarthestInsertionTSP(components[k], phantom_node_vector, *result_table, scc_route); void FarthestInsertionTSP(const std::vector & locations, const PhantomNodeArray & phantom_node_vector, const std::vector & dist_table, std::vector & current_trip) { ////////////////////////////////////////////////////////////////////////////////////////////////// // 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(); // tracks which nodes have been already visited std::vector visited(number_of_locations, false); auto max_dist = 0; auto max_from = -1; auto max_to = -1; //TODO for (auto x : locations) { for (auto y : locations) { auto xy_dist = *(dist_table.begin() + x * number_of_locations + y); if (xy_dist > max_dist) { max_dist = xy_dist; max_from = x; max_to = y; } } } visited[max_from] = true; visited[max_to] = true; current_trip.push_back(max_from); current_trip.push_back(max_to); // SimpleLogger().Write() << size_of_component; // add all other nodes missing (two nodes are already in the initial start trip) for (int j = 2; j < size_of_component; ++j) { // SimpleLogger().Write() << j << "/" << size_of_component; auto farthest_distance = 0; auto next_node = -1; std::vector::iterator next_insert_point; // find unvisited loc i that is the farthest away from all other visited locs for (auto i : locations) { // find the shortest distance from i to all visited nodes if (!visited[i]) { auto min_trip_distance = std::numeric_limits::max(); std::vector::iterator next_insert_point_candidate; GetShortestRoundTrip(i, dist_table, number_of_locations, current_trip, min_trip_distance, next_insert_point_candidate); // add the location to the current trip such that it results in the shortest total tour // SimpleLogger().Write() << "min_trip_distance " << min_trip_distance; if (min_trip_distance >= farthest_distance) { farthest_distance = min_trip_distance; next_node = i; next_insert_point = next_insert_point_candidate; } } } // SimpleLogger().Write() << "next node " << next_node; // mark as visited and insert node visited[next_node] = true; current_trip.insert(next_insert_point, next_node); } } void FarthestInsertionTSP(const PhantomNodeArray & phantom_node_vector, const std::vector & dist_table, std::vector & current_trip) { ////////////////////////////////////////////////////////////////////////////////////////////////// // 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 auto number_of_locations = phantom_node_vector.size(); // tracks which nodes have been already visited std::vector 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; 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 < number_of_locations; ++j) { auto farthest_distance = 0; auto next_node = -1; //todo move out of loop and overwrite std::vector::iterator 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 = std::numeric_limits::max(); std::vector::iterator next_insert_point_candidate; GetShortestRoundTrip(i, dist_table, number_of_locations, current_trip, 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; current_trip.insert(next_insert_point, next_node); } } } } #endif // TSP_FARTHEST_INSERTION_HPP