All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
KPIECE1.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/kpiece/KPIECE1.h"
38 #include "ompl/base/goals/GoalSampleableRegion.h"
39 #include "ompl/tools/config/SelfConfig.h"
40 #include <limits>
41 #include <cassert>
42 
43 ompl::geometric::KPIECE1::KPIECE1(const base::SpaceInformationPtr &si) : base::Planner(si, "KPIECE1"),
44  disc_(boost::bind(&KPIECE1::freeMotion, this, _1))
45 {
47  specs_.directed = true;
48 
49  goalBias_ = 0.05;
52  maxDistance_ = 0.0;
53  lastGoalMotion_ = NULL;
54 
55  Planner::declareParam<double>("range", this, &KPIECE1::setRange, &KPIECE1::getRange);
56  Planner::declareParam<double>("goal_bias", this, &KPIECE1::setGoalBias, &KPIECE1::getGoalBias);
57  Planner::declareParam<double>("border_fraction", this, &KPIECE1::setBorderFraction, &KPIECE1::getBorderFraction);
58  Planner::declareParam<double>("failed_expansion_score_factor", this, &KPIECE1::setFailedExpansionCellScoreFactor, &KPIECE1::getFailedExpansionCellScoreFactor);
59  Planner::declareParam<double>("min_valid_path_fraction", this, &KPIECE1::setMinValidPathFraction, &KPIECE1::getMinValidPathFraction);
60 }
61 
62 ompl::geometric::KPIECE1::~KPIECE1(void)
63 {
64 }
65 
67 {
68  Planner::setup();
69  tools::SelfConfig sc(si_, getName());
70  sc.configureProjectionEvaluator(projectionEvaluator_);
71  sc.configurePlannerRange(maxDistance_);
72 
73  if (failedExpansionScoreFactor_ < std::numeric_limits<double>::epsilon() || failedExpansionScoreFactor_ > 1.0)
74  throw Exception("Failed expansion cell score factor must be in the range (0,1]");
75  if (minValidPathFraction_ < std::numeric_limits<double>::epsilon() || minValidPathFraction_ > 1.0)
76  throw Exception("The minimum valid path fraction must be in the range (0,1]");
77 
78  disc_.setDimension(projectionEvaluator_->getDimension());
79 }
80 
82 {
83  Planner::clear();
84  sampler_.reset();
85  disc_.clear();
86  lastGoalMotion_ = NULL;
87 }
88 
90 {
91  if (motion->state)
92  si_->freeState(motion->state);
93  delete motion;
94 }
95 
97 {
98  checkValidity();
99  base::Goal *goal = pdef_->getGoal().get();
100  base::GoalSampleableRegion *goal_s = dynamic_cast<base::GoalSampleableRegion*>(goal);
101 
103 
104  while (const base::State *st = pis_.nextStart())
105  {
106  Motion *motion = new Motion(si_);
107  si_->copyState(motion->state, st);
108  projectionEvaluator_->computeCoordinates(motion->state, xcoord);
109  disc_.addMotion(motion, xcoord, 1.0);
110  }
111 
112  if (disc_.getMotionCount() == 0)
113  {
114  logError("There are no valid initial states!");
116  }
117 
118  if (!sampler_)
119  sampler_ = si_->allocStateSampler();
120 
121  logInform("Starting with %u states", disc_.getMotionCount());
122 
123  Motion *solution = NULL;
124  Motion *approxsol = NULL;
125  double approxdif = std::numeric_limits<double>::infinity();
126  base::State *xstate = si_->allocState();
127 
128  while (ptc() == false)
129  {
130  disc_.countIteration();
131 
132  /* Decide on a state to expand from */
133  Motion *existing = NULL;
134  Discretization<Motion>::Cell *ecell = NULL;
135  disc_.selectMotion(existing, ecell);
136  assert(existing);
137 
138  /* sample random state (with goal biasing) */
139  if (goal_s && rng_.uniform01() < goalBias_ && goal_s->canSample())
140  goal_s->sampleGoal(xstate);
141  else
142  sampler_->sampleUniformNear(xstate, existing->state, maxDistance_);
143 
144  std::pair<base::State*, double> fail(xstate, 0.0);
145  bool keep = si_->checkMotion(existing->state, xstate, fail);
146  if (!keep && fail.second > minValidPathFraction_)
147  keep = true;
148 
149  if (keep)
150  {
151  /* create a motion */
152  Motion *motion = new Motion(si_);
153  si_->copyState(motion->state, xstate);
154  motion->parent = existing;
155 
156  double dist = 0.0;
157  bool solv = goal->isSatisfied(motion->state, &dist);
158  projectionEvaluator_->computeCoordinates(motion->state, xcoord);
159  disc_.addMotion(motion, xcoord, dist); // this will also update the discretization heaps as needed, so no call to updateCell() is needed
160 
161  if (solv)
162  {
163  approxdif = dist;
164  solution = motion;
165  break;
166  }
167  if (dist < approxdif)
168  {
169  approxdif = dist;
170  approxsol = motion;
171  }
172  }
173  else
174  ecell->data->score *= failedExpansionScoreFactor_;
175  disc_.updateCell(ecell);
176  }
177 
178  bool solved = false;
179  bool approximate = false;
180  if (solution == NULL)
181  {
182  solution = approxsol;
183  approximate = true;
184  }
185 
186  if (solution != NULL)
187  {
188  lastGoalMotion_ = solution;
189 
190  /* construct the solution path */
191  std::vector<Motion*> mpath;
192  while (solution != NULL)
193  {
194  mpath.push_back(solution);
195  solution = solution->parent;
196  }
197 
198  /* set the solution path */
199  PathGeometric *path = new PathGeometric(si_);
200  for (int i = mpath.size() - 1 ; i >= 0 ; --i)
201  path->append(mpath[i]->state);
202  pdef_->addSolutionPath(base::PathPtr(path), approximate, approxdif);
203  solved = true;
204  }
205 
206  si_->freeState(xstate);
207 
208  logInform("Created %u states in %u cells (%u internal + %u external)", disc_.getMotionCount(), disc_.getCellCount(),
209  disc_.getGrid().countInternal(), disc_.getGrid().countExternal());
210 
211  return base::PlannerStatus(solved, approximate);
212 }
213 
215 {
216  Planner::getPlannerData(data);
217  disc_.getPlannerData(data, 0, true, lastGoalMotion_);
218 }