LBTRRT.h
1 /*********************************************************************
2 * Software License Agreement (BSD License)
3 *
4 * Copyright (c) 2015, Tel Aviv 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 Tel Aviv 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: Oren Salzman, Sertac Karaman, Ioan Sucan, Mark Moll */
36 
37 #ifndef OMPL_GEOMETRIC_PLANNERS_RRT_LBT_RRT_
38 #define OMPL_GEOMETRIC_PLANNERS_RRT_LBT_RRT_
39 
40 #include "ompl/geometric/planners/PlannerIncludes.h"
41 #include "ompl/base/OptimizationObjective.h"
42 #include "ompl/datastructures/NearestNeighbors.h"
43 #include "ompl/datastructures/DynamicSSSP.h"
44 
45 #include <fstream>
46 
47 namespace ompl
48 {
49 
50  namespace geometric
51  {
52 
73  class LBTRRT : public base::Planner
74  {
75  public:
76 
79 
80  virtual ~LBTRRT();
81 
82  virtual void getPlannerData(base::PlannerData &data) const;
83 
85 
86  virtual void clear();
87 
97  void setGoalBias(double goalBias)
98  {
99  goalBias_ = goalBias;
100  }
101 
103  double getGoalBias() const
104  {
105  return goalBias_;
106  }
107 
113  void setRange(double distance)
114  {
115  maxDistance_ = distance;
116  }
117 
119  double getRange() const
120  {
121  return maxDistance_;
122  }
123 
125  template<template<typename T> class NN>
127  {
128  nn_.reset(new NN<Motion*>());
129  }
130 
131  virtual void setup();
132 
134  void setApproximationFactor(double epsilon)
135  {
136  epsilon_ = epsilon;
137  }
138 
140  double getApproximationFactor() const
141  {
142  return epsilon_;
143  }
144 
146  // Planner progress property functions
147  std::string getIterationCount() const
148  {
149  return std::to_string(iterations_);
150  }
151  std::string getBestCost() const
152  {
153  return std::to_string(bestCost_);
154  }
155 
156  protected:
157 
162  class Motion
163  {
164  public:
165 
166  Motion() : state_(nullptr), parentApx_(nullptr), costApx_(0.0)
167  {
168  }
169 
172  : state_(si->allocState()), parentApx_(nullptr), costApx_(0.0)
173  {
174  }
175 
176  ~Motion()
177  {
178  }
179 
183  std::size_t id_;
189  double costLb_;
193  double costApx_;
195  std::vector<Motion*> childrenApx_;
196  };
197 
199  struct IsLessThan
200  {
201  IsLessThan (LBTRRT *plannerPtr, Motion *motion)
202  : plannerPtr_(plannerPtr), motion_(motion)
203  {
204  }
205 
206  bool operator() (const Motion *motionA, const Motion *motionB)
207  {
208  double costA = motionA->costLb_;
209  double costB = motionB->costLb_;
210 
211  double distA = plannerPtr_->distanceFunction(motionA, motion_);
212  double distB = plannerPtr_->distanceFunction(motionB, motion_);
213 
214  return costA + distA < costB + distB;
215  }
216  LBTRRT *plannerPtr_;
217  Motion *motion_;
218  }; //IsLessThan
219 
222  {
223  IsLessThanLB (LBTRRT *plannerPtr): plannerPtr_(plannerPtr)
224  {
225  }
226 
227  bool operator() (const Motion *motionA, const Motion *motionB) const
228  {
229  return motionA->costLb_ < motionB->costLb_;
230  }
231  LBTRRT* plannerPtr_;
232  }; //IsLessThanLB
233 
234  typedef std::set<Motion*, IsLessThanLB> Lb_queue;
235  typedef Lb_queue::iterator Lb_queue_iter;
236 
238  void considerEdge(Motion *parent, Motion *child, double c);
239 
241  double lazilyUpdateApxParent(Motion *child, Motion *parent);
242 
244  void updateChildCostsApx(Motion *m, double delta);
245 
247  void removeFromParentApx(Motion *m);
248 
250  void freeMemory();
251 
253  double distanceFunction(const Motion *a, const Motion *b) const
254  {
255  return si_->distance(a->state_, b->state_);
256  }
257 
259  bool checkMotion(const Motion *a, const Motion *b)
260  {
261  return checkMotion(a->state_, b->state_);
262  }
264  bool checkMotion(const base::State *a, const base::State *b)
265  {
266  return si_->checkMotion(a, b);
267  }
268 
270  Motion* getMotion(std::size_t i)
271  {
272  return idToMotionMap_[i];
273  }
274 
277 
279  std::shared_ptr< NearestNeighbors<Motion*> > nn_;
280 
283 
285  std::vector<Motion*> idToMotionMap_;
286 
288  double goalBias_;
289 
291  double maxDistance_;
292 
294  double epsilon_;
295 
298 
301 
303  // Planner progress properties
305  unsigned int iterations_;
307  double bestCost_;
308  };
309 
310  }
311 }
312 
313 #endif //OMPL_GEOMETRIC_PLANNERS_RRT_LBT_RRT_
void updateChildCostsApx(Motion *m, double delta)
update the child cost of the approximation tree
Definition: LBTRRT.cpp:406
std::shared_ptr< NearestNeighbors< Motion * > > nn_
A nearest-neighbors datastructure containing the tree of motions.
Definition: LBTRRT.h:279
double costApx_
The approximation cost.
Definition: LBTRRT.h:193
comparator - metric is the lower bound cost
Definition: LBTRRT.h:221
Object containing planner generated vertex and edge data. It is assumed that all vertices are unique...
Definition: PlannerData.h:163
void setApproximationFactor(double epsilon)
Set the apprimation factor.
Definition: LBTRRT.h:134
void freeMemory()
Free the memory allocated by this planner.
Definition: LBTRRT.cpp:94
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: LBTRRT.cpp:107
LBTRRT(const base::SpaceInformationPtr &si)
Constructor.
Definition: LBTRRT.cpp:44
base::State * state_
The state contained by the motion.
Definition: LBTRRT.h:181
A shared pointer wrapper for ompl::base::StateSampler.
double getGoalBias() const
Get the goal bias the planner is using.
Definition: LBTRRT.h:103
Encapsulate a termination condition for a motion planner. Planners will call operator() to decide whe...
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: LBTRRT.cpp:385
double maxDistance_
The maximum length of a motion to be added to a tree.
Definition: LBTRRT.h:291
Lower Bound Tree Rapidly-exploring Random Trees.
Definition: LBTRRT.h:73
double distanceFunction(const Motion *a, const Motion *b) const
Compute distance between motions (actually distance between contained states)
Definition: LBTRRT.h:253
void removeFromParentApx(Motion *m)
remove motion from its parent in the approximation tree
Definition: LBTRRT.cpp:429
double goalBias_
The fraction of time the goal is picked as the state to expand towards (if such a state is available)...
Definition: LBTRRT.h:288
double getApproximationFactor() const
Get the apprimation factor.
Definition: LBTRRT.h:140
virtual void setup()
Perform extra configuration steps, if needed. This call will also issue a call to ompl::base::SpaceIn...
Definition: LBTRRT.cpp:83
RNG rng_
The random number generator.
Definition: LBTRRT.h:297
Random number generation. An instance of this class cannot be used by multiple threads at once (membe...
Definition: RandomNumbers.h:58
Motion * lastGoalMotion_
The most recent goal motion. Used for PlannerData computation.
Definition: LBTRRT.h:300
Base class for a planner.
Definition: Planner.h:230
bool checkMotion(const base::State *a, const base::State *b)
local planner
Definition: LBTRRT.h:264
DynamicSSSP lowerBoundGraph_
A graph of motions Glb.
Definition: LBTRRT.h:282
A class to store the exit status of Planner::solve()
Definition: PlannerStatus.h:48
A shared pointer wrapper for ompl::base::SpaceInformation.
double epsilon_
approximation factor
Definition: LBTRRT.h:294
bool checkMotion(const Motion *a, const Motion *b)
local planner
Definition: LBTRRT.h:259
Definition of an abstract state.
Definition: State.h:50
void setRange(double distance)
Set the range the planner is supposed to use.
Definition: LBTRRT.h:113
void considerEdge(Motion *parent, Motion *child, double c)
consider an edge for addition to the roadmap
Definition: LBTRRT.cpp:306
std::vector< Motion * > childrenApx_
The children in the approximation tree.
Definition: LBTRRT.h:195
double getRange() const
Get the range the planner is using.
Definition: LBTRRT.h:119
std::vector< Motion * > idToMotionMap_
mapping between a motion id and the motion
Definition: LBTRRT.h:285
Motion * parentApx_
The parent motion in the approximation tree.
Definition: LBTRRT.h:191
Motion(const base::SpaceInformationPtr &si)
Constructor that allocates memory for the state.
Definition: LBTRRT.h:171
Motion * getMotion(std::size_t i)
get motion from id
Definition: LBTRRT.h:270
double lazilyUpdateApxParent(Motion *child, Motion *parent)
lazily update the parent in the approximation tree without updating costs to cildren ...
Definition: LBTRRT.cpp:417
virtual void clear()
Clear all internal datastructures. Planner settings are not affected. Subsequent calls to solve() wil...
Definition: LBTRRT.cpp:70
comparator - metric is the cost to reach state via a specific state
Definition: LBTRRT.h:199
double bestCost_
Best cost found so far by algorithm.
Definition: LBTRRT.h:307
double costLb_
The lower bound cost of the motion while it is stored in the lowerBoundGraph_ and this may seem redun...
Definition: LBTRRT.h:189
void setGoalBias(double goalBias)
Set the goal bias.
Definition: LBTRRT.h:97
Representation of a motion.
Definition: LBTRRT.h:162
SpaceInformationPtr si_
The space information for which planning is done.
Definition: Planner.h:398
std::size_t id_
unique id of the motion
Definition: LBTRRT.h:183
void setNearestNeighbors()
Set a different nearest neighbors datastructure.
Definition: LBTRRT.h:126
unsigned int iterations_
Number of iterations the algorithm performed.
Definition: LBTRRT.h:305
base::StateSamplerPtr sampler_
State sampler.
Definition: LBTRRT.h:276