2015-06-16 17:19:51 -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_BRUTE_FORCE_HPP
|
|
|
|
#define TSP_BRUTE_FORCE_HPP
|
|
|
|
|
|
|
|
|
|
|
|
#include "../data_structures/search_engine.hpp"
|
|
|
|
#include "../util/string_util.hpp"
|
|
|
|
|
|
|
|
#include <osrm/json_container.hpp>
|
|
|
|
|
|
|
|
#include <cstdlib>
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
#include <limits>
|
2015-06-22 14:09:00 -04:00
|
|
|
#include <iostream>
|
2015-06-16 17:19:51 -04:00
|
|
|
#include "../util/simple_logger.hpp"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace osrm
|
|
|
|
{
|
|
|
|
namespace tsp
|
|
|
|
{
|
2015-06-22 14:09:00 -04:00
|
|
|
|
2015-07-04 18:15:55 -04:00
|
|
|
template <typename number>
|
2015-08-18 07:48:12 -04:00
|
|
|
int ReturnDistance(const std::vector<EdgeWeight> & dist_table,
|
|
|
|
const std::vector<number> & location_order,
|
2015-08-19 04:25:32 -04:00
|
|
|
const EdgeWeight min_route_dist,
|
2015-08-18 07:48:12 -04:00
|
|
|
const int number_of_locations) {
|
2015-06-22 14:09:00 -04:00
|
|
|
int route_dist = 0;
|
2015-08-18 07:48:12 -04:00
|
|
|
int i = 0;
|
2015-08-19 04:25:32 -04:00
|
|
|
while (i < location_order.size() - 1) {
|
2015-06-22 14:09:00 -04:00
|
|
|
route_dist += *(dist_table.begin() + (location_order[i] * number_of_locations) + location_order[i+1]);
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
//get distance from last location to first location
|
2015-08-18 07:48:12 -04:00
|
|
|
route_dist += *(dist_table.begin() + (location_order[location_order.size()-1] * number_of_locations) + location_order[0]);
|
|
|
|
return route_dist;
|
2015-06-22 14:09:00 -04:00
|
|
|
}
|
|
|
|
|
2015-08-19 04:25:32 -04:00
|
|
|
std::vector<NodeID> BruteForceTSP(std::vector<NodeID> & component,
|
|
|
|
const std::size_t number_of_locations,
|
|
|
|
const std::vector<EdgeWeight> & dist_table) {
|
2015-07-04 18:15:55 -04:00
|
|
|
|
2015-08-19 04:25:32 -04:00
|
|
|
std::vector<NodeID> route;
|
|
|
|
route.reserve(number_of_locations);
|
|
|
|
|
|
|
|
|
|
|
|
EdgeWeight min_route_dist = INVALID_EDGE_WEIGHT;
|
2015-07-04 18:15:55 -04:00
|
|
|
|
2015-08-18 07:48:12 -04:00
|
|
|
// check length of all possible permutation of the component ids
|
2015-07-04 18:15:55 -04:00
|
|
|
do {
|
2015-08-19 04:25:32 -04:00
|
|
|
const auto new_distance = ReturnDistance(dist_table, component, min_route_dist, number_of_locations);
|
|
|
|
if (new_distance <= min_route_dist) {
|
2015-07-04 18:15:55 -04:00
|
|
|
min_route_dist = new_distance;
|
2015-08-18 07:48:12 -04:00
|
|
|
route = component;
|
2015-07-04 18:15:55 -04:00
|
|
|
}
|
2015-08-18 07:48:12 -04:00
|
|
|
} while(std::next_permutation(component.begin(), component.end()));
|
2015-08-19 04:25:32 -04:00
|
|
|
|
|
|
|
return route;
|
2015-07-04 18:15:55 -04:00
|
|
|
}
|
|
|
|
|
2015-08-19 05:35:36 -04:00
|
|
|
} //end namespace osrm
|
|
|
|
} //end namespace tsp
|
2015-06-16 17:19:51 -04:00
|
|
|
#endif // TSP_BRUTE_FORCE_HPP
|