ompl/geometric/planners/rrt/src/RRT.cpp
00001 /********************************************************************* 00002 * Software License Agreement (BSD License) 00003 * 00004 * Copyright (c) 2008, Willow Garage, Inc. 00005 * All rights reserved. 00006 * 00007 * Redistribution and use in source and binary forms, with or without 00008 * modification, are permitted provided that the following conditions 00009 * are met: 00010 * 00011 * * Redistributions of source code must retain the above copyright 00012 * notice, this list of conditions and the following disclaimer. 00013 * * Redistributions in binary form must reproduce the above 00014 * copyright notice, this list of conditions and the following 00015 * disclaimer in the documentation and/or other materials provided 00016 * with the distribution. 00017 * * Neither the name of the Willow Garage nor the names of its 00018 * contributors may be used to endorse or promote products derived 00019 * from this software without specific prior written permission. 00020 * 00021 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 00022 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00023 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 00024 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 00025 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 00026 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 00027 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 00028 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 00029 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 00030 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 00031 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00032 * POSSIBILITY OF SUCH DAMAGE. 00033 *********************************************************************/ 00034 00035 /* Author: Ioan Sucan */ 00036 00037 #include "ompl/geometric/planners/rrt/RRT.h" 00038 #include "ompl/base/goals/GoalSampleableRegion.h" 00039 #include "ompl/tools/config/SelfConfig.h" 00040 #include <limits> 00041 00042 ompl::geometric::RRT::RRT(const base::SpaceInformationPtr &si) : base::Planner(si, "RRT") 00043 { 00044 specs_.approximateSolutions = true; 00045 specs_.directed = true; 00046 00047 goalBias_ = 0.05; 00048 maxDistance_ = 0.0; 00049 lastGoalMotion_ = NULL; 00050 00051 Planner::declareParam<double>("range", this, &RRT::setRange, &RRT::getRange, "0.:1.:10000."); 00052 Planner::declareParam<double>("goal_bias", this, &RRT::setGoalBias, &RRT::getGoalBias, "0.:.05:1."); 00053 } 00054 00055 ompl::geometric::RRT::~RRT() 00056 { 00057 freeMemory(); 00058 } 00059 00060 void ompl::geometric::RRT::clear() 00061 { 00062 Planner::clear(); 00063 sampler_.reset(); 00064 freeMemory(); 00065 if (nn_) 00066 nn_->clear(); 00067 lastGoalMotion_ = NULL; 00068 } 00069 00070 void ompl::geometric::RRT::setup() 00071 { 00072 Planner::setup(); 00073 tools::SelfConfig sc(si_, getName()); 00074 sc.configurePlannerRange(maxDistance_); 00075 00076 if (!nn_) 00077 nn_.reset(tools::SelfConfig::getDefaultNearestNeighbors<Motion*>(si_->getStateSpace())); 00078 nn_->setDistanceFunction(boost::bind(&RRT::distanceFunction, this, _1, _2)); 00079 } 00080 00081 void ompl::geometric::RRT::freeMemory() 00082 { 00083 if (nn_) 00084 { 00085 std::vector<Motion*> motions; 00086 nn_->list(motions); 00087 for (unsigned int i = 0 ; i < motions.size() ; ++i) 00088 { 00089 if (motions[i]->state) 00090 si_->freeState(motions[i]->state); 00091 delete motions[i]; 00092 } 00093 } 00094 } 00095 00096 ompl::base::PlannerStatus ompl::geometric::RRT::solve(const base::PlannerTerminationCondition &ptc) 00097 { 00098 checkValidity(); 00099 base::Goal *goal = pdef_->getGoal().get(); 00100 base::GoalSampleableRegion *goal_s = dynamic_cast<base::GoalSampleableRegion*>(goal); 00101 00102 while (const base::State *st = pis_.nextStart()) 00103 { 00104 Motion *motion = new Motion(si_); 00105 si_->copyState(motion->state, st); 00106 nn_->add(motion); 00107 } 00108 00109 if (nn_->size() == 0) 00110 { 00111 OMPL_ERROR("%s: There are no valid initial states!", getName().c_str()); 00112 return base::PlannerStatus::INVALID_START; 00113 } 00114 00115 if (!sampler_) 00116 sampler_ = si_->allocStateSampler(); 00117 00118 OMPL_INFORM("%s: Starting planning with %u states already in datastructure", getName().c_str(), nn_->size()); 00119 00120 Motion *solution = NULL; 00121 Motion *approxsol = NULL; 00122 double approxdif = std::numeric_limits<double>::infinity(); 00123 Motion *rmotion = new Motion(si_); 00124 base::State *rstate = rmotion->state; 00125 base::State *xstate = si_->allocState(); 00126 00127 while (ptc == false) 00128 { 00129 00130 /* sample random state (with goal biasing) */ 00131 if (goal_s && rng_.uniform01() < goalBias_ && goal_s->canSample()) 00132 goal_s->sampleGoal(rstate); 00133 else 00134 sampler_->sampleUniform(rstate); 00135 00136 /* find closest state in the tree */ 00137 Motion *nmotion = nn_->nearest(rmotion); 00138 base::State *dstate = rstate; 00139 00140 /* find state to add */ 00141 double d = si_->distance(nmotion->state, rstate); 00142 if (d > maxDistance_) 00143 { 00144 si_->getStateSpace()->interpolate(nmotion->state, rstate, maxDistance_ / d, xstate); 00145 dstate = xstate; 00146 } 00147 00148 if (si_->checkMotion(nmotion->state, dstate)) 00149 { 00150 /* create a motion */ 00151 Motion *motion = new Motion(si_); 00152 si_->copyState(motion->state, dstate); 00153 motion->parent = nmotion; 00154 00155 nn_->add(motion); 00156 double dist = 0.0; 00157 bool sat = goal->isSatisfied(motion->state, &dist); 00158 if (sat) 00159 { 00160 approxdif = dist; 00161 solution = motion; 00162 break; 00163 } 00164 if (dist < approxdif) 00165 { 00166 approxdif = dist; 00167 approxsol = motion; 00168 } 00169 } 00170 } 00171 00172 bool solved = false; 00173 bool approximate = false; 00174 if (solution == NULL) 00175 { 00176 solution = approxsol; 00177 approximate = true; 00178 } 00179 00180 if (solution != NULL) 00181 { 00182 lastGoalMotion_ = solution; 00183 00184 /* construct the solution path */ 00185 std::vector<Motion*> mpath; 00186 while (solution != NULL) 00187 { 00188 mpath.push_back(solution); 00189 solution = solution->parent; 00190 } 00191 00192 /* set the solution path */ 00193 PathGeometric *path = new PathGeometric(si_); 00194 for (int i = mpath.size() - 1 ; i >= 0 ; --i) 00195 path->append(mpath[i]->state); 00196 pdef_->addSolutionPath(base::PathPtr(path), approximate, approxdif, getName()); 00197 solved = true; 00198 } 00199 00200 si_->freeState(xstate); 00201 if (rmotion->state) 00202 si_->freeState(rmotion->state); 00203 delete rmotion; 00204 00205 OMPL_INFORM("%s: Created %u states", getName().c_str(), nn_->size()); 00206 00207 return base::PlannerStatus(solved, approximate); 00208 } 00209 00210 void ompl::geometric::RRT::getPlannerData(base::PlannerData &data) const 00211 { 00212 Planner::getPlannerData(data); 00213 00214 std::vector<Motion*> motions; 00215 if (nn_) 00216 nn_->list(motions); 00217 00218 if (lastGoalMotion_) 00219 data.addGoalVertex(base::PlannerDataVertex(lastGoalMotion_->state)); 00220 00221 for (unsigned int i = 0 ; i < motions.size() ; ++i) 00222 { 00223 if (motions[i]->parent == NULL) 00224 data.addStartVertex(base::PlannerDataVertex(motions[i]->state)); 00225 else 00226 data.addEdge(base::PlannerDataVertex(motions[i]->parent->state), 00227 base::PlannerDataVertex(motions[i]->state)); 00228 } 00229 }