// //======================================================================= // Copyright (c) 2004 Kristopher Beevers // // Distributed under the Boost Software License, Version 1.0. (See // accompanying file LICENSE_1_0.txt or copy at // http://www.boost.org/LICENSE_1_0.txt) //======================================================================= // #ifndef BOOST_GRAPH_ASTAR_SEARCH_HPP #define BOOST_GRAPH_ASTAR_SEARCH_HPP #include #include #include #include #include #include #include #include #include #include #include #include namespace boost { template struct AStarHeuristicConcept { void constraints() { BOOST_CONCEPT_ASSERT(( CopyConstructibleConcept )); h(u); } Heuristic h; typename graph_traits::vertex_descriptor u; }; template class astar_heuristic : public std::unary_function< typename graph_traits::vertex_descriptor, CostType> { public: typedef typename graph_traits::vertex_descriptor Vertex; astar_heuristic() {} CostType operator()(Vertex u) { return static_cast(0); } }; template struct AStarVisitorConcept { void constraints() { BOOST_CONCEPT_ASSERT(( CopyConstructibleConcept )); vis.initialize_vertex(u, g); vis.discover_vertex(u, g); vis.examine_vertex(u, g); vis.examine_edge(e, g); vis.edge_relaxed(e, g); vis.edge_not_relaxed(e, g); vis.black_target(e, g); vis.finish_vertex(u, g); } Visitor vis; Graph g; typename graph_traits::vertex_descriptor u; typename graph_traits::edge_descriptor e; }; template class astar_visitor : public bfs_visitor { public: astar_visitor() {} astar_visitor(Visitors vis) : bfs_visitor(vis) {} template void edge_relaxed(Edge e, const Graph& g) { invoke_visitors(this->m_vis, e, g, on_edge_relaxed()); } template void edge_not_relaxed(Edge e, const Graph& g) { invoke_visitors(this->m_vis, e, g, on_edge_not_relaxed()); } private: template void tree_edge(Edge e, const Graph& g) {} template void non_tree_edge(Edge e, const Graph& g) {} }; template astar_visitor make_astar_visitor(Visitors vis) { return astar_visitor(vis); } typedef astar_visitor<> default_astar_visitor; namespace detail { template struct astar_bfs_visitor { typedef typename property_traits::value_type C; typedef typename property_traits::value_type ColorValue; typedef color_traits Color; typedef typename property_traits::value_type distance_type; astar_bfs_visitor(AStarHeuristic h, UniformCostVisitor vis, UpdatableQueue& Q, PredecessorMap p, CostMap c, DistanceMap d, WeightMap w, ColorMap col, BinaryFunction combine, BinaryPredicate compare, C zero) : m_h(h), m_vis(vis), m_Q(Q), m_predecessor(p), m_cost(c), m_distance(d), m_weight(w), m_color(col), m_combine(combine), m_compare(compare), m_zero(zero) {} template void initialize_vertex(Vertex u, const Graph& g) { m_vis.initialize_vertex(u, g); } template void discover_vertex(Vertex u, const Graph& g) { m_vis.discover_vertex(u, g); } template void examine_vertex(Vertex u, const Graph& g) { m_vis.examine_vertex(u, g); } template void finish_vertex(Vertex u, const Graph& g) { m_vis.finish_vertex(u, g); } template void examine_edge(Edge e, const Graph& g) { if (m_compare(get(m_weight, e), m_zero)) BOOST_THROW_EXCEPTION(negative_edge()); m_vis.examine_edge(e, g); } template void non_tree_edge(Edge, const Graph&) {} template void tree_edge(Edge e, const Graph& g) { using boost::get; m_decreased = relax(e, g, m_weight, m_predecessor, m_distance, m_combine, m_compare); if(m_decreased) { m_vis.edge_relaxed(e, g); put(m_cost, target(e, g), m_combine(get(m_distance, target(e, g)), m_h(target(e, g)))); } else m_vis.edge_not_relaxed(e, g); } template void gray_target(Edge e, const Graph& g) { using boost::get; m_decreased = relax(e, g, m_weight, m_predecessor, m_distance, m_combine, m_compare); if(m_decreased) { put(m_cost, target(e, g), m_combine(get(m_distance, target(e, g)), m_h(target(e, g)))); m_Q.update(target(e, g)); m_vis.edge_relaxed(e, g); } else m_vis.edge_not_relaxed(e, g); } template void black_target(Edge e, const Graph& g) { using boost::get; m_decreased = relax(e, g, m_weight, m_predecessor, m_distance, m_combine, m_compare); if(m_decreased) { m_vis.edge_relaxed(e, g); put(m_cost, target(e, g), m_combine(get(m_distance, target(e, g)), m_h(target(e, g)))); m_Q.push(target(e, g)); put(m_color, target(e, g), Color::gray()); m_vis.black_target(e, g); } else m_vis.edge_not_relaxed(e, g); } AStarHeuristic m_h; UniformCostVisitor m_vis; UpdatableQueue& m_Q; PredecessorMap m_predecessor; CostMap m_cost; DistanceMap m_distance; WeightMap m_weight; ColorMap m_color; BinaryFunction m_combine; BinaryPredicate m_compare; bool m_decreased; C m_zero; }; } // namespace detail template inline void astar_search_no_init (const VertexListGraph &g, typename graph_traits::vertex_descriptor s, AStarHeuristic h, AStarVisitor vis, PredecessorMap predecessor, CostMap cost, DistanceMap distance, WeightMap weight, ColorMap color, VertexIndexMap index_map, CompareFunction compare, CombineFunction combine, CostInf /*inf*/, CostZero zero) { typedef typename graph_traits::vertex_descriptor Vertex; typedef boost::vector_property_map IndexInHeapMap; IndexInHeapMap index_in_heap(index_map); typedef d_ary_heap_indirect MutableQueue; MutableQueue Q(cost, index_in_heap, compare); detail::astar_bfs_visitor bfs_vis(h, vis, Q, predecessor, cost, distance, weight, color, combine, compare, zero); breadth_first_visit(g, s, Q, bfs_vis, color); } // Non-named parameter interface template inline void astar_search (const VertexListGraph &g, typename graph_traits::vertex_descriptor s, AStarHeuristic h, AStarVisitor vis, PredecessorMap predecessor, CostMap cost, DistanceMap distance, WeightMap weight, VertexIndexMap index_map, ColorMap color, CompareFunction compare, CombineFunction combine, CostInf inf, CostZero zero) { typedef typename property_traits::value_type ColorValue; typedef color_traits Color; typename graph_traits::vertex_iterator ui, ui_end; for (boost::tie(ui, ui_end) = vertices(g); ui != ui_end; ++ui) { put(color, *ui, Color::white()); put(distance, *ui, inf); put(cost, *ui, inf); put(predecessor, *ui, *ui); vis.initialize_vertex(*ui, g); } put(distance, s, zero); put(cost, s, h(s)); astar_search_no_init (g, s, h, vis, predecessor, cost, distance, weight, color, index_map, compare, combine, inf, zero); } // Named parameter interfaces template void astar_search (const VertexListGraph &g, typename graph_traits::vertex_descriptor s, AStarHeuristic h, const bgl_named_params& params) { using namespace boost::graph::keywords; typedef bgl_named_params params_type; BOOST_GRAPH_DECLARE_CONVERTED_PARAMETERS(params_type, params) // Distance type is the value type of the distance map if there is one, // otherwise the value type of the weight map. typedef typename detail::override_const_property_result< arg_pack_type, tag::weight_map, edge_weight_t, VertexListGraph>::type weight_map_type; typedef typename boost::property_traits::value_type W; typedef typename detail::map_maker::map_type distance_map_type; typedef typename boost::property_traits::value_type D; const D inf = arg_pack[_distance_inf | (std::numeric_limits::max)()]; astar_search (g, s, h, arg_pack[_visitor | make_astar_visitor(null_visitor())], arg_pack[_predecessor_map | dummy_property_map()], detail::make_property_map_from_arg_pack_gen(D())(g, arg_pack), detail::make_property_map_from_arg_pack_gen(W())(g, arg_pack), detail::override_const_property(arg_pack, _weight_map, g, edge_weight), detail::override_const_property(arg_pack, _vertex_index_map, g, vertex_index), detail::make_color_map_from_arg_pack(g, arg_pack), arg_pack[_distance_compare | std::less()], arg_pack[_distance_combine | closed_plus(inf)], inf, arg_pack[_distance_zero | D()]); } template void astar_search_no_init (const VertexListGraph &g, typename graph_traits::vertex_descriptor s, AStarHeuristic h, const bgl_named_params& params) { using namespace boost::graph::keywords; typedef bgl_named_params params_type; BOOST_GRAPH_DECLARE_CONVERTED_PARAMETERS(params_type, params) typedef typename detail::override_const_property_result< arg_pack_type, tag::weight_map, edge_weight_t, VertexListGraph>::type weight_map_type; typedef typename boost::property_traits::value_type D; const D inf = arg_pack[_distance_inf | (std::numeric_limits::max)()]; astar_search_no_init (g, s, h, arg_pack[_visitor | make_astar_visitor(null_visitor())], arg_pack[_predecessor_map | dummy_property_map()], detail::make_property_map_from_arg_pack_gen(D())(g, arg_pack), detail::make_property_map_from_arg_pack_gen(D())(g, arg_pack), detail::override_const_property(arg_pack, _weight_map, g, edge_weight), detail::override_const_property(arg_pack, _vertex_index_map, g, vertex_index), detail::make_color_map_from_arg_pack(g, arg_pack), arg_pack[_distance_compare | std::less()], arg_pack[_distance_combine | closed_plus(inf)], inf, arg_pack[_distance_zero | D()]); } } // namespace boost #endif // BOOST_GRAPH_ASTAR_SEARCH_HPP