LazyPRM.h
1 /*********************************************************************
2 * Software License Agreement (BSD License)
3 *
4 * Copyright (c) 2013, Rice University
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
17 * * Neither the name of the Rice University nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
33 *********************************************************************/
34 
35 /* Author: Ioan Sucan */
36 
37 #ifndef OMPL_GEOMETRIC_PLANNERS_PRM_LAZY_PRM_
38 #define OMPL_GEOMETRIC_PLANNERS_PRM_LAZY_PRM_
39 
40 #include "ompl/geometric/planners/PlannerIncludes.h"
41 #include "ompl/datastructures/NearestNeighbors.h"
42 #include <boost/graph/graph_traits.hpp>
43 #include <boost/graph/adjacency_list.hpp>
44 #include <boost/function.hpp>
45 #include <utility>
46 #include <vector>
47 #include <map>
48 
49 namespace ompl
50 {
51  namespace base
52  {
53  // Forward declare for use in implementation
54  OMPL_CLASS_FORWARD(OptimizationObjective);
55  }
56 
57  namespace geometric
58  {
59 
75  class LazyPRM : public base::Planner
76  {
77  public:
78  struct vertex_state_t {
79  typedef boost::vertex_property_tag kind;
80  };
81 
82  struct vertex_flags_t {
83  typedef boost::vertex_property_tag kind;
84  };
85 
87  typedef boost::vertex_property_tag kind;
88  };
89 
90  struct edge_flags_t {
91  typedef boost::edge_property_tag kind;
92  };
93 
95  typedef boost::adjacency_list_traits<boost::vecS, boost::listS,
96  boost::undirectedS>::vertex_descriptor Vertex;
97 
114  typedef boost::adjacency_list <
115  boost::vecS, boost::listS, boost::undirectedS,
116  boost::property < vertex_state_t, base::State*,
117  boost::property < boost::vertex_index_t, unsigned long int,
118  boost::property < vertex_flags_t, unsigned int,
119  boost::property < vertex_component_t, unsigned long int,
120  boost::property < boost::vertex_predecessor_t, Vertex,
121  boost::property < boost::vertex_rank_t, unsigned long int > > > > > >,
122  boost::property < boost::edge_weight_t, base::Cost,
123  boost::property < edge_flags_t, unsigned int > >
124  > Graph;
125 
127  typedef boost::graph_traits<Graph>::edge_descriptor Edge;
128 
130  typedef boost::shared_ptr< NearestNeighbors<Vertex> > RoadmapNeighbors;
131 
134  typedef boost::function<const std::vector<Vertex>&(const Vertex)> ConnectionStrategy;
135 
141  typedef boost::function<bool(const Vertex&, const Vertex&)> ConnectionFilter;
142 
144  LazyPRM(const base::SpaceInformationPtr &si, bool starStrategy = false);
145 
146  virtual ~LazyPRM();
147 
149  void setRange(double distance);
150 
152  double getRange() const
153  {
154  return maxDistance_;
155  }
156 
158  template<template<typename T> class NN>
160  {
161  nn_.reset(new NN<Vertex>());
162  if (!userSetConnectionStrategy_)
163  connectionStrategy_.clear();
164  if (isSetup())
165  setup();
166  }
167 
168  virtual void setProblemDefinition(const base::ProblemDefinitionPtr &pdef);
169 
183  void setConnectionStrategy(const ConnectionStrategy &connectionStrategy)
184  {
185  connectionStrategy_ = connectionStrategy;
186  userSetConnectionStrategy_ = true;
187  }
188 
192  void setMaxNearestNeighbors(unsigned int k);
193 
207  void setConnectionFilter(const ConnectionFilter &connectionFilter)
208  {
209  connectionFilter_ = connectionFilter;
210  }
211 
213  unsigned long int milestoneCount() const
214  {
215  return boost::num_vertices(g_);
216  }
217 
219  unsigned long int edgeCount() const
220  {
221  return boost::num_edges(g_);
222  }
223 
224  virtual void getPlannerData(base::PlannerData &data) const;
225 
226  virtual void setup();
227 
228  virtual void clear();
229 
234  void clearQuery();
235 
236  virtual base::PlannerStatus solve(const base::PlannerTerminationCondition &ptc);
237 
238  protected:
239 
241  static const unsigned int VALIDITY_UNKNOWN = 0;
242 
244  static const unsigned int VALIDITY_TRUE = 1;
245 
247  // Planner progress property functions
248  std::string getIterationCount() const
249  {
250  return boost::lexical_cast<std::string>(iterations_);
251  }
252  std::string getBestCost() const
253  {
254  return boost::lexical_cast<std::string>(bestCost_);
255  }
256  std::string getMilestoneCountString() const
257  {
258  return boost::lexical_cast<std::string>(milestoneCount());
259  }
260  std::string getEdgeCountString() const
261  {
262  return boost::lexical_cast<std::string>(edgeCount());
263  }
264 
266  void freeMemory();
267 
270  Vertex addMilestone(base::State *state);
271 
272  void uniteComponents(Vertex a, Vertex b);
273 
274  void markComponent(Vertex v, unsigned long int newComponent);
275 
278  long int solutionComponent(std::pair<std::size_t, std::size_t> *startGoalPair) const;
279 
281  ompl::base::PathPtr constructSolution(const Vertex &start, const Vertex &goal);
282 
284  double distanceFunction(const Vertex a, const Vertex b) const
285  {
286  return si_->distance(stateProperty_[a], stateProperty_[b]);
287  }
288 
291  base::Cost costHeuristic(Vertex u, Vertex v) const;
292 
295 
298 
300  ConnectionFilter connectionFilter_;
301 
304 
306  double maxDistance_;
307 
310 
312  RoadmapNeighbors nn_;
313 
316 
318  std::vector<Vertex> startM_;
319 
321  std::vector<Vertex> goalM_;
322 
324  boost::property_map<Graph, boost::vertex_index_t>::type indexProperty_;
325 
327  boost::property_map<Graph, vertex_state_t>::type stateProperty_;
328 
330  boost::property_map<Graph, boost::edge_weight_t>::type weightProperty_;
331 
333  boost::property_map<Graph, vertex_component_t>::type vertexComponentProperty_;
334 
336  boost::property_map<Graph, vertex_flags_t>::type vertexValidityProperty_;
337 
339  boost::property_map<Graph, edge_flags_t>::type edgeValidityProperty_;
340 
343  unsigned long int componentCount_;
344 
346  std::map<unsigned long int, unsigned long int> componentSize_;
347 
350 
351  base::Cost bestCost_;
352 
353  unsigned long int iterations_;
354  };
355 
356  }
357 }
358 
359 #endif
bool starStrategy_
Flag indicating whether the default connection strategy is the Star strategy.
Definition: LazyPRM.h:294
Object containing planner generated vertex and edge data. It is assumed that all vertices are unique...
Definition: PlannerData.h:164
boost::property_map< Graph, vertex_state_t >::type stateProperty_
Access to the internal base::state at each Vertex.
Definition: LazyPRM.h:327
A boost shared pointer wrapper for ompl::base::ProblemDefinition.
base::OptimizationObjectivePtr opt_
Objective cost function for PRM graph edges.
Definition: LazyPRM.h:349
boost::property_map< Graph, boost::vertex_index_t >::type indexProperty_
Access to the internal base::state at each Vertex.
Definition: LazyPRM.h:324
boost::shared_ptr< NearestNeighbors< Vertex > > RoadmapNeighbors
A nearest neighbors data structure for roadmap vertices.
Definition: LazyPRM.h:130
A boost shared pointer wrapper for ompl::base::StateSampler.
double maxDistance_
The maximum length of a motion to be added to a tree.
Definition: LazyPRM.h:306
Graph g_
Connectivity graph.
Definition: LazyPRM.h:315
boost::property_map< Graph, vertex_component_t >::type vertexComponentProperty_
Access the connected component of a vertex.
Definition: LazyPRM.h:333
ConnectionStrategy connectionStrategy_
Function that returns the milestones to attempt connections with.
Definition: LazyPRM.h:297
Encapsulate a termination condition for a motion planner. Planners will call operator() to decide whe...
boost::adjacency_list< boost::vecS, boost::listS, boost::undirectedS, boost::property< vertex_state_t, base::State *, boost::property< boost::vertex_index_t, unsigned long int, boost::property< vertex_flags_t, unsigned int, boost::property< vertex_component_t, unsigned long int, boost::property< boost::vertex_predecessor_t, Vertex, boost::property< boost::vertex_rank_t, unsigned long int > > > > > >, boost::property< boost::edge_weight_t, base::Cost, boost::property< edge_flags_t, unsigned int > > > Graph
The underlying roadmap graph.
Definition: LazyPRM.h:124
boost::property_map< Graph, boost::edge_weight_t >::type weightProperty_
Access to the weights of each Edge.
Definition: LazyPRM.h:330
Lazy Probabilistic RoadMap planner.
Definition: LazyPRM.h:75
RoadmapNeighbors nn_
Nearest neighbors data structure.
Definition: LazyPRM.h:312
boost::function< const std::vector< Vertex > &(const Vertex)> ConnectionStrategy
A function returning the milestones that should be attempted to connect to.
Definition: LazyPRM.h:134
ConnectionFilter connectionFilter_
Function that can reject a milestone connection.
Definition: LazyPRM.h:300
Main namespace. Contains everything in this library.
Definition: Cost.h:42
boost::function< bool(const Vertex &, const Vertex &)> ConnectionFilter
A function that can reject connections.
Definition: LazyPRM.h:141
boost::property_map< Graph, vertex_flags_t >::type vertexValidityProperty_
Access the validity state of a vertex.
Definition: LazyPRM.h:336
Base class for a planner.
Definition: Planner.h:232
std::vector< Vertex > startM_
Array of start milestones.
Definition: LazyPRM.h:318
double getRange() const
Get the range the planner is using.
Definition: LazyPRM.h:152
void setConnectionFilter(const ConnectionFilter &connectionFilter)
Set the function that can reject a milestone connection.
Definition: LazyPRM.h:207
boost::adjacency_list_traits< boost::vecS, boost::listS, boost::undirectedS >::vertex_descriptor Vertex
The type for a vertex in the roadmap.
Definition: LazyPRM.h:96
unsigned long int componentCount_
Number of connected components created so far. This is used as an ID only, does not represent the act...
Definition: LazyPRM.h:343
unsigned long int edgeCount() const
Return the number of edges currently in the graph.
Definition: LazyPRM.h:219
A class to store the exit status of Planner::solve()
Definition: PlannerStatus.h:48
A boost shared pointer wrapper for ompl::base::SpaceInformation.
void setConnectionStrategy(const ConnectionStrategy &connectionStrategy)
Set the connection strategy function that specifies the milestones that connection attempts will be m...
Definition: LazyPRM.h:183
Definition of an abstract state.
Definition: State.h:50
unsigned long int milestoneCount() const
Return the number of milestones currently in the graph.
Definition: LazyPRM.h:213
void setNearestNeighbors()
Set a different nearest neighbors datastructure.
Definition: LazyPRM.h:159
double distanceFunction(const Vertex a, const Vertex b) const
Compute distance between two milestones (this is simply distance between the states of the milestones...
Definition: LazyPRM.h:284
A boost shared pointer wrapper for ompl::base::OptimizationObjective.
boost::graph_traits< Graph >::edge_descriptor Edge
The type for an edge in the roadmap.
Definition: LazyPRM.h:127
std::vector< Vertex > goalM_
Array of goal milestones.
Definition: LazyPRM.h:321
bool userSetConnectionStrategy_
Flag indicating whether the employed connection strategy was set by the user (or defaults are assumed...
Definition: LazyPRM.h:303
boost::property_map< Graph, edge_flags_t >::type edgeValidityProperty_
Access the validity state of an edge.
Definition: LazyPRM.h:339
Definition of a cost value. Can represent the cost of a motion or the cost of a state.
Definition: Cost.h:47
std::map< unsigned long int, unsigned long int > componentSize_
The number of elements in each component in the LazyPRM roadmap.
Definition: LazyPRM.h:346
A boost shared pointer wrapper for ompl::base::Path.
base::StateSamplerPtr sampler_
Sampler user for generating random in the state space.
Definition: LazyPRM.h:309