27 #ifndef INCLUDE_ASTAR_PGR_ASTAR_HPP_ 28 #define INCLUDE_ASTAR_PGR_ASTAR_HPP_ 31 #include <boost/config.hpp> 32 #include <boost/graph/graph_traits.hpp> 33 #include <boost/graph/adjacency_list.hpp> 34 #include <boost/graph/astar_search.hpp> 49 namespace algorithms {
54 typedef typename G::V
V;
55 typedef typename G::B_G
B_G;
80 if (!graph.has_vertex(start_vertex)
81 || !graph.has_vertex(end_vertex)) {
82 return Path(start_vertex, end_vertex);
85 auto v_source(graph.get_V(start_vertex));
86 auto v_target(graph.get_V(end_vertex));
89 astar_1_to_1(graph, v_source, v_target, heuristic, factor, epsilon);
100 int64_t start_vertex,
101 std::vector<int64_t> end_vertex,
111 if (!graph.has_vertex(start_vertex))
return std::deque<Path>();
112 auto v_source(graph.get_V(start_vertex));
114 std::vector<V> v_targets;
115 for (
const auto &
vertex : end_vertex) {
116 if (graph.has_vertex(
vertex)) {
117 v_targets.push_back(graph.get_V(
vertex));
128 auto paths =
get_paths(graph, v_source, v_targets, only_cost);
130 std::stable_sort(paths.begin(), paths.end(),
131 [](
const Path &e1,
const Path &e2)->
bool {
132 return e1.
end_id() < e2.end_id();
141 std::vector<int64_t> start_vertex,
142 std::vector<int64_t> end_vertex,
147 std::deque<Path> paths;
148 for (
const auto &start : start_vertex) {
149 auto r_paths =
astar(graph, start, end_vertex,
150 heuristic, factor, epsilon, only_cost);
151 paths.insert(paths.begin(), r_paths.begin(), r_paths.end());
154 std::sort(paths.begin(), paths.end(),
155 [](
const Path &e1,
const Path &e2)->
bool {
156 return e1.
end_id() < e2.end_id();
158 std::stable_sort(paths.begin(), paths.end(),
159 [](
const Path &e1,
const Path &e2)->
bool {
160 return e1.
start_id() < e2.start_id();
183 m_heuristic(heuristic) {
184 m_goals.insert(goal);
188 std::vector< V > goals,
192 m_goals(goals.begin(), goals.end()),
194 m_heuristic(heuristic) {}
197 if (m_heuristic == 0)
return 0;
198 if (m_goals.empty())
return 0;
199 double best_h((std::numeric_limits<double>::max)());
200 for (
auto goal : m_goals) {
201 double current((std::numeric_limits<double>::max)());
202 double dx = m_g[goal].x() - m_g[u].x();
203 double dy = m_g[goal].y() - m_g[u].y();
204 switch (m_heuristic) {
208 current = std::fabs((std::max)(dx, dy)) * m_factor;
210 current = std::fabs((std::min)(dx, dy)) * m_factor;
212 current = (dx * dx + dy * dy) * m_factor * m_factor;
214 current = std::sqrt(dx * dx + dy * dy) * m_factor;
216 current = (std::fabs(dx) + std::fabs(dy)) * m_factor;
220 if (current < best_h) {
225 auto s_it = m_goals.find(u);
226 if (!(s_it == m_goals.end())) {
261 :m_goals(goals.begin(), goals.end()) {}
264 auto s_it = m_goals.find(u);
265 if (s_it == m_goals.end())
return;
293 heuristic, factor * epsilon),
294 boost::predecessor_map(&predecessors[0])
296 .distance_map(&distances[0])
310 const std::vector< V > &targets,
319 graph.graph, targets,
320 heuristic, factor * epsilon),
321 boost::predecessor_map(&predecessors[0])
323 .distance_map(&distances[0])
341 const std::vector<V> &targets,
342 bool only_cost)
const {
343 std::deque<Path> paths;
344 for (
const auto &target : targets) {
348 predecessors, distances,
359 #endif // INCLUDE_ASTAR_PGR_ASTAR_HPP_ class for stopping when all targets are found
bool astar_1_to_1(G &graph, V source, V target, int heuristic, double factor, double epsilon)
Call to Astar 1 source to 1 target.
distance_heuristic(B_G &g, std::vector< V > goals, int heuristic, double factor)
visitor that terminates when we find the goal
std::deque< Path > astar(G &graph, int64_t start_vertex, std::vector< int64_t > end_vertex, int heuristic, double factor, double epsilon, bool only_cost)
astar 1 to many
bool astar_1_to_many(G &graph, V source, const std::vector< V > &targets, int heuristic, double factor, double epsilon)
Call to astar 1 source to many targets.
std::deque< V > nodesInDistance
std::deque< Path > get_paths(const G &graph, V source, const std::vector< V > &targets, bool only_cost) const
exception for termination
Path astar(G &graph, int64_t start_vertex, int64_t end_vertex, int heuristic, double factor, double epsilon, bool only_cost)
one to one astar 1 to 1
std::vector< V > predecessors
void examine_vertex(V u, B_G &g)
astar_many_goals_visitor(std::vector< V > goals)
Book keeping class for swapping orders between vehicles.
distance_heuristic(B_G &g, V goal, int heuristic, double factor)
std::deque< Path > astar(G &graph, std::vector< int64_t > start_vertex, std::vector< int64_t > end_vertex, int heuristic, double factor, double epsilon, bool only_cost)
astar_one_goal_visitor(V goal)
void examine_vertex(V u, B_G &g)
std::vector< double > distances