RRTXstatic.h
1 /*********************************************************************
2 * Software License Agreement (BSD License)
3 *
4 * Copyright (c) 2016, Georgia Institute of Technology
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: Florian Hauer */
36 
37 #ifndef OMPL_GEOMETRIC_PLANNERS_RRT_RRTXSTATIC_
38 #define OMPL_GEOMETRIC_PLANNERS_RRT_RRTXSTATIC_
39 
40 #include <ompl/datastructures/BinaryHeap.h>
41 #include "ompl/base/OptimizationObjective.h"
42 #include "ompl/datastructures/NearestNeighbors.h"
43 #include "ompl/geometric/planners/PlannerIncludes.h"
44 
45 #include <deque>
46 #include <limits>
47 #include <list>
48 #include <queue>
49 #include <utility>
50 #include <vector>
51 
52 namespace ompl
53 {
54  namespace geometric
55  {
103  class RRTXstatic : public base::Planner
104  {
105  public:
107 
108  virtual ~RRTXstatic();
109 
110  virtual void getPlannerData(base::PlannerData &data) const;
111 
113 
114  virtual void clear();
115 
116  virtual void setup();
117 
127  void setGoalBias(double goalBias)
128  {
129  goalBias_ = goalBias;
130  }
131 
133  double getGoalBias() const
134  {
135  return goalBias_;
136  }
137 
140  void setInformedSampling(bool informedSampling);
141 
143  bool getInformedSampling() const
144  {
145  return useInformedSampling_;
146  }
147 
149  void setSampleRejection(const bool reject);
150 
152  bool getSampleRejection() const
153  {
154  return useRejectionSampling_;
155  }
156 
158  void setNumSamplingAttempts(unsigned int numAttempts)
159  {
160  numSampleAttempts_ = numAttempts;
161  }
162 
164  unsigned int getNumSamplingAttempts() const
165  {
166  return numSampleAttempts_;
167  }
168 
173  virtual void setEpsilon(double epsilon)
174  {
175  epsilonCost_ = base::Cost(epsilon);
176  }
177 
179  double getEpsilon() const
180  {
181  return epsilonCost_.value();
182  }
183 
189  void setRange(double distance)
190  {
191  maxDistance_ = distance;
192  }
193 
195  double getRange() const
196  {
197  return maxDistance_;
198  }
199 
202  void setRewireFactor(double rewireFactor)
203  {
204  rewireFactor_ = rewireFactor;
206  }
207 
210  double getRewireFactor() const
211  {
212  return rewireFactor_;
213  }
214 
216  template <template <typename T> class NN>
218  {
219  if (nn_ && nn_->size() != 0)
220  OMPL_WARN("Calling setNearestNeighbors will clear all states.");
221  clear();
222  nn_ = std::make_shared<NN<Motion *>>();
223  setup();
224  }
225 
227  void setKNearest(bool useKNearest)
228  {
229  useKNearest_ = useKNearest;
230  }
231 
233  bool getKNearest() const
234  {
235  return useKNearest_;
236  }
237 
239  void setUpdateChildren(bool val)
240  {
241  updateChildren_ = val;
242  }
243 
245  bool getUpdateChildren() const
246  {
247  return updateChildren_;
248  }
249 
251  void setVariant(const int variant)
252  {
253  if (variant < 0 || variant > 3)
254  throw Exception("Variant must be 0 (original RRT#) or in [1, 3]");
255  variant_ = variant;
256  }
257 
259  int getVariant() const
260  {
261  return variant_;
262  }
263 
265  void setAlpha(const double a)
266  {
267  alpha_ = a;
268  }
269 
271  double getAlpha() const
272  {
273  return alpha_;
274  }
275 
276  unsigned int numIterations() const
277  {
278  return iterations_;
279  }
280 
281  ompl::base::Cost bestCost() const
282  {
283  return bestCost_;
284  }
285 
286  protected:
287  class Motion;
288 
291  {
294  : opt_(opt), pdef_(pdef)
295  {
296  }
297 
299  inline base::Cost costPlusHeuristic(const Motion *m) const
300  {
301  return opt_->combineCosts(m->cost, opt_->costToGo(m->state, pdef_->getGoal().get()));
302  }
303 
305  inline base::Cost alphaCostPlusHeuristic(const Motion *m, double alpha) const
306  {
307  return opt_->combineCosts(base::Cost(alpha * m->cost.value()),
308  opt_->costToGo(m->state, pdef_->getGoal().get()));
309  }
310 
312  inline bool operator()(const Motion *m1, const Motion *m2) const
313  {
314  // we use a max heap, to do a min heap so the operator < returns > in order to make it a min heap
315  return !opt_->isCostBetterThan(costPlusHeuristic(m1), costPlusHeuristic(m2));
316  }
317 
320 
323  };
324 
326  class Motion
327  {
328  public:
331  Motion(const base::SpaceInformationPtr &si) : state(si->allocState()), parent(nullptr), handle(nullptr)
332  {
333  }
334 
335  ~Motion()
336  {
337  }
338 
341 
344 
347 
349  std::vector<Motion *> children;
350 
353  std::vector<std::pair<Motion *, bool>> nbh;
354 
357  };
358 
360  void allocSampler();
361 
363  bool sampleUniform(base::State *statePtr);
364 
366  void freeMemory();
367 
369  double distanceFunction(const Motion *a, const Motion *b) const
370  {
371  return si_->distance(a->state, b->state);
372  }
373 
375  void updateQueue(Motion *x);
376 
378  void removeFromParent(Motion *m);
379 
381  void getNeighbors(Motion *motion) const;
382 
385 
387  void calculateRRG();
388 
390  bool includeVertex(const Motion *x) const;
391 
394 
396  base::InformedSamplerPtr infSampler_;
397 
399  std::shared_ptr<NearestNeighbors<Motion *>> nn_;
400 
403  double goalBias_;
404 
406  double maxDistance_;
407 
410 
413 
417 
419  double k_rrt_;
421  double r_rrt_;
422 
425 
428 
430  std::vector<Motion *> goalMotions_;
431 
434 
436  unsigned int iterations_;
437 
440 
443 
446 
449 
451  double rrg_r_;
452 
454  unsigned int rrg_k_;
455 
457  int variant_;
458 
460  double alpha_;
461 
464 
467 
469  unsigned int numSampleAttempts_;
470 
472  // Planner progress property functions
473  std::string numIterationsProperty() const
474  {
475  return std::to_string(numIterations());
476  }
477  std::string bestCostProperty() const
478  {
479  return std::to_string(bestCost().value());
480  }
481  std::string numMotionsProperty() const
482  {
483  return std::to_string(nn_->size());
484  }
485  };
486  }
487 }
488 
489 #endif
bool useInformedSampling_
Option to use informed sampling.
Definition: RRTXstatic.h:463
void setUpdateChildren(bool val)
Set whether or not to always propagate cost updates to children.
Definition: RRTXstatic.h:239
Object containing planner generated vertex and edge data. It is assumed that all vertices are unique...
Definition: PlannerData.h:174
void allocSampler()
Create the samplers.
Definition: RRTXstatic.cpp:754
bool getUpdateChildren() const
True if the cost is always propagate to children.
Definition: RRTXstatic.h:245
base::State * state
The state contained by the motion.
Definition: RRTXstatic.h:340
std::vector< Motion * > goalMotions_
A list of states in the tree that satisfy the goal condition.
Definition: RRTXstatic.h:430
base::Cost epsilonCost_
Threshold for the propagation of information.
Definition: RRTXstatic.h:445
A shared pointer wrapper for ompl::base::ProblemDefinition.
double k_rrt_
A constant for k-nearest rewiring calculations.
Definition: RRTXstatic.h:419
virtual void setup()
Perform extra configuration steps, if needed. This call will also issue a call to ompl::base::SpaceIn...
Definition: RRTXstatic.cpp:105
std::vector< std::pair< Motion *, bool > > nbh
The set of neighbors of this motion with a boolean indicating if the feasibility of edge as been test...
Definition: RRTXstatic.h:353
void calculateRewiringLowerBounds()
Calculate the k_RRG* and r_RRG* terms.
Definition: RRTXstatic.cpp:797
void setAlpha(const double a)
Set the value alpha used for rejection sampling.
Definition: RRTXstatic.h:265
bool sampleUniform(base::State *statePtr)
Generate a sample.
Definition: RRTXstatic.cpp:776
A shared pointer wrapper for ompl::base::StateSampler.
Representation of a motion (node of the tree)
Definition: RRTXstatic.h:326
unsigned int rrg_k_
Current value of the number of neighbors used.
Definition: RRTXstatic.h:454
std::vector< Motion * > children
The set of motions descending from the current motion.
Definition: RRTXstatic.h:349
MotionCompare mc_
Comparator of motions, used to order the queue.
Definition: RRTXstatic.h:439
MotionCompare(const base::OptimizationObjectivePtr &opt, const base::ProblemDefinitionPtr &pdef)
Constructor.
Definition: RRTXstatic.h:293
Encapsulate a termination condition for a motion planner. Planners will call operator() to decide whe...
BinaryHeap< Motion *, MotionCompare >::Element * handle
Handle to identify the motion in the queue.
Definition: RRTXstatic.h:356
bool getKNearest() const
Get the state of using a k-nearest search for rewiring.
Definition: RRTXstatic.h:233
void setGoalBias(double goalBias)
Set the goal bias.
Definition: RRTXstatic.h:127
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: RRTXstatic.cpp:171
double rewireFactor_
The rewiring factor, s, so that r_rrg = s r_rrg* > r_rrg* (or k_rrg = s k_rrg* > k_rrg*) ...
Definition: RRTXstatic.h:416
base::OptimizationObjectivePtr opt_
Objective we&#39;re optimizing.
Definition: RRTXstatic.h:424
base::StateSamplerPtr sampler_
State sampler.
Definition: RRTXstatic.h:393
int getVariant() const
Get variant used for rejection sampling.
Definition: RRTXstatic.h:259
void setKNearest(bool useKNearest)
Use a k-nearest search for rewiring instead of a r-disc search.
Definition: RRTXstatic.h:227
void setRewireFactor(double rewireFactor)
Set the rewiring scale factor, s, such that r_rrg = s r_rrg* (or k_rrg = s k_rrg*) ...
Definition: RRTXstatic.h:202
void setSampleRejection(const bool reject)
Controls whether heuristic rejection is used on samples (e.g., x_rand)
Definition: RRTXstatic.cpp:718
void removeFromParent(Motion *m)
Removes the given motion from the parent&#39;s child list.
Definition: RRTXstatic.cpp:589
base::InformedSamplerPtr infSampler_
An informed sampler.
Definition: RRTXstatic.h:396
void calculateRRG()
Calculate the rrg_r_ and rrg_k_ terms.
Definition: RRTXstatic.cpp:601
void setNumSamplingAttempts(unsigned int numAttempts)
Set the number of attempts to make while performing rejection or informed sampling.
Definition: RRTXstatic.h:158
unsigned int iterations_
Number of iterations the algorithm performed.
Definition: RRTXstatic.h:436
bool useRejectionSampling_
The status of the sample rejection parameter.
Definition: RRTXstatic.h:466
bool updateChildren_
Whether or not to propagate the cost to children if the update is less than epsilon.
Definition: RRTXstatic.h:448
RNG rng_
The random number generator.
Definition: RRTXstatic.h:409
This class provides an implementation of an updatable min-heap. Using it is a bit cumbersome...
Definition: BinaryHeap.h:51
Main namespace. Contains everything in this library.
Definition: AppBase.h:21
double r_rrt_
A constant for r-disc rewiring calculations.
Definition: RRTXstatic.h:421
Random number generation. An instance of this class cannot be used by multiple threads at once (membe...
Definition: RandomNumbers.h:58
bool useKNearest_
Option to use k-nearest search for rewiring.
Definition: RRTXstatic.h:412
Motion(const base::SpaceInformationPtr &si)
Constructor that allocates memory for the state. This constructor automatically allocates memory for ...
Definition: RRTXstatic.h:331
Base class for a planner.
Definition: Planner.h:232
void freeMemory()
Free the memory allocated by this planner.
Definition: RRTXstatic.cpp:647
bool operator()(const Motion *m1, const Motion *m2) const
Ordering of motions.
Definition: RRTXstatic.h:312
void setNearestNeighbors()
Set a different nearest neighbors datastructure.
Definition: RRTXstatic.h:217
A class to store the exit status of Planner::solve()
Definition: PlannerStatus.h:48
virtual void setEpsilon(double epsilon)
Set the threshold epsilon.
Definition: RRTXstatic.h:173
A shared pointer wrapper for ompl::base::SpaceInformation.
bool getSampleRejection() const
Get the state of the sample rejection option.
Definition: RRTXstatic.h:152
virtual void clear()
Clear all internal datastructures. Planner settings are not affected. Subsequent calls to solve() wil...
Definition: RRTXstatic.cpp:154
double maxDistance_
The maximum length of a motion to be added to a tree.
Definition: RRTXstatic.h:406
Definition of an abstract state.
Definition: State.h:49
base::Cost bestCost_
Best cost found so far by algorithm.
Definition: RRTXstatic.h:433
#define OMPL_WARN(fmt,...)
Log a formatted warning string.
Definition: Console.h:66
void getNeighbors(Motion *motion) const
Gets the neighbours of a given motion, using either k-nearest of radius as appropriate.
Definition: RRTXstatic.cpp:609
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: RRTXstatic.cpp:682
The exception type for ompl.
Definition: Exception.h:46
A shared pointer wrapper for ompl::base::OptimizationObjective.
BinaryHeap< Motion *, MotionCompare > q_
Queue to order the nodes to update.
Definition: RRTXstatic.h:442
double getGoalBias() const
Get the goal bias the planner is using.
Definition: RRTXstatic.h:133
std::shared_ptr< NearestNeighbors< Motion * > > nn_
A nearest-neighbors datastructure containing the tree of motions.
Definition: RRTXstatic.h:399
double distanceFunction(const Motion *a, const Motion *b) const
Compute distance between motions (actually distance between contained states)
Definition: RRTXstatic.h:369
unsigned int getNumSamplingAttempts() const
Get the number of attempts to make while performing rejection or informed sampling.
Definition: RRTXstatic.h:164
double getAlpha() const
Get the value alpha used for rejection sampling.
Definition: RRTXstatic.h:271
double getEpsilon() const
Get the threshold epsilon the planner is using.
Definition: RRTXstatic.h:179
double value() const
The value of the cost.
Definition: Cost.h:56
bool getInformedSampling() const
Get the state direct heuristic sampling.
Definition: RRTXstatic.h:143
base::Cost costPlusHeuristic(const Motion *m) const
Combines the current cost of a motion and the heuritic to the goal.
Definition: RRTXstatic.h:299
void updateQueue(Motion *x)
Update (or add) a motion in the queue.
Definition: RRTXstatic.cpp:576
Optimal Rapidly-exploring Random Trees Maintaining A Pseudo Optimal Tree.
Definition: RRTXstatic.h:103
void setVariant(const int variant)
Set variant used for rejection sampling.
Definition: RRTXstatic.h:251
void setRange(double distance)
Set the range the planner is supposed to use.
Definition: RRTXstatic.h:189
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: RRTXstatic.cpp:662
bool includeVertex(const Motion *x) const
Test if the vertex should be included according to the variant in use.
Definition: RRTXstatic.cpp:632
int variant_
Variant used for rejection sampling.
Definition: RRTXstatic.h:457
double alpha_
Alpha parameter, scaling the rejection sampling tests.
Definition: RRTXstatic.h:460
double rrg_r_
Current value of the radius used for the neighbors.
Definition: RRTXstatic.h:451
double goalBias_
The fraction of time the goal is picked as the state to expand towards (if such a state is available)...
Definition: RRTXstatic.h:403
base::ProblemDefinitionPtr pdef_
Pointer to the Problem Definition.
Definition: RRTXstatic.h:322
double getRange() const
Get the range the planner is using.
Definition: RRTXstatic.h:195
base::OptimizationObjectivePtr opt_
Pointer to the Optimization Objective.
Definition: RRTXstatic.h:319
SpaceInformationPtr si_
The space information for which planning is done.
Definition: Planner.h:415
base::Cost cost
The cost up to this motion.
Definition: RRTXstatic.h:346
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: RRTXstatic.h:210
Motion * lastGoalMotion_
The most recent goal motion. Used for PlannerData computation.
Definition: RRTXstatic.h:427
Defines the operator to compare motions.
Definition: RRTXstatic.h:290
base::Cost alphaCostPlusHeuristic(const Motion *m, double alpha) const
Combines the current cost of a motion, weighted by alpha, and the heuritic to the goal...
Definition: RRTXstatic.h:305
unsigned int numSampleAttempts_
The number of attempts to make at informed sampling.
Definition: RRTXstatic.h:469
Definition of a cost value. Can represent the cost of a motion or the cost of a state.
Definition: Cost.h:47
Motion * parent
The parent motion in the exploration tree.
Definition: RRTXstatic.h:343