split algorithms in different plugins for better evaluation
split tsp brute force algorithm for better testing refactor and clean up
This commit is contained in:
committed by
Huyen Chau Nguyen
parent
f0d66ff0fb
commit
a40b3a98dc
@@ -40,47 +40,66 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <limits>
|
||||
#include <iostream>
|
||||
#include "../util/simple_logger.hpp"
|
||||
|
||||
|
||||
// HAHAHA. NICE TRY, CHAU.
|
||||
|
||||
|
||||
namespace osrm
|
||||
{
|
||||
namespace tsp
|
||||
{
|
||||
void BruteForce(const RouteParameters & route_parameters,
|
||||
const PhantomNodeArray & phantom_node_vector,
|
||||
const std::vector<EdgeWeight> & dist_table,
|
||||
InternalRouteResult & min_route,
|
||||
std::vector<int> & min_loc_permutation) {
|
||||
|
||||
|
||||
int ReturnDistance(const std::vector<EdgeWeight> & dist_table, const std::vector<int> location_order, const int min_route_dist, const int number_of_locations) {
|
||||
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) {
|
||||
//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]);
|
||||
|
||||
if (route_dist < min_route_dist) {
|
||||
return route_dist;
|
||||
}
|
||||
else {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
void BruteForceTSP(const PhantomNodeArray & phantom_node_vector,
|
||||
const std::vector<EdgeWeight> & dist_table,
|
||||
InternalRouteResult & min_route,
|
||||
std::vector<int> & min_loc_permutation) {
|
||||
|
||||
const auto number_of_locations = phantom_node_vector.size();
|
||||
// fill a vector with node ids
|
||||
std::vector<int> location_ids(number_of_locations);
|
||||
std::iota(location_ids.begin(), location_ids.end(), 0);
|
||||
|
||||
int min_route_dist = std::numeric_limits<int>::max();
|
||||
do {
|
||||
int route_dist = 0;
|
||||
for (int i = 0; i < number_of_locations - 1; ++i) {
|
||||
route_dist += *(dist_table.begin() + (location_ids[i] * number_of_locations) + location_ids[i+1]);
|
||||
}
|
||||
route_dist += *(dist_table.begin() + (location_ids[number_of_locations-1] * number_of_locations) + location_ids[0]);
|
||||
|
||||
if (route_dist < min_route_dist) {
|
||||
min_route_dist = route_dist;
|
||||
// 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);
|
||||
if (new_distance != -1) {
|
||||
min_route_dist = new_distance;
|
||||
//TODO: this gets copied right? fix this
|
||||
min_loc_permutation = location_ids;
|
||||
}
|
||||
} while(std::next_permutation(location_ids.begin(), location_ids.end()));
|
||||
|
||||
PhantomNodes viapoint;
|
||||
for (int i = 0; i < number_of_locations - 1; ++i) {
|
||||
viapoint = PhantomNodes{phantom_node_vector[i][0], phantom_node_vector[i + 1][0]};
|
||||
viapoint = PhantomNodes{phantom_node_vector[min_loc_permutation[i]][0], phantom_node_vector[min_loc_permutation[i + 1]][0]};
|
||||
min_route.segment_end_coordinates.emplace_back(viapoint);
|
||||
}
|
||||
viapoint = PhantomNodes{phantom_node_vector[number_of_locations - 1][0], phantom_node_vector[0][0]};
|
||||
viapoint = PhantomNodes{phantom_node_vector[min_loc_permutation[number_of_locations - 1]][0], phantom_node_vector[min_loc_permutation[0]][0]};
|
||||
min_route.segment_end_coordinates.emplace_back(viapoint);
|
||||
}
|
||||
|
||||
|
||||
@@ -47,11 +47,10 @@ namespace osrm
|
||||
namespace tsp
|
||||
{
|
||||
|
||||
void FarthestInsertion(const RouteParameters & route_parameters,
|
||||
const PhantomNodeArray & phantom_node_vector,
|
||||
const std::vector<EdgeWeight> & dist_table,
|
||||
InternalRouteResult & min_route,
|
||||
std::vector<int> & min_loc_permutation) {
|
||||
void FarthestInsertionTSP(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
|
||||
@@ -67,6 +66,8 @@ void FarthestInsertion(const RouteParameters & route_parameters,
|
||||
// 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()));
|
||||
|
||||
@@ -48,11 +48,10 @@ namespace osrm
|
||||
namespace tsp
|
||||
{
|
||||
|
||||
void NearestNeighbour(const RouteParameters & route_parameters,
|
||||
const PhantomNodeArray & phantom_node_vector,
|
||||
const std::vector<EdgeWeight> & dist_table,
|
||||
InternalRouteResult & min_route,
|
||||
std::vector<int> & min_loc_permutation) {
|
||||
void NearestNeighbourTSP(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
|
||||
|
||||
Reference in New Issue
Block a user