RRTstar.h
1 /*********************************************************************
2 * Software License Agreement (BSD License)
3 *
4 * Copyright (c) 2011, 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 /* Authors: Alejandro Perez, Sertac Karaman, Ryan Luna, Luis G. Torres, Ioan Sucan, Javier V Gomez, Jonathan Gammell */
36 
37 #ifndef OMPL_GEOMETRIC_PLANNERS_RRT_RRTSTAR_
38 #define OMPL_GEOMETRIC_PLANNERS_RRT_RRTSTAR_
39 
40 #include "ompl/geometric/planners/PlannerIncludes.h"
41 #include "ompl/base/OptimizationObjective.h"
42 #include "ompl/datastructures/NearestNeighbors.h"
43 
44 #include <limits>
45 #include <vector>
46 #include <queue>
47 #include <deque>
48 #include <utility>
49 #include <list>
50 
51 
52 namespace ompl
53 {
54 
55  namespace geometric
56  {
57 
79  class RRTstar : public base::Planner
80  {
81  public:
82 
84 
85  virtual ~RRTstar();
86 
87  virtual void getPlannerData(base::PlannerData &data) const;
88 
90 
91  virtual void clear();
92 
93  virtual void setup();
94 
104  void setGoalBias(double goalBias)
105  {
106  goalBias_ = goalBias;
107  }
108 
110  double getGoalBias() const
111  {
112  return goalBias_;
113  }
114 
120  void setRange(double distance)
121  {
122  maxDistance_ = distance;
123  }
124 
126  double getRange() const
127  {
128  return maxDistance_;
129  }
130 
132  void setRewireFactor(double rewireFactor)
133  {
134  rewireFactor_ = rewireFactor;
136  }
137 
139  double getRewireFactor() const
140  {
141  return rewireFactor_;
142  }
143 
145  template<template<typename T> class NN>
147  {
148  nn_.reset(new NN<Motion*>());
149  }
150 
158  void setDelayCC(bool delayCC)
159  {
160  delayCC_ = delayCC;
161  }
162 
164  bool getDelayCC() const
165  {
166  return delayCC_;
167  }
168 
176  void setTreePruning(const bool prune);
177 
179  bool getTreePruning() const
180  {
181  return useTreePruning_;
182  }
183 
187  void setPruneThreshold(const double pp)
188  {
189  pruneThreshold_ = pp;
190  }
191 
193  double getPruneThreshold() const
194  {
195  return pruneThreshold_;
196  }
197 
200  void setPrunedMeasure(bool informedMeasure);
201 
203  bool getPrunedMeasure() const
204  {
205  return usePrunedMeasure_;
206  }
207 
210  void setInformedSampling(bool informedSampling);
211 
213  bool getInformedSampling() const
214  {
215  return useInformedSampling_;
216  }
217 
219  void setSampleRejection(const bool reject);
220 
222  bool getSampleRejection() const
223  {
224  return useRejectionSampling_;
225  }
226 
228  void setNewStateRejection(const bool reject)
229  {
230  useNewStateRejection_ = reject;
231  }
232 
234  bool getNewStateRejection() const
235  {
236  return useNewStateRejection_;
237  }
238 
240  void setAdmissibleCostToCome(const bool admissible)
241  {
242  useAdmissibleCostToCome_ = admissible;
243  }
244 
247  {
249  }
250 
256  void setFocusSearch(const bool focus)
257  {
258  setInformedSampling(focus);
259  setTreePruning(focus);
260  setPrunedMeasure(focus);
261  setNewStateRejection(focus);
262  }
263 
265  bool getFocusSearch() const
266  {
268  }
269 
271  void setKNearest(bool useKNearest)
272  {
273  useKNearest_ = useKNearest;
274  }
275 
277  bool getKNearest() const
278  {
279  return useKNearest_;
280  }
281 
283  void setNumSamplingAttempts(unsigned int numAttempts)
284  {
285  numSampleAttempts_ = numAttempts;
286  }
287 
289  unsigned int getNumSamplingAttempts() const
290  {
291  return numSampleAttempts_ ;
292  }
293 
294  unsigned int numIterations() const
295  {
296  return iterations_;
297  }
298 
299  ompl::base::Cost bestCost() const
300  {
301  return bestCost_;
302  }
303 
304  protected:
305 
307  class Motion
308  {
309  public:
312  state(si->allocState()),
313  parent(nullptr)
314  {
315  }
316 
317  ~Motion()
318  {
319  }
320 
323 
326 
329 
332 
334  std::vector<Motion*> children;
335  };
336 
338  void allocSampler();
339 
341  bool sampleUniform(base::State *statePtr);
342 
344  void freeMemory();
345 
346  // For sorting a list of costs and getting only their sorted indices
348  {
349  CostIndexCompare(const std::vector<base::Cost>& costs,
350  const base::OptimizationObjective &opt) :
351  costs_(costs), opt_(opt)
352  {}
353  bool operator()(unsigned i, unsigned j)
354  {
355  return opt_.isCostBetterThan(costs_[i],costs_[j]);
356  }
357  const std::vector<base::Cost>& costs_;
359  };
360 
362  double distanceFunction(const Motion *a, const Motion *b) const
363  {
364  return si_->distance(a->state, b->state);
365  }
366 
368  void getNeighbors(Motion *motion, std::vector<Motion*> &nbh) const;
369 
371  void removeFromParent(Motion *m);
372 
374  void updateChildCosts(Motion *m);
375 
378  int pruneTree(const base::Cost& pruneTreeCost);
379 
385  base::Cost solutionHeuristic(const Motion *motion) const;
386 
388  void addChildrenToList(std::queue<Motion*, std::deque<Motion*> > *motionList, Motion* motion);
389 
391  bool keepCondition(const Motion* motion, const base::Cost& threshold) const;
392 
395 
398 
400  base::InformedSamplerPtr infSampler_;
401 
403  std::shared_ptr< NearestNeighbors<Motion*> > nn_;
404 
406  double goalBias_;
407 
409  double maxDistance_;
410 
413 
416 
419 
421  double k_rrg_;
423  double r_rrg_;
424 
426  bool delayCC_;
427 
430 
433 
435  std::vector<Motion*> goalMotions_;
436 
439 
442 
445 
448 
451 
454 
457 
459  unsigned int numSampleAttempts_;
460 
462  std::vector<Motion*> startMotions_;
463 
466 
469 
472 
474  unsigned int iterations_;
475 
477  // Planner progress property functions
478  std::string numIterationsProperty() const
479  {
480  return std::to_string(numIterations());
481  }
482  std::string bestCostProperty() const
483  {
484  return std::to_string(bestCost().value());
485  }
486  };
487  }
488 }
489 
490 #endif
bool getNewStateRejection() const
Get the state of the new-state rejection option.
Definition: RRTstar.h:234
bool getTreePruning() const
Get the state of the pruning option.
Definition: RRTstar.h:179
Object containing planner generated vertex and edge data. It is assumed that all vertices are unique...
Definition: PlannerData.h:163
void setFocusSearch(const bool focus)
A meta parameter to focusing the search to improving the current solution. This is the parameter set ...
Definition: RRTstar.h:256
Optimal Rapidly-exploring Random Trees.
Definition: RRTstar.h:79
void addChildrenToList(std::queue< Motion *, std::deque< Motion * > > *motionList, Motion *motion)
Add the children of a vertex to the given list.
Definition: RRTstar.cpp:833
virtual void clear()
Clear all internal datastructures. Planner settings are not affected. Subsequent calls to solve() wil...
Definition: RRTstar.cpp:153
bool useRejectionSampling_
The status of the sample rejection parameter.
Definition: RRTstar.h:450
void getNeighbors(Motion *motion, std::vector< Motion * > &nbh) const
Gets the neighbours of a given motion, using either k-nearest of radius as appropriate.
Definition: RRTstar.cpp:576
void setRewireFactor(double rewireFactor)
Set the rewiring scale factor, s, such that r_rrg = s r_rrg* (or k_rrg = s k_rrg*) ...
Definition: RRTstar.h:132
void setDelayCC(bool delayCC)
Option that delays collision checking procedures. When it is enabled, all neighbors are sorted by cos...
Definition: RRTstar.h:158
void setInformedSampling(bool informedSampling)
Use direct sampling of the heuristic for the generation of random samples (e.g., x_rand). If a direct sampling method is not defined for the objective, rejection sampling will be used by default.
Definition: RRTstar.cpp:934
bool getSampleRejection() const
Get the state of the sample rejection option.
Definition: RRTstar.h:222
void updateChildCosts(Motion *m)
Updates the cost of the children of this node if the cost up to this node has changed.
Definition: RRTstar.cpp:605
double getGoalBias() const
Get the goal bias the planner is using.
Definition: RRTstar.h:110
base::OptimizationObjectivePtr opt_
Objective we&#39;re optimizing.
Definition: RRTstar.h:429
A shared pointer wrapper for ompl::base::StateSampler.
void setPrunedMeasure(bool informedMeasure)
Use the measure of the pruned subproblem instead of the measure of the entire problem domain (if such...
Definition: RRTstar.cpp:891
double goalBias_
The fraction of time the goal is picked as the state to expand towards (if such a state is available)...
Definition: RRTstar.h:406
bool useAdmissibleCostToCome_
The admissibility of the new-state rejection heuristic.
Definition: RRTstar.h:456
base::Cost cost
The cost up to this motion.
Definition: RRTstar.h:328
Encapsulate a termination condition for a motion planner. Planners will call operator() to decide whe...
bool useNewStateRejection_
The status of the new-state rejection parameter.
Definition: RRTstar.h:453
bool useTreePruning_
The status of the tree pruning option.
Definition: RRTstar.h:438
void setRange(double distance)
Set the range the planner is supposed to use.
Definition: RRTstar.h:120
Motion * lastGoalMotion_
The most recent goal motion. Used for PlannerData computation.
Definition: RRTstar.h:432
void setPruneThreshold(const double pp)
Set the fractional change in solution cost necessary for pruning to occur, i.e., prune if the new sol...
Definition: RRTstar.h:187
base::Cost bestCost_
Best cost found so far by algorithm.
Definition: RRTstar.h:465
void setNewStateRejection(const bool reject)
Controls whether heuristic rejection is used on new states before connection (e.g., x_new = steer(x_nearest, x_rand))
Definition: RRTstar.h:228
virtual void setup()
Perform extra configuration steps, if needed. This call will also issue a call to ompl::base::SpaceIn...
Definition: RRTstar.cpp:104
void setKNearest(bool useKNearest)
Use a k-nearest search for rewiring instead of a r-disc search.
Definition: RRTstar.h:271
double maxDistance_
The maximum length of a motion to be added to a tree.
Definition: RRTstar.h:409
Representation of a motion.
Definition: RRTstar.h:307
double getRange() const
Get the range the planner is using.
Definition: RRTstar.h:126
base::State * state
The state contained by the motion.
Definition: RRTstar.h:322
base::Cost prunedCost_
The cost at which the graph was last pruned.
Definition: RRTstar.h:468
void freeMemory()
Free the memory allocated by this planner.
Definition: RRTstar.cpp:614
bool keepCondition(const Motion *motion, const base::Cost &threshold) const
Check whether the given motion passes the specified cost threshold, meaning it will be kept during pr...
Definition: RRTstar.cpp:841
void setNumSamplingAttempts(unsigned int numAttempts)
Set the number of attempts to make while performing rejection or informed sampling.
Definition: RRTstar.h:283
bool getAdmissibleCostToCome() const
Get the admissibility of the pruning and new-state rejection heuristic.
Definition: RRTstar.h:246
Main namespace. Contains everything in this library.
Definition: Cost.h:42
unsigned int getNumSamplingAttempts() const
Get the number of attempts to make while performing rejection or informed sampling.
Definition: RRTstar.h:289
double pruneThreshold_
The tree is pruned when the change in solution cost is greater than this fraction.
Definition: RRTstar.h:441
bool getKNearest() const
Get the state of using a k-nearest search for rewiring.
Definition: RRTstar.h:277
void setAdmissibleCostToCome(const bool admissible)
Controls whether pruning and new-state rejection uses an admissible cost-to-come estimate or not...
Definition: RRTstar.h:240
Random number generation. An instance of this class cannot be used by multiple threads at once (membe...
Definition: RandomNumbers.h:58
Base class for a planner.
Definition: Planner.h:230
virtual void getPlannerData(base::PlannerData &data) const
Get information about the current run of the motion planner. Repeated calls to this function will upd...
Definition: RRTstar.cpp:629
bool getPrunedMeasure() const
Get the state of using the pruned measure.
Definition: RRTstar.h:203
int pruneTree(const base::Cost &pruneTreeCost)
Prunes all those states which estimated total cost is higher than pruneTreeCost. Returns the number o...
Definition: RRTstar.cpp:650
std::vector< Motion * > startMotions_
Stores the start states as Motions.
Definition: RRTstar.h:462
double getPruneThreshold() const
Get the current prune states percentage threshold parameter.
Definition: RRTstar.h:193
A class to store the exit status of Planner::solve()
Definition: PlannerStatus.h:48
double r_rrg_
A constant for r-disc rewiring calculations.
Definition: RRTstar.h:423
A shared pointer wrapper for ompl::base::SpaceInformation.
base::Cost solutionHeuristic(const Motion *motion) const
Computes the solution cost heuristically as the cost to come from start to the motion plus the cost t...
Definition: RRTstar.cpp:848
double prunedMeasure_
The measure of the problem when we pruned it (if this isn&#39;t in use, it will be set to si_->getSpaceMe...
Definition: RRTstar.h:471
Definition of an abstract state.
Definition: State.h:50
std::vector< Motion * > children
The set of motions descending from the current motion.
Definition: RRTstar.h:334
base::InformedSamplerPtr infSampler_
An informed sampler.
Definition: RRTstar.h:400
Abstract definition of optimization objectives.
base::StateSamplerPtr sampler_
State sampler.
Definition: RRTstar.h:397
void setNearestNeighbors()
Set a different nearest neighbors datastructure.
Definition: RRTstar.h:146
A shared pointer wrapper for ompl::base::OptimizationObjective.
std::shared_ptr< NearestNeighbors< Motion * > > nn_
A nearest-neighbors datastructure containing the tree of motions.
Definition: RRTstar.h:403
double k_rrg_
A constant for k-nearest rewiring calculations.
Definition: RRTstar.h:421
RNG rng_
The random number generator.
Definition: RRTstar.h:412
bool getFocusSearch() const
Get the state of search focusing.
Definition: RRTstar.h:265
void setSampleRejection(const bool reject)
Controls whether heuristic rejection is used on samples (e.g., x_rand)
Definition: RRTstar.cpp:981
Motion * parent
The parent motion in the exploration tree.
Definition: RRTstar.h:325
double rewireFactor_
The rewiring factor, s, so that r_rrg = s r_rrg* > r_rrg* (or k_rrg = s k_rrg* > k_rrg*) ...
Definition: RRTstar.h:418
bool delayCC_
Option to delay and reduce collision checking within iterations.
Definition: RRTstar.h:426
bool useInformedSampling_
Option to use informed sampling.
Definition: RRTstar.h:447
bool sampleUniform(base::State *statePtr)
Generate a sample.
Definition: RRTstar.cpp:1038
void allocSampler()
Create the samplers.
Definition: RRTstar.cpp:1016
void calculateRewiringLowerBounds()
Calculate the k_RRG* and r_RRG* terms.
Definition: RRTstar.cpp:1059
virtual base::PlannerStatus solve(const base::PlannerTerminationCondition &ptc)
Function that can solve the motion planning problem. This function can be called multiple times on th...
Definition: RRTstar.cpp:173
void removeFromParent(Motion *m)
Removes the given motion from the parent&#39;s child list.
Definition: RRTstar.cpp:592
void setTreePruning(const bool prune)
Controls whether the tree is pruned during the search. This pruning removes a vertex if and only if i...
Definition: RRTstar.cpp:871
bool usePrunedMeasure_
Option to use the informed measure.
Definition: RRTstar.h:444
void setGoalBias(double goalBias)
Set the goal bias.
Definition: RRTstar.h:104
SpaceInformationPtr si_
The space information for which planning is done.
Definition: Planner.h:398
Motion(const base::SpaceInformationPtr &si)
Constructor that allocates memory for the state. This constructor automatically allocates memory for ...
Definition: RRTstar.h:311
bool getDelayCC() const
Get the state of the delayed collision checking option.
Definition: RRTstar.h:164
std::vector< Motion * > goalMotions_
A list of states in the tree that satisfy the goal condition.
Definition: RRTstar.h:435
Definition of a cost value. Can represent the cost of a motion or the cost of a state.
Definition: Cost.h:47
base::Cost incCost
The incremental cost of this motion&#39;s parent to this motion (this is stored to save distance computat...
Definition: RRTstar.h:331
double getRewireFactor() const
Set the rewiring scale factor, s, such that r_rrg = s r_rrg* > r_rrg* (or k_rrg = s k_rrg* > k_rrg*...
Definition: RRTstar.h:139
bool getInformedSampling() const
Get the state direct heuristic sampling.
Definition: RRTstar.h:213
bool useKNearest_
Option to use k-nearest search for rewiring.
Definition: RRTstar.h:415
unsigned int iterations_
Number of iterations the algorithm performed.
Definition: RRTstar.h:474
double distanceFunction(const Motion *a, const Motion *b) const
Compute distance between motions (actually distance between contained states)
Definition: RRTstar.h:362
unsigned int numSampleAttempts_
The number of attempts to make at informed sampling.
Definition: RRTstar.h:459