All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
pRRT.cpp
1 /*********************************************************************
2 * Software License Agreement (BSD License)
3 *
4 * Copyright (c) 2008, Willow Garage, Inc.
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 Willow Garage 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 #include "ompl/geometric/planners/rrt/pRRT.h"
38 #include "ompl/datastructures/NearestNeighborsGNAT.h"
39 #include "ompl/base/goals/GoalSampleableRegion.h"
40 #include "ompl/tools/config/SelfConfig.h"
41 #include <boost/thread/thread.hpp>
42 #include <limits>
43 
44 ompl::geometric::pRRT::pRRT(const base::SpaceInformationPtr &si) : base::Planner(si, "pRRT"),
45  samplerArray_(si)
46 {
48  specs_.multithreaded = true;
49  specs_.directed = true;
50 
51  setThreadCount(2);
52  goalBias_ = 0.05;
53  maxDistance_ = 0.0;
54  lastGoalMotion_ = NULL;
55 
56  Planner::declareParam<double>("range", this, &pRRT::setRange, &pRRT::getRange);
57  Planner::declareParam<double>("goal_bias", this, &pRRT::setGoalBias, &pRRT::getGoalBias);
58  Planner::declareParam<unsigned int>("thread_count", this, &pRRT::setThreadCount, &pRRT::getThreadCount);
59 }
60 
61 ompl::geometric::pRRT::~pRRT(void)
62 {
63  freeMemory();
64 }
65 
67 {
68  Planner::setup();
69  tools::SelfConfig sc(si_, getName());
70  sc.configurePlannerRange(maxDistance_);
71 
72  if (!nn_)
73  nn_.reset(new NearestNeighborsGNAT<Motion*>());
74  nn_->setDistanceFunction(boost::bind(&pRRT::distanceFunction, this, _1, _2));
75 }
76 
78 {
79  Planner::clear();
80  samplerArray_.clear();
81  freeMemory();
82  if (nn_)
83  nn_->clear();
84  lastGoalMotion_ = NULL;
85 }
86 
87 void ompl::geometric::pRRT::freeMemory(void)
88 {
89  if (nn_)
90  {
91  std::vector<Motion*> motions;
92  nn_->list(motions);
93  for (unsigned int i = 0 ; i < motions.size() ; ++i)
94  {
95  if (motions[i]->state)
96  si_->freeState(motions[i]->state);
97  delete motions[i];
98  }
99  }
100 }
101 
102 void ompl::geometric::pRRT::threadSolve(unsigned int tid, const base::PlannerTerminationCondition &ptc, SolutionInfo *sol)
103 {
104  checkValidity();
105  base::Goal *goal = pdef_->getGoal().get();
106  base::GoalSampleableRegion *goal_s = dynamic_cast<base::GoalSampleableRegion*>(goal);
107  RNG rng;
108 
109  Motion *rmotion = new Motion(si_);
110  base::State *rstate = rmotion->state;
111  base::State *xstate = si_->allocState();
112 
113  while (sol->solution == NULL && ptc() == false)
114  {
115  /* sample random state (with goal biasing) */
116  if (goal_s && rng.uniform01() < goalBias_ && goal_s->canSample())
117  goal_s->sampleGoal(rstate);
118  else
119  samplerArray_[tid]->sampleUniform(rstate);
120 
121  /* find closest state in the tree */
122  nnLock_.lock();
123  Motion *nmotion = nn_->nearest(rmotion);
124  nnLock_.unlock();
125  base::State *dstate = rstate;
126 
127  /* find state to add */
128  double d = si_->distance(nmotion->state, rstate);
129  if (d > maxDistance_)
130  {
131  si_->getStateSpace()->interpolate(nmotion->state, rstate, maxDistance_ / d, xstate);
132  dstate = xstate;
133  }
134 
135  if (si_->checkMotion(nmotion->state, dstate))
136  {
137  /* create a motion */
138  Motion *motion = new Motion(si_);
139  si_->copyState(motion->state, dstate);
140  motion->parent = nmotion;
141 
142  nnLock_.lock();
143  nn_->add(motion);
144  nnLock_.unlock();
145 
146  double dist = 0.0;
147  bool solved = goal->isSatisfied(motion->state, &dist);
148  if (solved)
149  {
150  sol->lock.lock();
151  sol->approxdif = dist;
152  sol->solution = motion;
153  sol->lock.unlock();
154  break;
155  }
156  if (dist < sol->approxdif)
157  {
158  sol->lock.lock();
159  if (dist < sol->approxdif)
160  {
161  sol->approxdif = dist;
162  sol->approxsol = motion;
163  }
164  sol->lock.unlock();
165  }
166  }
167  }
168 
169  si_->freeState(xstate);
170  if (rmotion->state)
171  si_->freeState(rmotion->state);
172  delete rmotion;
173 }
174 
176 {
177  base::GoalRegion *goal = dynamic_cast<base::GoalRegion*>(pdef_->getGoal().get());
178 
179  if (!goal)
180  {
181  logError("Goal undefined");
183  }
184 
185  samplerArray_.resize(threadCount_);
186 
187  while (const base::State *st = pis_.nextStart())
188  {
189  Motion *motion = new Motion(si_);
190  si_->copyState(motion->state, st);
191  nn_->add(motion);
192  }
193 
194  if (nn_->size() == 0)
195  {
196  logError("There are no valid initial states!");
198  }
199 
200  logInform("Starting with %u states", nn_->size());
201 
202  SolutionInfo sol;
203  sol.solution = NULL;
204  sol.approxsol = NULL;
205  sol.approxdif = std::numeric_limits<double>::infinity();
206 
207  std::vector<boost::thread*> th(threadCount_);
208  for (unsigned int i = 0 ; i < threadCount_ ; ++i)
209  th[i] = new boost::thread(boost::bind(&pRRT::threadSolve, this, i, ptc, &sol));
210  for (unsigned int i = 0 ; i < threadCount_ ; ++i)
211  {
212  th[i]->join();
213  delete th[i];
214  }
215 
216  bool solved = false;
217  bool approximate = false;
218  if (sol.solution == NULL)
219  {
220  sol.solution = sol.approxsol;
221  approximate = true;
222  }
223 
224  if (sol.solution != NULL)
225  {
226  lastGoalMotion_ = sol.solution;
227 
228  /* construct the solution path */
229  std::vector<Motion*> mpath;
230  while (sol.solution != NULL)
231  {
232  mpath.push_back(sol.solution);
233  sol.solution = sol.solution->parent;
234  }
235 
236  /* set the solution path */
237  PathGeometric *path = new PathGeometric(si_);
238  for (int i = mpath.size() - 1 ; i >= 0 ; --i)
239  path->append(mpath[i]->state);
240 
241  pdef_->addSolutionPath(base::PathPtr(path), approximate, sol.approxdif);
242  solved = true;
243  }
244 
245  logInform("Created %u states", nn_->size());
246 
247  return base::PlannerStatus(solved, approximate);
248 }
249 
251 {
252  Planner::getPlannerData(data);
253 
254  std::vector<Motion*> motions;
255  if (nn_)
256  nn_->list(motions);
257 
258  if (lastGoalMotion_)
259  data.addGoalVertex(base::PlannerDataVertex(lastGoalMotion_->state));
260 
261  for (unsigned int i = 0 ; i < motions.size() ; ++i)
262  {
263  if (motions[i]->parent == NULL)
264  data.addStartVertex(base::PlannerDataVertex(motions[i]->state));
265  else
266  data.addEdge(base::PlannerDataVertex(motions[i]->parent->state),
267  base::PlannerDataVertex(motions[i]->state));
268  }
269 }
270 
271 void ompl::geometric::pRRT::setThreadCount(unsigned int nthreads)
272 {
273  assert(nthreads > 0);
274  threadCount_ = nthreads;
275 }