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:
77  {
78  typedef boost::vertex_property_tag kind;
79  };
80 
82  {
83  typedef boost::vertex_property_tag kind;
84  };
85 
87  {
88  typedef boost::vertex_property_tag kind;
89  };
90 
91  struct edge_flags_t
92  {
93  typedef boost::edge_property_tag kind;
94  };
95 
97  typedef boost::adjacency_list_traits<boost::vecS, boost::listS, boost::undirectedS>::vertex_descriptor
99 
116  typedef 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>>>
129 
131  typedef boost::graph_traits<Graph>::edge_descriptor Edge;
132 
134  typedef std::shared_ptr<NearestNeighbors<Vertex>> RoadmapNeighbors;
135 
138  typedef std::function<const std::vector<Vertex> &(const Vertex)> ConnectionStrategy;
139 
145  typedef std::function<bool(const Vertex &, const Vertex &)> ConnectionFilter;
146 
148  LazyPRM(const base::SpaceInformationPtr &si, bool starStrategy = false);
149 
150  ~LazyPRM() override;
151 
153  void setRange(double distance);
154 
156  double getRange() const
157  {
158  return maxDistance_;
159  }
160 
162  template <template <typename T> class NN>
164  {
165  if (nn_ && nn_->size() == 0)
166  OMPL_WARN("Calling setNearestNeighbors will clear all states.");
167  clear();
168  nn_ = std::make_shared<NN<Vertex>>();
169  if (!userSetConnectionStrategy_)
170  connectionStrategy_ = ConnectionStrategy();
171  if (isSetup())
172  setup();
173  }
174 
175  void setProblemDefinition(const base::ProblemDefinitionPtr &pdef) override;
176 
190  void setConnectionStrategy(const ConnectionStrategy &connectionStrategy)
191  {
192  connectionStrategy_ = connectionStrategy;
193  userSetConnectionStrategy_ = true;
194  }
195 
199  void setMaxNearestNeighbors(unsigned int k);
200 
214  void setConnectionFilter(const ConnectionFilter &connectionFilter)
215  {
216  connectionFilter_ = connectionFilter;
217  }
218 
220  unsigned long int milestoneCount() const
221  {
222  return boost::num_vertices(g_);
223  }
224 
226  unsigned long int edgeCount() const
227  {
228  return boost::num_edges(g_);
229  }
230 
231  void getPlannerData(base::PlannerData &data) const override;
232 
233  void setup() override;
234 
235  void clear() override;
236 
241  void clearQuery();
242 
243  base::PlannerStatus solve(const base::PlannerTerminationCondition &ptc) override;
244 
245  protected:
247  static const unsigned int VALIDITY_UNKNOWN = 0;
248 
250  static const unsigned int VALIDITY_TRUE = 1;
251 
253  // Planner progress property functions
254  std::string getIterationCount() const
255  {
256  return std::to_string(iterations_);
257  }
258  std::string getBestCost() const
259  {
260  return boost::lexical_cast<std::string>(bestCost_);
261  }
262  std::string getMilestoneCountString() const
263  {
264  return std::to_string(milestoneCount());
265  }
266  std::string getEdgeCountString() const
267  {
268  return std::to_string(edgeCount());
269  }
270 
272  void freeMemory();
273 
277  Vertex addMilestone(base::State *state);
278 
279  void uniteComponents(Vertex a, Vertex b);
280 
281  void markComponent(Vertex v, unsigned long int newComponent);
282 
285  long int solutionComponent(std::pair<std::size_t, std::size_t> *startGoalPair) const;
286 
289  ompl::base::PathPtr constructSolution(const Vertex &start, const Vertex &goal);
290 
293  double distanceFunction(const Vertex a, const Vertex b) const
294  {
295  return si_->distance(stateProperty_[a], stateProperty_[b]);
296  }
297 
300  base::Cost costHeuristic(Vertex u, Vertex v) const;
301 
304 
307 
309  ConnectionFilter connectionFilter_;
310 
313  bool userSetConnectionStrategy_{false};
314 
316  double maxDistance_{0.};
317 
320 
322  RoadmapNeighbors nn_;
323 
326 
328  std::vector<Vertex> startM_;
329 
331  std::vector<Vertex> goalM_;
332 
334  boost::property_map<Graph, boost::vertex_index_t>::type indexProperty_;
335 
337  boost::property_map<Graph, vertex_state_t>::type stateProperty_;
338 
340  boost::property_map<Graph, boost::edge_weight_t>::type weightProperty_;
341 
343  boost::property_map<Graph, vertex_component_t>::type vertexComponentProperty_;
344 
346  boost::property_map<Graph, vertex_flags_t>::type vertexValidityProperty_;
347 
349  boost::property_map<Graph, edge_flags_t>::type edgeValidityProperty_;
350 
353  unsigned long int componentCount_{0};
354 
356  std::map<unsigned long int, unsigned long int> componentSize_;
357 
360 
361  base::Cost bestCost_{std::numeric_limits<double>::quiet_NaN()};
362 
363  unsigned long int iterations_{0};
364  };
365  }
366 }
367 
368 #endif
bool starStrategy_
Flag indicating whether the default connection strategy is the Star strategy.
Definition: LazyPRM.h:303
Object containing planner generated vertex and edge data. It is assumed that all vertices are unique...
Definition: PlannerData.h:174
boost::property_map< Graph, vertex_state_t >::type stateProperty_
Access to the internal base::state at each Vertex.
Definition: LazyPRM.h:337
A shared pointer wrapper for ompl::base::ProblemDefinition.
base::OptimizationObjectivePtr opt_
Objective cost function for PRM graph edges.
Definition: LazyPRM.h:359
boost::property_map< Graph, boost::vertex_index_t >::type indexProperty_
Access to the internal base::state at each Vertex.
Definition: LazyPRM.h:334
A shared pointer wrapper for ompl::base::StateSampler.
Graph g_
Connectivity graph.
Definition: LazyPRM.h:325
boost::property_map< Graph, vertex_component_t >::type vertexComponentProperty_
Access the connected component of a vertex.
Definition: LazyPRM.h:343
ConnectionStrategy connectionStrategy_
Function that returns the milestones to attempt connections with.
Definition: LazyPRM.h:306
Encapsulate a termination condition for a motion planner. Planners will call operator() to decide whe...
boost::property_map< Graph, boost::edge_weight_t >::type weightProperty_
Access to the weights of each Edge.
Definition: LazyPRM.h:340
Lazy Probabilistic RoadMap planner.
Definition: LazyPRM.h:73
RoadmapNeighbors nn_
Nearest neighbors data structure.
Definition: LazyPRM.h:322
ConnectionFilter connectionFilter_
Function that can reject a milestone connection.
Definition: LazyPRM.h:309
double getRange() const
Get the range the planner is using.
Definition: LazyPRM.h:156
Main namespace. Contains everything in this library.
Definition: AppBase.h:21
boost::property_map< Graph, vertex_flags_t >::type vertexValidityProperty_
Access the validity state of a vertex.
Definition: LazyPRM.h:346
Base class for a planner.
Definition: Planner.h:223
std::vector< Vertex > startM_
Array of start milestones.
Definition: LazyPRM.h:328
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:293
unsigned long int edgeCount() const
Return the number of edges currently in the graph.
Definition: LazyPRM.h:226
void setConnectionFilter(const ConnectionFilter &connectionFilter)
Set the function that can reject a milestone connection.
Definition: LazyPRM.h:214
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:128
boost::adjacency_list_traits< boost::vecS, boost::listS, boost::undirectedS >::vertex_descriptor Vertex
The type for a vertex in the roadmap.
Definition: LazyPRM.h:98
std::function< const std::vector< Vertex > &(const Vertex)> ConnectionStrategy
A function returning the milestones that should be attempted to connect to.
Definition: LazyPRM.h:138
A class to store the exit status of Planner::solve()
Definition: PlannerStatus.h:48
A 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:190
Definition of an abstract state.
Definition: State.h:49
#define OMPL_WARN(fmt,...)
Log a formatted warning string.
Definition: Console.h:66
void setNearestNeighbors()
Set a different nearest neighbors datastructure.
Definition: LazyPRM.h:163
std::function< bool(const Vertex &, const Vertex &)> ConnectionFilter
A function that can reject connections.
Definition: LazyPRM.h:145
A 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:131
unsigned long int milestoneCount() const
Return the number of milestones currently in the graph.
Definition: LazyPRM.h:220
std::vector< Vertex > goalM_
Array of goal milestones.
Definition: LazyPRM.h:331
boost::property_map< Graph, edge_flags_t >::type edgeValidityProperty_
Access the validity state of an edge.
Definition: LazyPRM.h:349
std::shared_ptr< NearestNeighbors< Vertex > > RoadmapNeighbors
A nearest neighbors data structure for roadmap vertices.
Definition: LazyPRM.h:134
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:356
A shared pointer wrapper for ompl::base::Path.
base::StateSamplerPtr sampler_
Sampler user for generating random in the state space.
Definition: LazyPRM.h:319