ompl/geometric/planners/rrt/src/LazyRRT.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/LazyRRT.h"
00038 #include "ompl/base/goals/GoalSampleableRegion.h"
00039 #include "ompl/tools/config/SelfConfig.h"
00040 #include <cassert>
00041 
00042 ompl::geometric::LazyRRT::LazyRRT(const base::SpaceInformationPtr &si) : base::Planner(si, "LazyRRT")
00043 {
00044     specs_.directed = true;
00045     goalBias_ = 0.05;
00046     maxDistance_ = 0.0;
00047     lastGoalMotion_ = NULL;
00048 
00049     Planner::declareParam<double>("range", this, &LazyRRT::setRange, &LazyRRT::getRange, "0.:1.:10000.");
00050     Planner::declareParam<double>("goal_bias", this, &LazyRRT::setGoalBias, &LazyRRT::getGoalBias, "0.:.05:1.");
00051 }
00052 
00053 ompl::geometric::LazyRRT::~LazyRRT()
00054 {
00055     freeMemory();
00056 }
00057 
00058 void ompl::geometric::LazyRRT::setup()
00059 {
00060     Planner::setup();
00061     tools::SelfConfig sc(si_, getName());
00062     sc.configurePlannerRange(maxDistance_);
00063 
00064     if (!nn_)
00065         nn_.reset(tools::SelfConfig::getDefaultNearestNeighbors<Motion*>(si_->getStateSpace()));
00066     nn_->setDistanceFunction(boost::bind(&LazyRRT::distanceFunction, this, _1, _2));
00067 }
00068 
00069 void ompl::geometric::LazyRRT::clear()
00070 {
00071     Planner::clear();
00072     sampler_.reset();
00073     freeMemory();
00074     if (nn_)
00075         nn_->clear();
00076     lastGoalMotion_ = NULL;
00077 }
00078 
00079 void ompl::geometric::LazyRRT::freeMemory()
00080 {
00081     if (nn_)
00082     {
00083         std::vector<Motion*> motions;
00084         nn_->list(motions);
00085         for (unsigned int i = 0 ; i < motions.size() ; ++i)
00086         {
00087             if (motions[i]->state)
00088                 si_->freeState(motions[i]->state);
00089             delete motions[i];
00090         }
00091     }
00092 }
00093 
00094 ompl::base::PlannerStatus ompl::geometric::LazyRRT::solve(const base::PlannerTerminationCondition &ptc)
00095 {
00096     checkValidity();
00097     base::Goal                 *goal   = pdef_->getGoal().get();
00098     base::GoalSampleableRegion *goal_s = dynamic_cast<base::GoalSampleableRegion*>(goal);
00099 
00100     while (const base::State *st = pis_.nextStart())
00101     {
00102         Motion *motion = new Motion(si_);
00103         si_->copyState(motion->state, st);
00104         motion->valid = true;
00105         nn_->add(motion);
00106     }
00107 
00108     if (nn_->size() == 0)
00109     {
00110         OMPL_ERROR("%s: There are no valid initial states!", getName().c_str());
00111         return base::PlannerStatus::INVALID_START;
00112     }
00113 
00114     if (!sampler_)
00115         sampler_ = si_->allocStateSampler();
00116 
00117     OMPL_INFORM("%s: Starting planning with %u states already in datastructure", getName().c_str(), nn_->size());
00118 
00119     Motion *solution = NULL;
00120     double  distsol  = -1.0;
00121     Motion *rmotion  = new Motion(si_);
00122     base::State *rstate = rmotion->state;
00123     base::State *xstate = si_->allocState();
00124 
00125     bool solutionFound = false;
00126 
00127     while (ptc == false && !solutionFound)
00128     {
00129         /* sample random state (with goal biasing) */
00130         if (goal_s && rng_.uniform01() < goalBias_ && goal_s->canSample())
00131             goal_s->sampleGoal(rstate);
00132         else
00133             sampler_->sampleUniform(rstate);
00134 
00135         /* find closest state in the tree */
00136         Motion *nmotion = nn_->nearest(rmotion);
00137         assert(nmotion != 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         /* create a motion */
00149         Motion *motion = new Motion(si_);
00150         si_->copyState(motion->state, dstate);
00151         motion->parent = nmotion;
00152         nmotion->children.push_back(motion);
00153         nn_->add(motion);
00154 
00155         double dist = 0.0;
00156         if (goal->isSatisfied(motion->state, &dist))
00157         {
00158             distsol = dist;
00159             solution = motion;
00160             solutionFound = true;
00161             lastGoalMotion_ = solution;
00162 
00163             // Check that the solution is valid:
00164             // construct the solution path
00165             std::vector<Motion*> mpath;
00166             while (solution != NULL)
00167             {
00168                 mpath.push_back(solution);
00169                 solution = solution->parent;
00170             }
00171 
00172             // check each segment along the path for validity
00173             for (int i = mpath.size() - 1 ; i >= 0 && solutionFound; --i)
00174                 if (!mpath[i]->valid)
00175                 {
00176                     if (si_->checkMotion(mpath[i]->parent->state, mpath[i]->state))
00177                         mpath[i]->valid = true;
00178                     else
00179                     {
00180                         removeMotion(mpath[i]);
00181                         solutionFound = false;
00182                         lastGoalMotion_ = NULL;
00183                     }
00184                 }
00185 
00186             if (solutionFound)
00187             {
00188                 // set the solution path
00189                 PathGeometric *path = new PathGeometric(si_);
00190                 for (int i = mpath.size() - 1 ; i >= 0 ; --i)
00191                     path->append(mpath[i]->state);
00192 
00193                 pdef_->addSolutionPath(base::PathPtr(path), false, distsol, getName());
00194             }
00195         }
00196     }
00197 
00198     si_->freeState(xstate);
00199     si_->freeState(rstate);
00200     delete rmotion;
00201 
00202     OMPL_INFORM("%s: Created %u states", getName().c_str(), nn_->size());
00203 
00204     return solutionFound ?  base::PlannerStatus::EXACT_SOLUTION : base::PlannerStatus::TIMEOUT;
00205 }
00206 
00207 void ompl::geometric::LazyRRT::removeMotion(Motion *motion)
00208 {
00209     nn_->remove(motion);
00210 
00211     /* remove self from parent list */
00212 
00213     if (motion->parent)
00214     {
00215         for (unsigned int i = 0 ; i < motion->parent->children.size() ; ++i)
00216             if (motion->parent->children[i] == motion)
00217             {
00218                 motion->parent->children.erase(motion->parent->children.begin() + i);
00219                 break;
00220             }
00221     }
00222 
00223     /* remove children */
00224     for (unsigned int i = 0 ; i < motion->children.size() ; ++i)
00225     {
00226         motion->children[i]->parent = NULL;
00227         removeMotion(motion->children[i]);
00228     }
00229 
00230     if (motion->state)
00231         si_->freeState(motion->state);
00232     delete motion;
00233 }
00234 
00235 void ompl::geometric::LazyRRT::getPlannerData(base::PlannerData &data) const
00236 {
00237     Planner::getPlannerData(data);
00238 
00239     std::vector<Motion*> motions;
00240     if (nn_)
00241         nn_->list(motions);
00242 
00243     if (lastGoalMotion_)
00244         data.addGoalVertex(base::PlannerDataVertex(lastGoalMotion_->state, 1));
00245 
00246     for (unsigned int i = 0 ; i < motions.size() ; ++i)
00247     {
00248         if (motions[i]->parent == NULL)
00249             data.addStartVertex(base::PlannerDataVertex(motions[i]->state));
00250         else
00251             data.addEdge(base::PlannerDataVertex(motions[i]->parent ? motions[i]->parent->state : NULL),
00252                          base::PlannerDataVertex(motions[i]->state));
00253 
00254         data.tagState(motions[i]->state, motions[i]->valid ? 1 : 0);
00255     }
00256 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines