LEMUR Packages: ompl_lemur or_lemur pr_bgl prpy_lemur
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends Groups Pages
path_generator.h
Go to the documentation of this file.
1 
10 /* requires:
11 #include <map>
12 #include <vector>
13 #include <boost/graph/dijkstra_shortest_paths.hpp>
14 #include <boost/graph/reverse_graph.hpp>
15 #include <pr_bgl/compose_property_map.hpp>
16 #include <pr_bgl/rev_edge_map.h>
17 #include <pr_bgl/heap_indexed.h>
18 */
19 
20 namespace pr_bgl
21 {
22 
25 template <class Graph, class WeightMap>
27 {
28 private:
29  typedef typename boost::graph_traits<Graph>::vertex_descriptor Vertex;
30  typedef typename boost::graph_traits<Graph>::edge_descriptor Edge;
31  typedef typename boost::graph_traits<Graph>::out_edge_iterator EdgeOutIter;
32  typedef std::pair<double, double> Key; // (g+h, h), (path-sofar)
33 public:
34  typedef std::pair<double, std::vector<Vertex> > Path;
35 
36 private:
37  // inputs
38  const Graph & g;
39  const Vertex v_start;
40  const Vertex v_goal;
41  const WeightMap weight_map;
42 
44  std::map<Vertex,double> goaldist;
46  std::vector< Path > paths;
49 
51  Path next_path_buffer;
52 
53 public:
54  path_generator(const Graph & g, Vertex v_start, Vertex v_goal, WeightMap weight_map):
55  g(g), v_start(v_start), v_goal(v_goal), weight_map(weight_map)
56  {
57  // find optimal actual path cost
58  // run reversed dijkstra's to get goaldist (and preds) from w
59  printf("running w_est reverse dijstra's ...\n");
60  std::map<Vertex,Vertex> preds;
61  boost::reverse_graph<Graph> rg(g);
62  boost::dijkstra_shortest_paths(
63  rg,
64  v_goal, // source (actually dest of non-reversed graph)
65  boost::make_assoc_property_map(preds),
66  boost::make_assoc_property_map(goaldist),
67  pr_bgl::make_compose_property_map(
68  weight_map,
70  boost::get(boost::vertex_index, g),
71  std::less<double>(), // compare
72  boost::closed_plus<double>(std::numeric_limits<double>::max()), // combine
73  std::numeric_limits<double>::max(),
74  double(),
75  boost::make_dijkstra_visitor(boost::null_visitor())
76  );
77  // start with just the start vertex
78  paths.push_back(Path(0.0, std::vector<Vertex>(1,v_start)));
79  queue.insert(0, Key(goaldist[v_start],goaldist[v_start]));
80  // keep buffer
81  next_path_buffer = generate_path();
82  }
83 
84  bool peek_next_exists()
85  {
86  return (0 < next_path_buffer.second.size());
87  }
88 
89  double peek_length()
90  {
91  return next_path_buffer.first;
92  }
93 
94  Path next_path()
95  {
96  Path ret = next_path_buffer;
97  next_path_buffer = generate_path();
98  return ret;
99  }
100 
101 private:
103  Path generate_path()
104  {
105  unsigned int ui;
106  while (queue.size())
107  {
108  // remove top element from queue
109  size_t top_idx = queue.top_idx();
110  queue.remove_min();
111  Path path = paths[top_idx];
112  // is it a full path to the goal?
113  if (path.second.back() == v_goal)
114  return path;
115  // iterate over all successors of the last vertex
116  std::pair<EdgeOutIter, EdgeOutIter> out_edges
117  = boost::out_edges(path.second.back(), g);
118  for (; out_edges.first!=out_edges.second; out_edges.first++)
119  {
120  Vertex v_successor = boost::target(*out_edges.first, g);
121  // ignore non-simple paths
122  for (ui=0; ui<path.second.size(); ui++)
123  if (path.second[ui] == v_successor)
124  break;
125  if (ui<path.second.size())
126  continue;
127  // add new path to paths
128  Path path_new = path;
129  Edge e = *out_edges.first;
130  path_new.first += boost::get(weight_map, e);
131  path_new.second.push_back(v_successor);
132  paths.push_back(path_new);
133  queue.insert(paths.size()-1,
134  Key(path_new.first+goaldist[v_successor],goaldist[v_successor]));
135  }
136  }
137  // no path!
138  return Path(std::numeric_limits<double>::max(), std::vector<Vertex>());
139  }
140 };
141 
142 } // namespace pr_bgl
Readable boost property map which maps from reversed edges to original edges.
Definition: rev_edge_map.h:20
Generates all simple paths in non-decreasing order of length.
Definition: path_generator.h:26