FMT.h
1 /*********************************************************************
2 * Software License Agreement (BSD License)
3 *
4 * Copyright (c) 2013, Autonomous Systems Laboratory, Stanford 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 Stanford 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: Ashley Clark (Stanford) and Wolfgang Pointner (AIT) */
36 /* Co-developers: Brice Rebsamen (Stanford), Tim Wheeler (Stanford)
37  Edward Schmerling (Stanford), and Javier V. Gómez (UC3M - Stanford)*/
38 /* Algorithm design: Lucas Janson (Stanford) and Marco Pavone (Stanford) */
39 /* Acknowledgements for insightful comments: Oren Salzman (Tel Aviv University),
40  * Joseph Starek (Stanford) */
41 
42 #ifndef OMPL_GEOMETRIC_PLANNERS_FMT_
43 #define OMPL_GEOMETRIC_PLANNERS_FMT_
44 
45 #include <ompl/geometric/planners/PlannerIncludes.h>
46 #include <ompl/base/goals/GoalSampleableRegion.h>
47 #include <ompl/datastructures/NearestNeighbors.h>
48 #include <ompl/datastructures/BinaryHeap.h>
49 #include <ompl/base/OptimizationObjective.h>
50 #include <map>
51 
52 
53 namespace ompl
54 {
55 
56  namespace geometric
57  {
58 
91  class FMT : public ompl::base::Planner
92  {
93  public:
94 
95  FMT(const base::SpaceInformationPtr &si);
96 
97  virtual ~FMT();
98 
99  virtual void setup();
100 
102 
103  virtual void clear();
104 
105  virtual void getPlannerData(base::PlannerData &data) const;
106 
112  void setNumSamples(const unsigned int numSamples)
113  {
114  numSamples_ = numSamples;
115  }
116 
118  unsigned int getNumSamples() const
119  {
120  return numSamples_;
121  }
122 
124  void setNearestK(bool nearestK)
125  {
126  nearestK_ = nearestK;
127  }
128 
130  bool getNearestK() const
131  {
132  return nearestK_;
133  }
134 
142  void setRadiusMultiplier(const double radiusMultiplier)
143  {
144  if (radiusMultiplier <= 0.0)
145  throw Exception("Radius multiplier must be greater than zero");
146  radiusMultiplier_ = radiusMultiplier;
147  }
148 
151  double getRadiusMultiplier() const
152  {
153  return radiusMultiplier_;
154  }
155 
159  void setFreeSpaceVolume(const double freeSpaceVolume)
160  {
161  if (freeSpaceVolume < 0.0)
162  throw Exception("Free space volume should be greater than zero");
163  freeSpaceVolume_ = freeSpaceVolume;
164  }
165 
168  double getFreeSpaceVolume() const
169  {
170  return freeSpaceVolume_;
171  }
172 
175  void setCacheCC(bool ccc)
176  {
177  cacheCC_ = ccc;
178  }
179 
181  bool getCacheCC() const
182  {
183  return cacheCC_;
184  }
185 
187  void setHeuristics(bool h)
188  {
189  heuristics_ = h;
190  }
191 
194  bool getHeuristics() const
195  {
196  return heuristics_;
197  }
198 
200  void setExtendedFMT(bool e)
201  {
202  extendedFMT_ = e;
203  }
204 
206  bool getExtendedFMT() const
207  {
208  return extendedFMT_;
209  }
210 
211  protected:
214  class Motion
215  {
216  public:
217 
225  enum SetType { SET_CLOSED, SET_OPEN, SET_UNVISITED };
226 
227  Motion()
228  : state_(nullptr), parent_(nullptr), cost_(0.0), currentSet_(SET_UNVISITED)
229  {
230  }
231 
234  : state_(si->allocState()), parent_(nullptr), cost_(0.0), currentSet_(SET_UNVISITED)
235  {
236  }
237 
238  ~Motion()
239  {
240  }
241 
243  void setState(base::State *state)
244  {
245  state_ = state;
246  }
247 
250  {
251  return state_;
252  }
253 
255  void setParent(Motion *parent)
256  {
257  parent_ = parent;
258  }
259 
261  Motion* getParent() const
262  {
263  return parent_;
264  }
265 
267  void setCost(const base::Cost cost)
268  {
269  cost_ = cost;
270  }
271 
274  {
275  return cost_;
276  }
277 
279  void setSetType(const SetType currentSet)
280  {
281  currentSet_ = currentSet;
282  }
283 
286  {
287  return currentSet_;
288  }
289 
292  bool alreadyCC(Motion *m)
293  {
294  if (collChecksDone_.find(m) == collChecksDone_.end())
295  return false;
296  return true;
297  }
298 
300  void addCC(Motion *m)
301  {
302  collChecksDone_.insert(m);
303  }
304 
307  {
308  hcost_ = h;
309  }
310 
313  {
314  return hcost_;
315  }
316 
318  std::vector<Motion*>& getChildren()
319  {
320  return children_;
321  }
322 
323  protected:
324 
327 
330 
333 
336 
339 
341  std::set<Motion*> collChecksDone_;
342 
344  std::vector<Motion*> children_;
345  };
346 
349  {
350  MotionCompare() : opt_(nullptr), heuristics_(false)
351  {
352  }
353 
354  /* Returns true if m1 is lower cost than m2. m1 and m2 must
355  have been instantiated with the same optimization objective */
356  bool operator()(const Motion *m1, const Motion *m2) const
357  {
358  if (heuristics_)
359  return opt_->isCostBetterThan(opt_->combineCosts(m1->getCost(), m1->getHeuristicCost()),
360  opt_->combineCosts(m2->getCost(), m2->getHeuristicCost()));
361  else
362  return opt_->isCostBetterThan(m1->getCost(), m2->getCost());
363  }
364 
366  bool heuristics_;
367  };
368 
373  double distanceFunction(const Motion *a, const Motion *b) const
374  {
375  return opt_->motionCost(a->getState(), b->getState()).value();
376  }
377 
379  void freeMemory();
380 
384 
392 
394  double calculateUnitBallVolume(const unsigned int dimension) const;
395 
401  double calculateRadius(unsigned int dimension, unsigned int n) const;
402 
405  void saveNeighborhood(Motion *m);
406 
409  void traceSolutionPathThroughTree(Motion *goalMotion);
410 
417  bool expandTreeFromNode(Motion **z);
418 
422  void updateNeighborhood(Motion *m, const std::vector<Motion *> nbh);
423 
425  Motion* getBestParent(Motion *m, std::vector<Motion*> &neighbors, base::Cost &cMin);
426 
430 
436 
439  std::map<Motion*, std::vector<Motion*> > neighborhoods_;
440 
442  unsigned int numSamples_;
443 
445  unsigned int collisionChecks_;
446 
448  bool nearestK_;
449 
451  bool cacheCC_;
452 
455 
457  double NNr_;
458 
460  unsigned int NNk_;
461 
465 
475 
477  std::shared_ptr< NearestNeighbors<Motion*> > nn_;
478 
481 
484 
487 
490 
493 
494  // For sorting a list of costs and getting only their sorted indices
496  {
497  CostIndexCompare(const std::vector<base::Cost>& costs,
498  const base::OptimizationObjective &opt) :
499  costs_(costs), opt_(opt)
500  {}
501  bool operator()(unsigned i, unsigned j)
502  {
503  return opt_.isCostBetterThan(costs_[i],costs_[j]);
504  }
505  const std::vector<base::Cost>& costs_;
506  const base::OptimizationObjective &opt_;
507  };
508 
509  };
510  }
511 }
512 
513 #endif // OMPL_GEOMETRIC_PLANNERS_FMT_
bool nearestK_
Flag to activate the K nearest neighbors strategy.
Definition: FMT.h:448
bool cacheCC_
Flag to activate the collision check caching.
Definition: FMT.h:451
void setExtendedFMT(bool e)
Activates the extended FMT*: adding new samples if planner does not finish successfully.
Definition: FMT.h:200
Object containing planner generated vertex and edge data. It is assumed that all vertices are unique...
Definition: PlannerData.h:163
void sampleFree(const ompl::base::PlannerTerminationCondition &ptc)
Sample a state from the free configuration space and save it into the nearest neighbors data structur...
Definition: FMT.cpp:213
unsigned int numSamples_
The number of samples to use when planning.
Definition: FMT.h:442
SetType
The FMT* planner begins with all nodes included in set Unvisited "Waiting for optimal connection"...
Definition: FMT.h:225
void setParent(Motion *parent)
Set the parent motion of the current motion.
Definition: FMT.h:255
bool getNearestK() const
Get the state of the nearestK strategy.
Definition: FMT.h:130
std::vector< Motion * > children_
The set of motions descending from the current motion.
Definition: FMT.h:344
void setRadiusMultiplier(const double radiusMultiplier)
The planner searches for neighbors of a node within a cost r, where r is the value described for FMT*...
Definition: FMT.h:142
void addCC(Motion *m)
Caches a failed collision check to m.
Definition: FMT.h:300
virtual void setup()
Perform extra configuration steps, if needed. This call will also issue a call to ompl::base::SpaceIn...
Definition: FMT.cpp:84
void setFreeSpaceVolume(const double freeSpaceVolume)
Store the volume of the obstacle-free configuration space. If no value is specified, the default assumes an obstacle-free unit hypercube, freeSpaceVolume = (maximumExtent/sqrt(dimension))^(dimension)
Definition: FMT.h:159
SetType currentSet_
The flag indicating which set a motion belongs to.
Definition: FMT.h:338
base::StateSamplerPtr sampler_
State sampler.
Definition: FMT.h:480
double distanceFunction(const Motion *a, const Motion *b) const
Compute the distance between two motions as the cost between their contained states. Note that for computationally intensive cost functions, the cost between motions should be stored to avoid duplicate calculations.
Definition: FMT.h:373
A shared pointer wrapper for ompl::base::StateSampler.
double NNr_
Radius employed in the nearestR strategy.
Definition: FMT.h:457
bool getExtendedFMT() const
Returns true if the extended FMT* is activated.
Definition: FMT.h:206
bool extendedFMT_
Add new samples if the tree was not able to find a solution.
Definition: FMT.h:492
base::Cost cost_
The cost of this motion.
Definition: FMT.h:332
base::OptimizationObjectivePtr opt_
The cost objective function.
Definition: FMT.h:483
MotionBinHeap Open_
A binary heap for storing explored motions in cost-to-come sorted order. The motions in Open have bee...
Definition: FMT.h:435
double freeSpaceVolume_
The volume of the free configuration space, computed as an upper bound with 95% confidence.
Definition: FMT.h:464
base::Cost getCost() const
Get the cost-to-come for the current motion.
Definition: FMT.h:273
Encapsulate a termination condition for a motion planner. Planners will call operator() to decide whe...
std::map< Motion *, std::vector< Motion * > > neighborhoods_
A map linking a motion to all of the motions within a distance r of that motion.
Definition: FMT.h:439
unsigned int collisionChecks_
Number of collision checks performed by the algorithm.
Definition: FMT.h:445
std::set< Motion * > collChecksDone_
Contains the connections attempted FROM this node.
Definition: FMT.h:341
base::Cost hcost_
The minimum cost to go of this motion (heuristically computed)
Definition: FMT.h:335
Representation of a motion.
Definition: FMT.h:214
void freeMemory()
Free the memory allocated by this planner.
Definition: FMT.cpp:121
void setNumSamples(const unsigned int numSamples)
Set the number of states that the planner should sample. The planner will sample this number of state...
Definition: FMT.h:112
virtual void clear()
Clear all internal datastructures. Planner settings are not affected. Subsequent calls to solve() wil...
Definition: FMT.cpp:136
std::vector< Motion * > & getChildren()
Get the children of the motion.
Definition: FMT.h:318
bool expandTreeFromNode(Motion **z)
Complete one iteration of the main loop of the FMT* algorithm: Find K nearest nodes in set Unvisited ...
Definition: FMT.cpp:501
void traceSolutionPathThroughTree(Motion *goalMotion)
Trace the path from a goal state back to the start state and save the result as a solution in the Pro...
Definition: FMT.cpp:481
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: FMT.cpp:276
Motion(const base::SpaceInformationPtr &si)
Constructor that allocates memory for the state.
Definition: FMT.h:233
unsigned int getNumSamples() const
Get the number of states that the planner will sample.
Definition: FMT.h:118
ompl::BinaryHeap< Motion *, MotionCompare > MotionBinHeap
A binary heap for storing explored motions in cost-to-come sorted order.
Definition: FMT.h:429
Asymptotically Optimal Fast Marching Tree algorithm developed by L. Janson and M. Pavone...
Definition: FMT.h:91
Abstract definition of a goal region that can be sampled.
unsigned int NNk_
K used in the nearestK strategy.
Definition: FMT.h:460
Motion * getBestParent(Motion *m, std::vector< Motion * > &neighbors, base::Cost &cMin)
Returns the best parent and the connection cost in the neighborhood of a motion m.
Definition: FMT.cpp:621
double getRadiusMultiplier() const
Get the multiplier used for the nearest neighbors search radius.
Definition: FMT.h:151
void setCacheCC(bool ccc)
Sets the collision check caching to save calls to the collision checker with slightly memory usage as...
Definition: FMT.h:175
Base class for a planner.
Definition: Planner.h:230
void setSetType(const SetType currentSet)
Specify the set that this motion belongs to.
Definition: FMT.h:279
void saveNeighborhood(Motion *m)
Save the neighbors within a neighborhood of a given state. The strategy used (nearestK or nearestR de...
Definition: FMT.cpp:170
virtual bool isCostBetterThan(Cost c1, Cost c2) const
Check whether the the cost c1 is considered better than the cost c2. By default, this returns true if...
Motion * getParent() const
Get the parent motion of the current motion.
Definition: FMT.h:261
A class to store the exit status of Planner::solve()
Definition: PlannerStatus.h:48
A shared pointer wrapper for ompl::base::SpaceInformation.
base::State * goalState_
Goal state caching to accelerate cost to go heuristic computation.
Definition: FMT.h:489
void setState(base::State *state)
Set the state associated with the motion.
Definition: FMT.h:243
Definition of an abstract state.
Definition: State.h:50
base::Cost getHeuristicCost() const
Get the cost to go heuristic cost.
Definition: FMT.h:312
base::State * getState() const
Get the state associated with the motion.
Definition: FMT.h:249
void setCost(const base::Cost cost)
Set the cost-to-come for the current motion.
Definition: FMT.h:267
Abstract definition of optimization objectives.
void setHeuristics(bool h)
Activates the cost to go heuristics when ordering the heap.
Definition: FMT.h:187
void setNearestK(bool nearestK)
If nearestK is true, FMT will be run using the Knearest strategy.
Definition: FMT.h:124
bool getHeuristics() const
Returns true if the heap is ordered taking into account cost to go heuristics.
Definition: FMT.h:194
The exception type for ompl.
Definition: Exception.h:47
Motion * lastGoalMotion_
The most recent goal motion. Used for PlannerData computation.
Definition: FMT.h:486
A shared pointer wrapper for ompl::base::OptimizationObjective.
void assureGoalIsSampled(const ompl::base::GoalSampleableRegion *goal)
For each goal region, check to see if any of the sampled states fall within that region. If not, add a goal state from that region directly into the set of vertices. In this way, FMT is able to find a solution, if one exists. If no sampled nodes are within a goal region, there would be no way for the algorithm to successfully find a path to that region.
Definition: FMT.cpp:241
void setHeuristicCost(const base::Cost h)
Set the cost to go heuristic cost.
Definition: FMT.h:306
double getFreeSpaceVolume() const
Get the volume of the free configuration space that is being used by the planner. ...
Definition: FMT.h:168
Motion * parent_
The parent motion in the exploration tree.
Definition: FMT.h:329
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: FMT.cpp:150
double calculateRadius(unsigned int dimension, unsigned int n) const
Calculate the radius to use for nearest neighbor searches, using the bound given in L...
Definition: FMT.cpp:205
std::shared_ptr< NearestNeighbors< Motion * > > nn_
A nearest-neighbor datastructure containing the set of all motions.
Definition: FMT.h:477
SetType getSetType() const
Get the set that this motion belongs to.
Definition: FMT.h:285
double radiusMultiplier_
This planner uses a nearest neighbor search radius proportional to the lower bound for optimality der...
Definition: FMT.h:474
void updateNeighborhood(Motion *m, const std::vector< Motion * > nbh)
For a motion m, updates the stored neighborhoods of all its neighbors by by inserting m (maintaining ...
Definition: FMT.cpp:640
virtual Cost combineCosts(Cost c1, Cost c2) const
Get the cost that corresponds to combining the costs c1 and c2. Default implementation defines this c...
bool getCacheCC() const
Get the state of the collision check caching.
Definition: FMT.h:181
Comparator used to order motions in a binary heap.
Definition: FMT.h:348
base::State * state_
The state contained by the motion.
Definition: FMT.h:326
double calculateUnitBallVolume(const unsigned int dimension) const
Compute the volume of the unit ball in a given dimension.
Definition: FMT.cpp:195
bool heuristics_
Flag to activate the cost to go heuristics.
Definition: FMT.h:454
Definition of a cost value. Can represent the cost of a motion or the cost of a state.
Definition: Cost.h:47
bool alreadyCC(Motion *m)
Returns true if the connection to m has been already tested and failed because of a collision...
Definition: FMT.h:292