os/ossrv/ossrv_pub/boost_apis/boost/graph/astar_search.hpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
sl@0
     2
sl@0
     3
//
sl@0
     4
//=======================================================================
sl@0
     5
// Copyright (c) 2004 Kristopher Beevers
sl@0
     6
//
sl@0
     7
// Distributed under the Boost Software License, Version 1.0. (See
sl@0
     8
// accompanying file LICENSE_1_0.txt or copy at
sl@0
     9
// http://www.boost.org/LICENSE_1_0.txt)
sl@0
    10
//=======================================================================
sl@0
    11
//
sl@0
    12
sl@0
    13
#ifndef BOOST_GRAPH_ASTAR_SEARCH_HPP
sl@0
    14
#define BOOST_GRAPH_ASTAR_SEARCH_HPP
sl@0
    15
sl@0
    16
sl@0
    17
#include <functional>
sl@0
    18
#include <boost/limits.hpp>
sl@0
    19
#include <boost/graph/named_function_params.hpp>
sl@0
    20
#include <boost/pending/mutable_queue.hpp>
sl@0
    21
#include <boost/graph/relax.hpp>
sl@0
    22
#include <boost/pending/indirect_cmp.hpp>
sl@0
    23
#include <boost/graph/exception.hpp>
sl@0
    24
#include <boost/graph/breadth_first_search.hpp>
sl@0
    25
sl@0
    26
sl@0
    27
