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 <utility>
45 #include <vector>
46 #include <map>
47 
48 namespace ompl
49 {
50  namespace base
51  {
52  // Forward declare for use in implementation
53  OMPL_CLASS_FORWARD(OptimizationObjective);
54  }
55 
56  namespace geometric
57  {
73  class LazyPRM : public base::Planner
74  {
75  public:
76  struct vertex_state_t
77  {
78  using kind = boost::vertex_property_tag;
79  };
80 
81  struct vertex_flags_t
82  {
83  using kind = boost::vertex_property_tag;
84  };
85 
86  struct vertex_component_t
87  {
88  using kind = boost::vertex_property_tag;
89  };
90 
91  struct edge_flags_t
92  {
93  using kind = boost::edge_property_tag;
94  };
95 
97  using Vertex =
98  boost::adjacency_list_traits<boost::vecS, boost::listS, boost::undirectedS>::vertex_descriptor;
99 
116  using Graph = boost::adjacency_list<
117  boost::vecS, boost::listS, boost::undirectedS,
118  boost::property<
120  boost::property<
121  boost::vertex_index_t, unsigned long int,
122  boost::property<vertex_flags_t, unsigned int,
123  boost::property<vertex_component_t, unsigned long int,
124  boost::property<boost::vertex_predecessor_t, Vertex,
125  boost::property<boost::vertex_rank_t,
126  unsigned long int>>>>>>,
127  boost::property<boost::edge_weight_t, base::Cost, boost::property<edge_flags_t, unsigned int>>>;
128 
130  using Edge = boost::graph_traits<Graph>::edge_descriptor;
131 
133  using RoadmapNeighbors = std::shared_ptr<NearestNeighbors<Vertex> >;
134 
137  using ConnectionStrategy = std::function<const std::vector<Vertex> &(const Vertex)>;
138 
144  using ConnectionFilter = std::function<bool (const Vertex &, const Vertex &)>;
145 
147  LazyPRM(const base::SpaceInformationPtr &si, bool starStrategy = false);
148 
149  ~LazyPRM() override;
150 
152  void setRange(double distance);
153 
155  double getRange() const
156  {
157  return maxDistance_;
158  }
159 
161  template <template <typename T> class NN>
162  void setNearestNeighbors()
163  {
164  if (nn_ && nn_->size() == 0)
165  OMPL_WARN("Calling setNearestNeighbors will clear all states.");
166  clear();
167  nn_ = std::make_shared<NN<Vertex>>();
170  if (isSetup())
171  setup();
172  }
173 
174  void setProblemDefinition(const base::ProblemDefinitionPtr &pdef) override;
175 
189  void setConnectionStrategy(const ConnectionStrategy &connectionStrategy)
190  {
191  connectionStrategy_ = connectionStrategy;
193  }
196 
200  void setMaxNearestNeighbors(unsigned int k);
201 
215  void setConnectionFilter(const ConnectionFilter &connectionFilter)
216  {
217  connectionFilter_ = connectionFilter;
218  }
219 
221  unsigned long int milestoneCount() const
222  {
223  return boost::num_vertices(g_);
224  }
225 
227  unsigned long int edgeCount() const
228  {
229  return boost::num_edges(g_);
230  }
231 
232  void getPlannerData(base::PlannerData &data) const override;
233 
234  void setup() override;
235 
236  void clear() override;
237 
242  void clearQuery() override;
243 
245 
246  protected:
248  static const unsigned int VALIDITY_UNKNOWN = 0;
249 
251  static const unsigned int VALIDITY_TRUE = 1;
252 
254  // Planner progress property functions
255  std::string getIterationCount() const
256  {
257  return std::to_string(iterations_);
258  }
259  std::string getBestCost() const
260  {
261  return std::to_string(bestCost_.value());
262  }
263  std::string getMilestoneCountString() const
264  {
265  return std::to_string(milestoneCount());
266  }
267  std::string getEdgeCountString() const
268  {
269  return std::to_string(edgeCount());
270  }
271 
273  void freeMemory();
274 
279 
280  void uniteComponents(Vertex a, Vertex b);
281 
282  void markComponent(Vertex v, unsigned long int newComponent);
283 
286  long int solutionComponent(std::pair<std::size_t, std::size_t> *startGoalPair) const;
287 
290  ompl::base::PathPtr constructSolution(const Vertex &start, const Vertex &goal);
291 
294  double distanceFunction(const Vertex a, const Vertex b) const
295  {
296  return si_->distance(stateProperty_[a], stateProperty_[b]);
297  }
298 
301  base::Cost costHeuristic(Vertex u, Vertex v) const;
302 
304  bool starStrategy_;
305 
308 
311 
314  bool userSetConnectionStrategy_{false};
315 
317  double maxDistance_{0.};
318 
320  base::StateSamplerPtr sampler_;
321 
324 
326  Graph g_;
327 
329  std::vector<Vertex> startM_;
330 
332  std::vector<Vertex> goalM_;
333 
335  boost::property_map<Graph, boost::vertex_index_t>::type indexProperty_;
336 
338  boost::property_map<Graph, vertex_state_t>::type stateProperty_;
339 
341  boost::property_map<Graph, boost::edge_weight_t>::type weightProperty_;
342 
344  boost::property_map<Graph, vertex_component_t>::type vertexComponentProperty_;
345 
347  boost::property_map<Graph, vertex_flags_t>::type vertexValidityProperty_;
348 
350  boost::property_map<Graph, edge_flags_t>::type edgeValidityProperty_;
351 
354  unsigned long int componentCount_{0};
355 
357  std::map<unsigned long int, unsigned long int> componentSize_;
358 
360  base::OptimizationObjectivePtr opt_;
361 
362  base::Cost bestCost_{std::numeric_limits<double>::quiet_NaN()};
363 
364  unsigned long int iterations_{0};
365  };
366  }
367 }
368 
369 #endif
void setDefaultConnectionStrategy()
Definition: LazyPRM.cpp:180
std::shared_ptr< NearestNeighbors< Vertex > > RoadmapNeighbors
A nearest neighbors data structure for roadmap vertices.
Definition: LazyPRM.h:163
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:384
double getRange() const
Get the range the planner is using.
Definition: LazyPRM.h:185
void clear() override
Clear all internal datastructures. Planner settings are not affected. Subsequent calls to solve() wil...
Definition: LazyPRM.cpp:201
A shared pointer wrapper for ompl::base::Path.
std::map< unsigned long int, unsigned long int > componentSize_
The number of elements in each component in the LazyPRM roadmap.
Definition: LazyPRM.h:387
static const unsigned int VALIDITY_UNKNOWN
Flag indicating validity of an edge of a vertex.
Definition: LazyPRM.h:278
Vertex addMilestone(base::State *state)
Construct a milestone for a given state (state), store it in the nearest neighbors data structure and...
Definition: LazyPRM.cpp:221
void setMaxNearestNeighbors(unsigned int k)
Convenience function that sets the connection strategy to the default one with k nearest neighbors.
Definition: LazyPRM.cpp:162
boost::property_map< Graph, vertex_component_t >::type vertexComponentProperty_
Access the connected component of a vertex.
Definition: LazyPRM.h:374
void setup() override
Perform extra configuration steps, if needed. This call will also issue a call to ompl::base::SpaceIn...
Definition: LazyPRM.cpp:106
void setConnectionFilter(const ConnectionFilter &connectionFilter)
Set the function that can reject a milestone connection.
Definition: LazyPRM.h:245
Definition of an abstract state.
Definition: State.h:110
LazyPRM(const base::SpaceInformationPtr &si, bool starStrategy=false)
Constructor.
Definition: LazyPRM.cpp:67
unsigned long int edgeCount() const
Return the number of edges currently in the graph.
Definition: LazyPRM.h:257
boost::property_map< Graph, vertex_state_t >::type stateProperty_
Access to the internal base::state at each Vertex.
Definition: LazyPRM.h:368
void setNearestNeighbors()
Set a different nearest neighbors datastructure.
Definition: LazyPRM.h:192
void setRange(double distance)
Set the maximum length of a motion to be added to the roadmap.
Definition: LazyPRM.cpp:153
base::PlannerStatus solve(const base::PlannerTerminationCondition &ptc) override
Function that can solve the motion planning problem. This function can be called multiple times on th...
Definition: LazyPRM.cpp:247
boost::property_map< Graph, boost::edge_weight_t >::type weightProperty_
Access to the weights of each Edge.
Definition: LazyPRM.h:371
double value() const
The value of the cost.
Definition: Cost.h:148
base::OptimizationObjectivePtr opt_
Objective cost function for PRM graph edges.
Definition: LazyPRM.h:390
Definition of a cost value. Can represent the cost of a motion or the cost of a state.
Definition: Cost.h:108
boost::graph_traits< Graph >::edge_descriptor Edge
The type for an edge in the roadmap.
Definition: LazyPRM.h:160
std::vector< Vertex > goalM_
Array of goal milestones.
Definition: LazyPRM.h:362
Object containing planner generated vertex and edge data. It is assumed that all vertices are unique,...
Definition: PlannerData.h:235
boost::property_map< Graph, edge_flags_t >::type edgeValidityProperty_
Access the validity state of an edge.
Definition: LazyPRM.h:380
double maxDistance_
The maximum length of a motion to be added to a tree.
Definition: LazyPRM.h:347
Encapsulate a termination condition for a motion planner. Planners will call operator() to decide whe...
std::function< bool(const Vertex &, const Vertex &)> ConnectionFilter
A function that can reject connections.
Definition: LazyPRM.h:174
Lazy Probabilistic RoadMap planner.
Definition: LazyPRM.h:103
A class to store the exit status of Planner::solve()
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:157
void setConnectionStrategy(const ConnectionStrategy &connectionStrategy)
Set the connection strategy function that specifies the milestones that connection attempts will be m...
Definition: LazyPRM.h:219
static const unsigned int VALIDITY_TRUE
Flag indicating validity of an edge of a vertex.
Definition: LazyPRM.h:281
#define OMPL_WARN(fmt,...)
Log a formatted warning string.
Definition: Console.h:65
long int solutionComponent(std::pair< std::size_t, std::size_t > *startGoalPair) const
Check if any pair of a start state and goal state are part of the same connected component....
Definition: LazyPRM.cpp:403
boost::property_map< Graph, boost::vertex_index_t >::type indexProperty_
Access to the internal base::state at each Vertex.
Definition: LazyPRM.h:365
Graph g_
Connectivity graph.
Definition: LazyPRM.h:356
void freeMemory()
Free all the memory allocated by the planner.
Definition: LazyPRM.cpp:214
boost::property_map< Graph, vertex_flags_t >::type vertexValidityProperty_
Access the validity state of a vertex.
Definition: LazyPRM.h:377
std::vector< Vertex > startM_
Array of start milestones.
Definition: LazyPRM.h:359
bool isSetup() const
Check if setup() was called for this planner.
Definition: Planner.cpp:112
std::function< const std::vector< Vertex > &(const Vertex)> ConnectionStrategy
A function returning the milestones that should be attempted to connect to.
Definition: LazyPRM.h:167
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:324
SpaceInformationPtr si_
The space information for which planning is done.
Definition: Planner.h:479
RoadmapNeighbors nn_
Nearest neighbors data structure.
Definition: LazyPRM.h:353
bool userSetConnectionStrategy_
Flag indicating whether the employed connection strategy was set by the user (or defaults are assumed...
Definition: LazyPRM.h:344
ConnectionStrategy connectionStrategy_
Function that returns the milestones to attempt connections with.
Definition: LazyPRM.h:337
void clearQuery() override
Clear the query previously loaded from the ProblemDefinition. Subsequent calls to solve() will reuse ...
Definition: LazyPRM.cpp:194
ompl::base::PathPtr constructSolution(const Vertex &start, const Vertex &goal)
Given two milestones from the same connected component, construct a path connecting them and set it a...
Definition: LazyPRM.cpp:421
boost::adjacency_list_traits< boost::vecS, boost::listS, boost::undirectedS >::vertex_descriptor Vertex
The type for a vertex in the roadmap.
Definition: LazyPRM.h:128
unsigned long int milestoneCount() const
Return the number of milestones currently in the graph.
Definition: LazyPRM.h:251
ConnectionFilter connectionFilter_
Function that can reject a milestone connection.
Definition: LazyPRM.h:340
base::StateSamplerPtr sampler_
Sampler user for generating random in the state space.
Definition: LazyPRM.h:350
void getPlannerData(base::PlannerData &data) const override
Get information about the current run of the motion planner. Repeated calls to this function will upd...
Definition: LazyPRM.cpp:553
base::Cost costHeuristic(Vertex u, Vertex v) const
Given two vertices, returns a heuristic on the cost of the path connecting them. This method wraps Op...
Definition: LazyPRM.cpp:548
Main namespace. Contains everything in this library.
Definition: AppBase.h:20
bool starStrategy_
Flag indicating whether the default connection strategy is the Star strategy.
Definition: LazyPRM.h:334