2015-06-16 17:20:38 -04:00
|
|
|
/*
|
|
|
|
|
|
|
|
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"
|
2015-07-04 18:15:55 -04:00
|
|
|
#include "../tools/tsp_logs.hpp"
|
2015-06-16 17:20:38 -04:00
|
|
|
|
|
|
|
#include <osrm/json_container.hpp>
|
|
|
|
|
|
|
|
#include <cstdlib>
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
#include <limits>
|
|
|
|
|
2015-07-04 18:15:55 -04:00
|
|
|
#include <iostream>
|
2015-06-16 17:20:38 -04:00
|
|
|
|
|
|
|
namespace osrm
|
|
|
|
{
|
|
|
|
namespace tsp
|
|
|
|
{
|
|
|
|
|
2015-08-18 07:48:12 -04:00
|
|
|
void GetShortestRoundTrip(const int current_loc,
|
2015-07-04 18:15:55 -04:00
|
|
|
const std::vector<EdgeWeight> & dist_table,
|
|
|
|
const int number_of_locations,
|
2015-08-19 04:25:32 -04:00
|
|
|
std::vector<NodeID> & route,
|
2015-08-18 07:48:12 -04:00
|
|
|
int & min_trip_distance,
|
2015-08-19 04:25:32 -04:00
|
|
|
std::vector<NodeID>::iterator & next_insert_point_candidate){
|
2015-07-04 18:15:55 -04:00
|
|
|
// for all nodes in the current trip find the best insertion resulting in the shortest path
|
2015-08-19 04:25:32 -04:00
|
|
|
// assert min 2 nodes in route
|
|
|
|
for (auto from_node = route.begin(); from_node != std::prev(route.end()); ++from_node) {
|
2015-08-18 07:48:12 -04:00
|
|
|
const auto to_node = std::next(from_node);
|
2015-07-04 18:15:55 -04:00
|
|
|
|
2015-08-18 07:48:12 -04:00
|
|
|
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);
|
2015-07-04 18:15:55 -04:00
|
|
|
|
2015-08-18 07:48:12 -04:00
|
|
|
// 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;
|
2015-07-04 18:15:55 -04:00
|
|
|
}
|
|
|
|
}
|
2015-08-18 07:48:12 -04:00
|
|
|
// check insertion between last and first location too
|
2015-08-19 04:25:32 -04:00
|
|
|
auto from_node = std::prev(route.end());
|
|
|
|
auto to_node = route.begin();
|
2015-08-18 07:48:12 -04:00
|
|
|
|
|
|
|
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;
|
2015-07-04 18:15:55 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-18 07:48:12 -04:00
|
|
|
// osrm::tsp::FarthestInsertionTSP(components[k], phantom_node_vector, *result_table, scc_route);
|
2015-08-19 04:25:32 -04:00
|
|
|
std::vector<NodeID> FarthestInsertionTSP(const std::vector<NodeID> & locations,
|
|
|
|
const std::size_t number_of_locations,
|
|
|
|
const std::vector<EdgeWeight> & dist_table) {
|
2015-07-04 18:15:55 -04:00
|
|
|
//////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// 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!
|
|
|
|
//////////////////////////////////////////////////////////////////////////////////////////////////
|
2015-08-19 04:25:32 -04:00
|
|
|
|
|
|
|
std::vector<NodeID> route;
|
|
|
|
route.reserve(number_of_locations);
|
|
|
|
|
2015-07-04 18:15:55 -04:00
|
|
|
const int size_of_component = locations.size();
|
|
|
|
// tracks which nodes have been already visited
|
|
|
|
std::vector<bool> visited(number_of_locations, false);
|
|
|
|
|
|
|
|
auto max_dist = 0;
|
2015-08-18 07:48:12 -04:00
|
|
|
auto max_from = -1;
|
|
|
|
auto max_to = -1;
|
|
|
|
|
|
|
|
//TODO
|
2015-07-04 18:15:55 -04:00
|
|
|
for (auto x : locations) {
|
|
|
|
for (auto y : locations) {
|
2015-08-18 07:48:12 -04:00
|
|
|
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;
|
2015-07-04 18:15:55 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
visited[max_from] = true;
|
|
|
|
visited[max_to] = true;
|
2015-08-19 04:25:32 -04:00
|
|
|
route.push_back(max_from);
|
|
|
|
route.push_back(max_to);
|
2015-08-18 07:48:12 -04:00
|
|
|
// SimpleLogger().Write() << size_of_component;
|
2015-07-04 18:15:55 -04:00
|
|
|
// add all other nodes missing (two nodes are already in the initial start trip)
|
|
|
|
for (int j = 2; j < size_of_component; ++j) {
|
2015-08-18 07:48:12 -04:00
|
|
|
// SimpleLogger().Write() << j << "/" << size_of_component;
|
|
|
|
auto farthest_distance = 0;
|
|
|
|
auto next_node = -1;
|
2015-08-19 04:25:32 -04:00
|
|
|
std::vector<NodeID>::iterator next_insert_point;
|
2015-07-04 18:15:55 -04:00
|
|
|
|
|
|
|
// find unvisited loc i that is the farthest away from all other visited locs
|
|
|
|
for (auto i : locations) {
|
2015-08-18 07:48:12 -04:00
|
|
|
// find the shortest distance from i to all visited nodes
|
2015-07-04 18:15:55 -04:00
|
|
|
if (!visited[i]) {
|
2015-08-19 04:25:32 -04:00
|
|
|
auto min_trip_distance = INVALID_EDGE_WEIGHT;
|
|
|
|
std::vector<NodeID>::iterator next_insert_point_candidate;
|
2015-08-18 07:48:12 -04:00
|
|
|
|
2015-08-19 04:25:32 -04:00
|
|
|
GetShortestRoundTrip(i, dist_table, number_of_locations, route, min_trip_distance, next_insert_point_candidate);
|
2015-07-04 18:15:55 -04:00
|
|
|
|
|
|
|
// add the location to the current trip such that it results in the shortest total tour
|
2015-08-18 07:48:12 -04:00
|
|
|
// SimpleLogger().Write() << "min_trip_distance " << min_trip_distance;
|
|
|
|
if (min_trip_distance >= farthest_distance) {
|
|
|
|
farthest_distance = min_trip_distance;
|
2015-07-04 18:15:55 -04:00
|
|
|
next_node = i;
|
2015-08-18 07:48:12 -04:00
|
|
|
next_insert_point = next_insert_point_candidate;
|
2015-07-04 18:15:55 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-08-18 07:48:12 -04:00
|
|
|
// SimpleLogger().Write() << "next node " << next_node;
|
2015-07-04 18:15:55 -04:00
|
|
|
// mark as visited and insert node
|
|
|
|
visited[next_node] = true;
|
2015-08-19 04:25:32 -04:00
|
|
|
route.insert(next_insert_point, next_node);
|
2015-07-04 18:15:55 -04:00
|
|
|
}
|
2015-08-19 04:25:32 -04:00
|
|
|
return route;
|
2015-07-04 18:15:55 -04:00
|
|
|
}
|
|
|
|
|
2015-08-19 04:25:32 -04:00
|
|
|
std::vector<NodeID> FarthestInsertionTSP(const std::size_t number_of_locations,
|
|
|
|
const std::vector<EdgeWeight> & dist_table) {
|
2015-06-16 17:20:38 -04:00
|
|
|
//////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// 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!
|
|
|
|
//////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2015-08-19 04:25:32 -04:00
|
|
|
std::vector<NodeID> route;
|
|
|
|
route.reserve(number_of_locations);
|
|
|
|
|
2015-06-16 17:20:38 -04:00
|
|
|
// 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;
|
2015-08-19 04:25:32 -04:00
|
|
|
route.push_back(max_from);
|
|
|
|
route.push_back(max_to);
|
2015-06-16 17:20:38 -04:00
|
|
|
|
|
|
|
// add all other nodes missing (two nodes are already in the initial start trip)
|
|
|
|
for (int j = 2; j < number_of_locations; ++j) {
|
2015-08-18 07:48:12 -04:00
|
|
|
auto farthest_distance = 0;
|
|
|
|
auto next_node = -1;
|
|
|
|
//todo move out of loop and overwrite
|
2015-08-19 04:25:32 -04:00
|
|
|
std::vector<NodeID>::iterator next_insert_point;
|
2015-06-16 17:20:38 -04:00
|
|
|
|
|
|
|
// 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]) {
|
2015-08-19 04:25:32 -04:00
|
|
|
auto min_trip_distance = INVALID_EDGE_WEIGHT;
|
|
|
|
std::vector<NodeID>::iterator next_insert_point_candidate;
|
2015-06-16 17:20:38 -04:00
|
|
|
|
2015-08-19 04:25:32 -04:00
|
|
|
GetShortestRoundTrip(i, dist_table, number_of_locations, route, min_trip_distance, next_insert_point_candidate);
|
2015-06-16 17:20:38 -04:00
|
|
|
|
|
|
|
// add the location to the current trip such that it results in the shortest total tour
|
2015-08-18 07:48:12 -04:00
|
|
|
if (min_trip_distance >= farthest_distance) {
|
|
|
|
farthest_distance = min_trip_distance;
|
2015-06-16 17:20:38 -04:00
|
|
|
next_node = i;
|
2015-08-18 07:48:12 -04:00
|
|
|
next_insert_point = next_insert_point_candidate;
|
2015-06-16 17:20:38 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-08-18 07:48:12 -04:00
|
|
|
|
2015-06-16 17:20:38 -04:00
|
|
|
// mark as visited and insert node
|
|
|
|
visited[next_node] = true;
|
2015-08-19 04:25:32 -04:00
|
|
|
route.insert(next_insert_point, next_node);
|
2015-06-16 17:20:38 -04:00
|
|
|
}
|
2015-08-19 04:25:32 -04:00
|
|
|
return route;
|
2015-06-16 17:20:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // TSP_FARTHEST_INSERTION_HPP
|