sl@0: sl@0: sl@0: // sl@0: //======================================================================= sl@0: // Copyright (c) 2004 Kristopher Beevers sl@0: // sl@0: // Distributed under the Boost Software License, Version 1.0. (See sl@0: // accompanying file LICENSE_1_0.txt or copy at sl@0: // http://www.boost.org/LICENSE_1_0.txt) sl@0: //======================================================================= sl@0: // sl@0: sl@0: #ifndef BOOST_GRAPH_ASTAR_SEARCH_HPP sl@0: #define BOOST_GRAPH_ASTAR_SEARCH_HPP sl@0: sl@0: sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: sl@0: sl@0: namespace boost { sl@0: sl@0: sl@0: template sl@0: struct AStarHeuristicConcept { sl@0: void constraints() sl@0: { sl@0: function_requires< CopyConstructibleConcept >(); sl@0: h(u); sl@0: } sl@0: Heuristic h; sl@0: typename graph_traits::vertex_descriptor u; sl@0: }; sl@0: sl@0: sl@0: template sl@0: class astar_heuristic : public std::unary_function< sl@0: typename graph_traits::vertex_descriptor, CostType> sl@0: { sl@0: public: sl@0: typedef typename graph_traits::vertex_descriptor Vertex; sl@0: astar_heuristic() {} sl@0: CostType operator()(Vertex u) { return static_cast(0); } sl@0: }; sl@0: sl@0: sl@0: sl@0: template sl@0: struct AStarVisitorConcept { sl@0: void constraints() sl@0: { sl@0: function_requires< CopyConstructibleConcept >(); sl@0: vis.initialize_vertex(u, g); sl@0: vis.discover_vertex(u, g); sl@0: vis.examine_vertex(u, g); sl@0: vis.examine_edge(e, g); sl@0: vis.edge_relaxed(e, g); sl@0: vis.edge_not_relaxed(e, g); sl@0: vis.black_target(e, g); sl@0: vis.finish_vertex(u, g); sl@0: } sl@0: Visitor vis; sl@0: Graph g; sl@0: typename graph_traits::vertex_descriptor u; sl@0: typename graph_traits::edge_descriptor e; sl@0: }; sl@0: sl@0: sl@0: template sl@0: class astar_visitor : public bfs_visitor { sl@0: public: sl@0: astar_visitor() {} sl@0: astar_visitor(Visitors vis) sl@0: : bfs_visitor(vis) {} sl@0: sl@0: template sl@0: void edge_relaxed(Edge e, Graph& g) { sl@0: invoke_visitors(this->m_vis, e, g, on_edge_relaxed()); sl@0: } sl@0: template sl@0: void edge_not_relaxed(Edge e, Graph& g) { sl@0: invoke_visitors(this->m_vis, e, g, on_edge_not_relaxed()); sl@0: } sl@0: private: sl@0: template sl@0: void tree_edge(Edge e, Graph& g) {} sl@0: template sl@0: void non_tree_edge(Edge e, Graph& g) {} sl@0: }; sl@0: template sl@0: astar_visitor sl@0: make_astar_visitor(Visitors vis) { sl@0: return astar_visitor(vis); sl@0: } sl@0: typedef astar_visitor<> default_astar_visitor; sl@0: sl@0: sl@0: namespace detail { sl@0: sl@0: template sl@0: struct astar_bfs_visitor sl@0: { sl@0: sl@0: typedef typename property_traits::value_type C; sl@0: typedef typename property_traits::value_type ColorValue; sl@0: typedef color_traits Color; sl@0: typedef typename property_traits::value_type distance_type; sl@0: sl@0: astar_bfs_visitor(AStarHeuristic h, UniformCostVisitor vis, sl@0: UpdatableQueue& Q, PredecessorMap p, sl@0: CostMap c, DistanceMap d, WeightMap w, sl@0: ColorMap col, BinaryFunction combine, sl@0: BinaryPredicate compare, C zero) sl@0: : m_h(h), m_vis(vis), m_Q(Q), m_predecessor(p), m_cost(c), sl@0: m_distance(d), m_weight(w), m_color(col), sl@0: m_combine(combine), m_compare(compare), m_zero(zero) {} sl@0: sl@0: sl@0: template sl@0: void initialize_vertex(Vertex u, Graph& g) { sl@0: m_vis.initialize_vertex(u, g); sl@0: } sl@0: template sl@0: void discover_vertex(Vertex u, Graph& g) { sl@0: m_vis.discover_vertex(u, g); sl@0: } sl@0: template sl@0: void examine_vertex(Vertex u, Graph& g) { sl@0: m_vis.examine_vertex(u, g); sl@0: } sl@0: template sl@0: void finish_vertex(Vertex u, Graph& g) { sl@0: m_vis.finish_vertex(u, g); sl@0: } sl@0: template sl@0: void examine_edge(Edge e, Graph& g) { sl@0: if (m_compare(get(m_weight, e), m_zero)) sl@0: throw negative_edge(); sl@0: m_vis.examine_edge(e, g); sl@0: } sl@0: template sl@0: void non_tree_edge(Edge, Graph&) {} sl@0: sl@0: sl@0: sl@0: template sl@0: void tree_edge(Edge e, Graph& g) { sl@0: m_decreased = relax(e, g, m_weight, m_predecessor, m_distance, sl@0: m_combine, m_compare); sl@0: sl@0: if(m_decreased) { sl@0: m_vis.edge_relaxed(e, g); sl@0: put(m_cost, target(e, g), sl@0: m_combine(get(m_distance, target(e, g)), sl@0: m_h(target(e, g)))); sl@0: } else sl@0: m_vis.edge_not_relaxed(e, g); sl@0: } sl@0: sl@0: sl@0: template sl@0: void gray_target(Edge e, Graph& g) { sl@0: distance_type old_distance = get(m_distance, target(e, g)); sl@0: sl@0: m_decreased = relax(e, g, m_weight, m_predecessor, m_distance, sl@0: m_combine, m_compare); sl@0: sl@0: /* On x86 Linux with optimization, we sometimes get into a sl@0: horrible case where m_decreased is true but the distance hasn't sl@0: actually changed. This occurs when the comparison inside sl@0: relax() occurs with the 80-bit precision of the x87 floating sl@0: point unit, but the difference is lost when the resulting sl@0: values are written back to lower-precision memory (e.g., a sl@0: double). With the eager Dijkstra's implementation, this results sl@0: in looping. */ sl@0: if(m_decreased && old_distance != get(m_distance, target(e, g))) { sl@0: put(m_cost, target(e, g), sl@0: m_combine(get(m_distance, target(e, g)), sl@0: m_h(target(e, g)))); sl@0: m_Q.update(target(e, g)); sl@0: m_vis.edge_relaxed(e, g); sl@0: } else sl@0: m_vis.edge_not_relaxed(e, g); sl@0: } sl@0: sl@0: sl@0: template sl@0: void black_target(Edge e, Graph& g) { sl@0: distance_type old_distance = get(m_distance, target(e, g)); sl@0: sl@0: m_decreased = relax(e, g, m_weight, m_predecessor, m_distance, sl@0: m_combine, m_compare); sl@0: sl@0: /* See comment in gray_target */ sl@0: if(m_decreased && old_distance != get(m_distance, target(e, g))) { sl@0: m_vis.edge_relaxed(e, g); sl@0: put(m_cost, target(e, g), sl@0: m_combine(get(m_distance, target(e, g)), sl@0: m_h(target(e, g)))); sl@0: m_Q.push(target(e, g)); sl@0: put(m_color, target(e, g), Color::gray()); sl@0: m_vis.black_target(e, g); sl@0: } else sl@0: m_vis.edge_not_relaxed(e, g); sl@0: } sl@0: sl@0: sl@0: sl@0: AStarHeuristic m_h; sl@0: UniformCostVisitor m_vis; sl@0: UpdatableQueue& m_Q; sl@0: PredecessorMap m_predecessor; sl@0: CostMap m_cost; sl@0: DistanceMap m_distance; sl@0: WeightMap m_weight; sl@0: ColorMap m_color; sl@0: BinaryFunction m_combine; sl@0: BinaryPredicate m_compare; sl@0: bool m_decreased; sl@0: C m_zero; sl@0: sl@0: }; sl@0: sl@0: } // namespace detail sl@0: sl@0: sl@0: sl@0: template sl@0: inline void sl@0: astar_search_no_init sl@0: (VertexListGraph &g, sl@0: typename graph_traits::vertex_descriptor s, sl@0: AStarHeuristic h, AStarVisitor vis, sl@0: PredecessorMap predecessor, CostMap cost, sl@0: DistanceMap distance, WeightMap weight, sl@0: ColorMap color, VertexIndexMap index_map, sl@0: CompareFunction compare, CombineFunction combine, sl@0: CostInf inf, CostZero zero) sl@0: { sl@0: typedef indirect_cmp IndirectCmp; sl@0: IndirectCmp icmp(cost, compare); sl@0: sl@0: typedef typename graph_traits::vertex_descriptor sl@0: Vertex; sl@0: typedef mutable_queue, sl@0: IndirectCmp, VertexIndexMap> sl@0: MutableQueue; sl@0: MutableQueue Q(num_vertices(g), icmp, index_map); sl@0: sl@0: detail::astar_bfs_visitor sl@0: bfs_vis(h, vis, Q, predecessor, cost, distance, weight, sl@0: color, combine, compare, zero); sl@0: sl@0: breadth_first_visit(g, s, Q, bfs_vis, color); sl@0: } sl@0: sl@0: sl@0: // Non-named parameter interface sl@0: template sl@0: inline void sl@0: astar_search sl@0: (VertexListGraph &g, sl@0: typename graph_traits::vertex_descriptor s, sl@0: AStarHeuristic h, AStarVisitor vis, sl@0: PredecessorMap predecessor, CostMap cost, sl@0: DistanceMap distance, WeightMap weight, sl@0: VertexIndexMap index_map, ColorMap color, sl@0: CompareFunction compare, CombineFunction combine, sl@0: CostInf inf, CostZero zero) sl@0: { sl@0: sl@0: typedef typename property_traits::value_type ColorValue; sl@0: typedef color_traits Color; sl@0: typename graph_traits::vertex_iterator ui, ui_end; sl@0: for (tie(ui, ui_end) = vertices(g); ui != ui_end; ++ui) { sl@0: put(color, *ui, Color::white()); sl@0: put(distance, *ui, inf); sl@0: put(cost, *ui, inf); sl@0: put(predecessor, *ui, *ui); sl@0: vis.initialize_vertex(*ui, g); sl@0: } sl@0: put(distance, s, zero); sl@0: put(cost, s, h(s)); sl@0: sl@0: astar_search_no_init sl@0: (g, s, h, vis, predecessor, cost, distance, weight, sl@0: color, index_map, compare, combine, inf, zero); sl@0: sl@0: } sl@0: sl@0: sl@0: sl@0: namespace detail { sl@0: template sl@0: inline void sl@0: astar_dispatch2 sl@0: (VertexListGraph& g, sl@0: typename graph_traits::vertex_descriptor s, sl@0: AStarHeuristic h, CostMap cost, DistanceMap distance, sl@0: WeightMap weight, IndexMap index_map, ColorMap color, sl@0: const Params& params) sl@0: { sl@0: dummy_property_map p_map; sl@0: typedef typename property_traits::value_type C; sl@0: astar_search sl@0: (g, s, h, sl@0: choose_param(get_param(params, graph_visitor), sl@0: make_astar_visitor(null_visitor())), sl@0: choose_param(get_param(params, vertex_predecessor), p_map), sl@0: cost, distance, weight, index_map, color, sl@0: choose_param(get_param(params, distance_compare_t()), sl@0: std::less()), sl@0: choose_param(get_param(params, distance_combine_t()), sl@0: closed_plus()), sl@0: choose_param(get_param(params, distance_inf_t()), sl@0: std::numeric_limits::max BOOST_PREVENT_MACRO_SUBSTITUTION ()), sl@0: choose_param(get_param(params, distance_zero_t()), sl@0: C())); sl@0: } sl@0: sl@0: template sl@0: inline void sl@0: astar_dispatch1 sl@0: (VertexListGraph& g, sl@0: typename graph_traits::vertex_descriptor s, sl@0: AStarHeuristic h, CostMap cost, DistanceMap distance, sl@0: WeightMap weight, IndexMap index_map, ColorMap color, sl@0: const Params& params) sl@0: { sl@0: typedef typename property_traits::value_type D; sl@0: typename std::vector::size_type sl@0: n = is_default_param(distance) ? num_vertices(g) : 1; sl@0: std::vector distance_map(n); sl@0: n = is_default_param(cost) ? num_vertices(g) : 1; sl@0: std::vector cost_map(n); sl@0: std::vector color_map(num_vertices(g)); sl@0: default_color_type c = white_color; sl@0: sl@0: detail::astar_dispatch2 sl@0: (g, s, h, sl@0: choose_param(cost, make_iterator_property_map sl@0: (cost_map.begin(), index_map, sl@0: cost_map[0])), sl@0: choose_param(distance, make_iterator_property_map sl@0: (distance_map.begin(), index_map, sl@0: distance_map[0])), sl@0: weight, index_map, sl@0: choose_param(color, make_iterator_property_map sl@0: (color_map.begin(), index_map, c)), sl@0: params); sl@0: } sl@0: } // namespace detail sl@0: sl@0: sl@0: // Named parameter interface sl@0: template sl@0: void sl@0: astar_search sl@0: (VertexListGraph &g, sl@0: typename graph_traits::vertex_descriptor s, sl@0: AStarHeuristic h, const bgl_named_params& params) sl@0: { sl@0: sl@0: detail::astar_dispatch1 sl@0: (g, s, h, sl@0: get_param(params, vertex_rank), sl@0: get_param(params, vertex_distance), sl@0: choose_const_pmap(get_param(params, edge_weight), g, edge_weight), sl@0: choose_const_pmap(get_param(params, vertex_index), g, vertex_index), sl@0: get_param(params, vertex_color), sl@0: params); sl@0: sl@0: } sl@0: sl@0: } // namespace boost sl@0: sl@0: #endif // BOOST_GRAPH_ASTAR_SEARCH_HPP