namespace boost {
sl@0
    28
sl@0
    29
  
sl@0
    30
  template <class Heuristic, class Graph>
sl@0
    31
  struct AStarHeuristicConcept {
sl@0
    32
    void constraints()
sl@0
    33
    {
sl@0
    34
      function_requires< CopyConstructibleConcept<Heuristic> >();
sl@0
    35
      h(u);
sl@0
    36
    }
sl@0
    37
    Heuristic h;
sl@0
    38
    typename graph_traits<Graph>::vertex_descriptor u;
sl@0
    39
  };
sl@0
    40
  
sl@0
    41
  
sl@0
    42
  template <class Graph, class CostType>
sl@0
    43
  class astar_heuristic : public std::unary_function<
sl@0
    44
    typename graph_traits<Graph>::vertex_descriptor, CostType>
sl@0
    45
  {
sl@0
    46
  public:
sl@0
    47
    typedef typename graph_traits<Graph>::vertex_descriptor Vertex;
sl@0
    48
    astar_heuristic() {}
sl@0
    49
    CostType operator()(Vertex u) { return static_cast<CostType>(0); }
sl@0
    50
  };
sl@0
    51
  
sl@0
    52
sl@0
    53
  
sl@0
    54
  template <class Visitor, class Graph>
sl@0
    55
  struct AStarVisitorConcept {
sl@0
    56
    void constraints()
sl@0
    57
    {
sl@0
    58
      function_requires< CopyConstructibleConcept<Visitor> >();
sl@0
    59
      vis.initialize_vertex(u, g);
sl@0
    60
      vis.discover_vertex(u, g);
sl@0
    61
      vis.examine_vertex(u, g);
sl@0
    62
      vis.examine_edge(e, g);
sl@0
    63
      vis.edge_relaxed(e, g);
sl@0
    64
      vis.edge_not_relaxed(e, g);
sl@0
    65
      vis.black_target(e, g);
sl@0
    66
      vis.finish_vertex(u, g);
sl@0
    67
    }
sl@0
    68
    Visitor vis;
sl@0
    69
    Graph g;
sl@0
    70
    typename graph_traits<Graph>::vertex_descriptor u;
sl@0
    71
    typename graph_traits<Graph>::edge_descriptor e;
sl@0
    72
  };
sl@0
    73
  
sl@0
    74
  
sl@0
    75
  template <class Visitors = null_visitor>
sl@0
    76
  class astar_visitor : public bfs_visitor<Visitors> {
sl@0
    77
  public:
sl@0
    78
    astar_visitor() {}
sl@0
    79
    astar_visitor(Visitors vis)
sl@0
    80
      : bfs_visitor<Visitors>(vis) {}
sl@0
    81
  
sl@0
    82
    template <class Edge, class Graph>
sl@0
    83
    void edge_relaxed(Edge e, Graph& g) {
sl@0
    84
      invoke_visitors(this->m_vis, e, g, on_edge_relaxed());
sl@0
    85
    }
sl@0
    86
    template <class Edge, class Graph>
sl@0
    87
    void edge_not_relaxed(Edge e, Graph& g) {
sl@0
    88
      invoke_visitors(this->m_vis, e, g, on_edge_not_relaxed());      
sl@0
    89
    }
sl@0
    90
  private:
sl@0
    91
    template <class Edge, class Graph>
sl@0
    92
    void tree_edge(Edge e, Graph& g) {}
sl@0
    93
    template <class Edge, class Graph>
sl@0
    94
    void non_tree_edge(Edge e, Graph& g) {}
sl@0
    95
  };
sl@0
    96
  template <class Visitors>
sl@0
    97
  astar_visitor<Visitors>
sl@0
    98
  make_astar_visitor(Visitors vis) {
sl@0
    99
    return astar_visitor<Visitors>(vis);
sl@0
   100
  }
sl@0
   101
  typedef astar_visitor<> default_astar_visitor;
sl@0
   102
  
sl@0
   103
sl@0
   104
  namespace detail {
sl@0
   105
    
sl@0
   106
    template <class AStarHeuristic, class UniformCostVisitor,
sl@0
   107
              class UpdatableQueue, class PredecessorMap,
sl@0
   108
              class CostMap, class DistanceMap, class WeightMap,
sl@0
   109
              class ColorMap, class BinaryFunction,
sl@0
   110
              class BinaryPredicate>
sl@0
   111
    struct astar_bfs_visitor
sl@0
   112
    {
sl@0
   113
      
sl@0
   114
      typedef typename property_traits<CostMap>::value_type C;
sl@0
   115
      typedef typename property_traits<ColorMap>::value_type ColorValue;
sl@0
   116
      typedef color_traits<ColorValue> Color;
sl@0
   117
      typedef typename property_traits<DistanceMap>::value_type distance_type;
sl@0
   118
      
sl@0
   119
      astar_bfs_visitor(AStarHeuristic h, UniformCostVisitor vis,
sl@0
   120
                        UpdatableQueue& Q, PredecessorMap p,
sl@0
   121
                        CostMap c, DistanceMap d, WeightMap w,
sl@0
   122
                        ColorMap col, BinaryFunction combine,
sl@0
   123
                        BinaryPredicate compare, C zero)
sl@0
   124
        : m_h(h), m_vis(vis), m_Q(Q), m_predecessor(p), m_cost(c),
sl@0
   125
          m_distance(d), m_weight(w), m_color(col),
sl@0
   126
          m_combine(combine), m_compare(compare), m_zero(zero) {}
sl@0
   127
      
sl@0
   128
      
sl@0
   129
      template <class Vertex, class Graph>
sl@0
   130
      void initialize_vertex(Vertex u, Graph& g) {
sl@0
   131
        m_vis.initialize_vertex(u, g);
sl@0
   132
      }
sl@0
   133
      template <class Vertex, class Graph>
sl@0
   134
      void discover_vertex(Vertex u, Graph& g) {
sl@0
   135
        m_vis.discover_vertex(u, g);
sl@0
   136
      }
sl@0
   137
      template <class Vertex, class Graph>
sl@0
   138
      void examine_vertex(Vertex u, Graph& g) {
sl@0
   139
        m_vis.examine_vertex(u, g);
sl@0
   140
      }
sl@0
   141
      template <class Vertex, class Graph>
sl@0
   142
      void finish_vertex(Vertex u, Graph& g) {
sl@0
   143
        m_vis.finish_vertex(u, g);
sl@0
   144
      }
sl@0
   145
      template <class Edge, class Graph>
sl@0
   146
      void examine_edge(Edge e, Graph& g) { 
sl@0
   147
        if (m_compare(get(m_weight, e), m_zero))
sl@0
   148
          throw negative_edge();
sl@0
   149
        m_vis.examine_edge(e, g);
sl@0
   150
      }
sl@0
   151
      template <class Edge, class Graph>
sl@0
   152
      void non_tree_edge(Edge, Graph&) {}
sl@0
   153
      
sl@0
   154
      
sl@0
   155
      
sl@0
   156
      template <class Edge, class Graph>
sl@0
   157
      void tree_edge(Edge e, Graph& g) {
sl@0
   158
        m_decreased = relax(e, g, m_weight, m_predecessor, m_distance,
sl@0
   159
                            m_combine, m_compare);
sl@0
   160
sl@0
   161
        if(m_decreased) {
sl@0
   162
          m_vis.edge_relaxed(e, g);
sl@0
   163
          put(m_cost, target(e, g),
sl@0
   164
              m_combine(get(m_distance, target(e, g)),
sl@0
   165
                        m_h(target(e, g))));
sl@0
   166
        } else
sl@0
   167
          m_vis.edge_not_relaxed(e, g);
sl@0
   168
      }
sl@0
   169
      
sl@0
   170
      
sl@0
   171
      template <class Edge, class Graph>
sl@0
   172
      void gray_target(Edge e, Graph& g) {
sl@0
   173
        distance_type old_distance = get(m_distance, target(e, g));
sl@0
   174
sl@0
   175
        m_decreased = relax(e, g, m_weight, m_predecessor, m_distance,
sl@0
   176
                            m_combine, m_compare);
sl@0
   177
sl@0
   178
        /* On x86 Linux with optimization, we sometimes get into a
sl@0
   179
           horrible case where m_decreased is true but the distance hasn't
sl@0
   180
           actually changed. This occurs when the comparison inside
sl@0
   181
           relax() occurs with the 80-bit precision of the x87 floating
sl@0
   182
           point unit, but the difference is lost when the resulting
sl@0
   183
           values are written back to lower-precision memory (e.g., a
sl@0
   184
           double). With the eager Dijkstra's implementation, this results
sl@0
   185
           in looping. */
sl@0
   186
        if(m_decreased && old_distance != get(m_distance, target(e, g))) {
sl@0
   187
          put(m_cost, target(e, g),
sl@0
   188
              m_combine(get(m_distance, target(e, g)),
sl@0
   189
                        m_h(target(e, g))));
sl@0
   190
          m_Q.update(target(e, g));
sl@0
   191
          m_vis.edge_relaxed(e, g);
sl@0
   192
        } else
sl@0
   193
          m_vis.edge_not_relaxed(e, g);
sl@0
   194
      }
sl@0
   195
      
sl@0
   196
      
sl@0
   197
      template <class Edge, class Graph>
sl@0
   198
      void black_target(Edge e, Graph& g) {
sl@0
   199
        distance_type old_distance = get(m_distance, target(e, g));
sl@0
   200
sl@0
   201
        m_decreased = relax(e, g, m_weight, m_predecessor, m_distance,
sl@0
   202
                            m_combine, m_compare);
sl@0
   203
sl@0
   204
        /* See comment in gray_target */
sl@0
   205
        if(m_decreased && old_distance != get(m_distance, target(e, g))) {
sl@0
   206
          m_vis.edge_relaxed(e, g);
sl@0
   207
          put(m_cost, target(e, g),
sl@0
   208
              m_combine(get(m_distance, target(e, g)),
sl@0
   209
                        m_h(target(e, g))));
sl@0
   210
          m_Q.push(target(e, g));
sl@0
   211
          put(m_color, target(e, g), Color::gray());
sl@0
   212
          m_vis.black_target(e, g);
sl@0
   213
        } else
sl@0
   214
          m_vis.edge_not_relaxed(e, g);
sl@0
   215
      }
sl@0
   216
      
sl@0
   217
      
sl@0
   218
      
sl@0
   219
      AStarHeuristic m_h;
sl@0
   220
      UniformCostVisitor m_vis;
sl@0
   221
      UpdatableQueue& m_Q;
sl@0
   222
      PredecessorMap m_predecessor;
sl@0
   223
      CostMap m_cost;
sl@0
   224
      DistanceMap m_distance;
sl@0
   225
      WeightMap m_weight;
sl@0
   226
      ColorMap m_color;
sl@0
   227
      BinaryFunction m_combine;
sl@0
   228
      BinaryPredicate m_compare;
sl@0
   229
      bool m_decreased;
sl@0
   230
      C m_zero;
sl@0
   231
      
sl@0
   232
    };
sl@0
   233
    
sl@0
   234
  } // namespace detail
sl@0
   235
sl@0
   236
  
sl@0
   237
  
sl@0
   238
  template <typename VertexListGraph, typename AStarHeuristic,
sl@0
   239
            typename AStarVisitor, typename PredecessorMap,
sl@0
   240
            typename CostMap, typename DistanceMap,
sl@0
   241
            typename WeightMap, typename ColorMap,
sl@0
   242
            typename VertexIndexMap,
sl@0
   243
            typename CompareFunction, typename CombineFunction,
sl@0
   244
            typename CostInf, typename CostZero>
sl@0
   245
  inline void
sl@0
   246
  astar_search_no_init
sl@0
   247
    (VertexListGraph &g,
sl@0
   248
     typename graph_traits<VertexListGraph>::vertex_descriptor s,
sl@0
   249
     AStarHeuristic h, AStarVisitor vis,
sl@0
   250
     PredecessorMap predecessor, CostMap cost,
sl@0
   251
     DistanceMap distance, WeightMap weight,
sl@0
   252
     ColorMap color, VertexIndexMap index_map,
sl@0
   253
     CompareFunction compare, CombineFunction combine,
sl@0
   254
     CostInf inf, CostZero zero)
sl@0
   255
  {
sl@0
   256
    typedef indirect_cmp<CostMap, CompareFunction> IndirectCmp;
sl@0
   257
    IndirectCmp icmp(cost, compare);
sl@0
   258
  
sl@0
   259
    typedef typename graph_traits<VertexListGraph>::vertex_descriptor
sl@0
   260
      Vertex;
sl@0
   261
    typedef mutable_queue<Vertex, std::vector<Vertex>,
sl@0
   262
        IndirectCmp, VertexIndexMap>
sl@0
   263
      MutableQueue;
sl@0
   264
    MutableQueue Q(num_vertices(g), icmp, index_map);
sl@0
   265
  
sl@0
   266
    detail::astar_bfs_visitor<AStarHeuristic, AStarVisitor,
sl@0
   267
        MutableQueue, PredecessorMap, CostMap, DistanceMap,
sl@0
   268
        WeightMap, ColorMap, CombineFunction, CompareFunction>
sl@0
   269
      bfs_vis(h, vis, Q, predecessor, cost, distance, weight,
sl@0
   270
              color, combine, compare, zero);
sl@0
   271
  
sl@0
   272
    breadth_first_visit(g, s, Q, bfs_vis, color);
sl@0
   273
  }
sl@0
   274
  
sl@0
   275
  
sl@0
   276
  // Non-named parameter interface
sl@0
   277
  template <typename VertexListGraph, typename AStarHeuristic,
sl@0
   278
            typename AStarVisitor, typename PredecessorMap,
sl@0
   279
            typename CostMap, typename DistanceMap,
sl@0
   280
            typename WeightMap, typename VertexIndexMap,
sl@0
   281
            typename ColorMap,
sl@0
   282
            typename CompareFunction, typename CombineFunction,
sl@0
   283
            typename CostInf, typename CostZero>
sl@0
   284
  inline void
sl@0
   285
  astar_search
sl@0
   286
    (VertexListGraph &g,
sl@0
   287
     typename graph_traits<VertexListGraph>::vertex_descriptor s,
sl@0
   288
     AStarHeuristic h, AStarVisitor vis,
sl@0
   289
     PredecessorMap predecessor, CostMap cost,
sl@0
   290
     DistanceMap distance, WeightMap weight,
sl@0
   291
     VertexIndexMap index_map, ColorMap color,
sl@0
   292
     CompareFunction compare, CombineFunction combine,
sl@0
   293
     CostInf inf, CostZero zero)
sl@0
   294
  {
sl@0
   295
    
sl@0
   296
    typedef typename property_traits<ColorMap>::value_type ColorValue;
sl@0
   297
    typedef color_traits<ColorValue> Color;
sl@0
   298
    typename graph_traits<VertexListGraph>::vertex_iterator ui, ui_end;
sl@0
   299
    for (tie(ui, ui_end) = vertices(g); ui != ui_end; ++ui) {
sl@0
   300
      put(color, *ui, Color::white());
sl@0
   301
      put(distance, *ui, inf);
sl@0
   302
      put(cost, *ui, inf);
sl@0
   303
      put(predecessor, *ui, *ui);
sl@0
   304
      vis.initialize_vertex(*ui, g);
sl@0
   305
    }
sl@0
   306
    put(distance, s, zero);
sl@0
   307
    put(cost, s, h(s));
sl@0
   308
    
sl@0
   309
    astar_search_no_init
sl@0
   310
      (g, s, h, vis, predecessor, cost, distance, weight,
sl@0
   311
       color, index_map, compare, combine, inf, zero);
sl@0
   312
    
sl@0
   313
  }
sl@0
   314
  
sl@0
   315
  
sl@0
   316
  
sl@0
   317
  namespace detail {
sl@0
   318
    template <class VertexListGraph, class AStarHeuristic,
sl@0
   319
              class CostMap, class DistanceMap, class WeightMap,
sl@0
   320
              class IndexMap, class ColorMap, class Params>
sl@0
   321
    inline void
sl@0
   322
    astar_dispatch2
sl@0
   323
      (VertexListGraph& g,
sl@0
   324
       typename graph_traits<VertexListGraph>::vertex_descriptor s,
sl@0
   325
       AStarHeuristic h, CostMap cost, DistanceMap distance,
sl@0
   326
       WeightMap weight, IndexMap index_map, ColorMap color,
sl@0
   327
       const Params& params)
sl@0
   328
    {
sl@0
   329
      dummy_property_map p_map;
sl@0
   330
      typedef typename property_traits<CostMap>::value_type C;
sl@0
   331
      astar_search
sl@0
   332
        (g, s, h,
sl@0
   333
         choose_param(get_param(params, graph_visitor),
sl@0
   334
                      make_astar_visitor(null_visitor())),
sl@0
   335
         choose_param(get_param(params, vertex_predecessor), p_map),
sl@0
   336
         cost, distance, weight, index_map, color,
sl@0
   337
         choose_param(get_param(params, distance_compare_t()),
sl@0
   338
                      std::less<C>()),
sl@0
   339
         choose_param(get_param(params, distance_combine_t()),
sl@0
   340
                      closed_plus<C>()),
sl@0
   341
         choose_param(get_param(params, distance_inf_t()),
sl@0
   342
                      std::numeric_limits<C>::max BOOST_PREVENT_MACRO_SUBSTITUTION ()),
sl@0
   343
         choose_param(get_param(params, distance_zero_t()),
sl@0
   344
                      C()));
sl@0
   345
    }
sl@0
   346
  
sl@0
   347
    template <class VertexListGraph, class AStarHeuristic,
sl@0
   348
              class CostMap, class DistanceMap, class WeightMap,
sl@0
   349
              class IndexMap, class ColorMap, class Params>
sl@0
   350
    inline void
sl@0
   351
    astar_dispatch1
sl@0
   352
      (VertexListGraph& g,
sl@0
   353
       typename graph_traits<VertexListGraph>::vertex_descriptor s,
sl@0
   354
       AStarHeuristic h, CostMap cost, DistanceMap distance,
sl@0
   355
       WeightMap weight, IndexMap index_map, ColorMap color,
sl@0
   356
       const Params& params)
sl@0
   357
    {
sl@0
   358
      typedef typename property_traits<WeightMap>::value_type D;
sl@0
   359
      typename std::vector<D>::size_type
sl@0
   360
        n = is_default_param(distance) ? num_vertices(g) : 1;
sl@0
   361
      std::vector<D> distance_map(n);
sl@0
   362
      n = is_default_param(cost) ? num_vertices(g) : 1;
sl@0
   363
      std::vector<D> cost_map(n);
sl@0
   364
      std::vector<default_color_type> color_map(num_vertices(g));
sl@0
   365
      default_color_type c = white_color;
sl@0
   366
  
sl@0
   367
      detail::astar_dispatch2
sl@0
   368
        (g, s, h,
sl@0
   369
         choose_param(cost, make_iterator_property_map
sl@0
   370
                      (cost_map.begin(), index_map,
sl@0
   371
                       cost_map[0])),
sl@0
   372
         choose_param(distance, make_iterator_property_map
sl@0
   373
                      (distance_map.begin(), index_map, 
sl@0
   374
                       distance_map[0])),
sl@0
   375
         weight, index_map,
sl@0
   376
         choose_param(color, make_iterator_property_map
sl@0
   377
                      (color_map.begin(), index_map, c)),
sl@0
   378
         params);
sl@0
   379
    }
sl@0
   380
  } // namespace detail
sl@0
   381
  
sl@0
   382
  
sl@0
   383
  // Named parameter interface
sl@0
   384
  template <typename VertexListGraph,
sl@0
   385
            typename AStarHeuristic,
sl@0
   386
            typename P, typename T, typename R>
sl@0
   387
  void
sl@0
   388
  astar_search
sl@0
   389
    (VertexListGraph &g,
sl@0
   390
     typename graph_traits<VertexListGraph>::vertex_descriptor s,
sl@0
   391
     AStarHeuristic h, const bgl_named_params<P, T, R>& params)
sl@0
   392
  {
sl@0
   393
    
sl@0
   394
    detail::astar_dispatch1
sl@0
   395
      (g, s, h,
sl@0
   396
       get_param(params, vertex_rank),
sl@0
   397
       get_param(params, vertex_distance),
sl@0
   398
       choose_const_pmap(get_param(params, edge_weight), g, edge_weight),
sl@0
   399
       choose_const_pmap(get_param(params, vertex_index), g, vertex_index),
sl@0
   400
       get_param(params, vertex_color),
sl@0
   401
       params);
sl@0
   402
    
sl@0
   403
  }
sl@0
   404
  
sl@0
   405
} // namespace boost
sl@0
   406
sl@0
   407
#endif // BOOST_GRAPH_ASTAR_SEARCH_HPP