Loading...
Searching...
No Matches
LBKPIECE1.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/LBKPIECE1.h"
38#include "ompl/base/goals/GoalSampleableRegion.h"
39#include "ompl/tools/config/SelfConfig.h"
40#include <cassert>
41
42ompl::geometric::LBKPIECE1::LBKPIECE1(const base::SpaceInformationPtr &si)
43 : base::Planner(si, "LBKPIECE1")
44 , dStart_([this](Motion *m) { freeMotion(m); })
45 , dGoal_([this](Motion *m) { freeMotion(m); })
46{
47 specs_.recognizedGoal = base::GOAL_SAMPLEABLE_REGION;
48
49 Planner::declareParam<double>("range", this, &LBKPIECE1::setRange, &LBKPIECE1::getRange, "0.:1.:10000");
50 Planner::declareParam<double>("border_fraction", this, &LBKPIECE1::setBorderFraction, &LBKPIECE1::getBorderFraction,
51 "0.:.05:1.");
52 Planner::declareParam<double>("min_valid_path_fraction", this, &LBKPIECE1::setMinValidPathFraction,
54}
55
56ompl::geometric::LBKPIECE1::~LBKPIECE1() = default;
57
59{
60 Planner::setup();
64
65 if (minValidPathFraction_ < std::numeric_limits<double>::epsilon() || minValidPathFraction_ > 1.0)
66 throw Exception("The minimum valid path fraction must be in the range (0,1]");
67
68 dStart_.setDimension(projectionEvaluator_->getDimension());
69 dGoal_.setDimension(projectionEvaluator_->getDimension());
70}
71
73{
75 auto *goal = dynamic_cast<base::GoalSampleableRegion *>(pdef_->getGoal().get());
76
77 if (goal == nullptr)
78 {
79 OMPL_ERROR("%s: Unknown type of goal", getName().c_str());
81 }
82
84
85 while (const base::State *st = pis_.nextStart())
86 {
87 auto *motion = new Motion(si_);
88 si_->copyState(motion->state, st);
89 motion->root = st;
90 motion->valid = true;
91 projectionEvaluator_->computeCoordinates(motion->state, xcoord);
92 dStart_.addMotion(motion, xcoord);
93 }
94
95 if (dStart_.getMotionCount() == 0)
96 {
97 OMPL_ERROR("%s: Motion planning start tree could not be initialized!", getName().c_str());
99 }
100
101 if (!goal->couldSample())
102 {
103 OMPL_ERROR("%s: Insufficient states in sampleable goal region", getName().c_str());
105 }
106
107 if (!sampler_)
108 sampler_ = si_->allocStateSampler();
109
110 OMPL_INFORM("%s: Starting planning with %d states already in datastructure", getName().c_str(),
111 (int)(dStart_.getMotionCount() + dGoal_.getMotionCount()));
112
113 base::State *xstate = si_->allocState();
114 bool startTree = true;
115 bool solved = false;
117
118 while (!ptc)
119 {
120 Discretization<Motion> &disc = startTree ? dStart_ : dGoal_;
121 startTree = !startTree;
122 Discretization<Motion> &otherDisc = startTree ? dStart_ : dGoal_;
123 disc.countIteration();
124
125 // if we have not sampled too many goals already
126 if (dGoal_.getMotionCount() == 0 || pis_.getSampledGoalsCount() < dGoal_.getMotionCount() / 2)
127 {
128 const base::State *st = dGoal_.getMotionCount() == 0 ? pis_.nextGoal(ptc) : pis_.nextGoal();
129 if (st != nullptr)
130 {
131 auto *motion = new Motion(si_);
132 si_->copyState(motion->state, st);
133 motion->root = motion->state;
134 motion->valid = true;
135 projectionEvaluator_->computeCoordinates(motion->state, xcoord);
136 dGoal_.addMotion(motion, xcoord);
137 }
138 if (dGoal_.getMotionCount() == 0)
139 {
140 OMPL_ERROR("%s: Unable to sample any valid states for goal tree", getName().c_str());
142 break;
143 }
144 }
145
146 Discretization<Motion>::Cell *ecell = nullptr;
147 Motion *existing = nullptr;
148 disc.selectMotion(existing, ecell);
149 assert(existing);
150 sampler_->sampleUniformNear(xstate, existing->state, maxDistance_);
151
152 /* create a motion */
153 auto *motion = new Motion(si_);
154 si_->copyState(motion->state, xstate);
155 motion->parent = existing;
156 motion->root = existing->root;
157 existing->children.push_back(motion);
158 projectionEvaluator_->computeCoordinates(motion->state, xcoord);
159 disc.addMotion(motion, xcoord);
160
161 /* attempt to connect trees */
162 Discretization<Motion>::Cell *ocell = otherDisc.getGrid().getCell(xcoord);
163 if ((ocell != nullptr) && !ocell->data->motions.empty())
164 {
165 Motion *connectOther = ocell->data->motions[rng_.uniformInt(0, ocell->data->motions.size() - 1)];
166
167 if (goal->isStartGoalPairValid(startTree ? connectOther->root : motion->root,
168 startTree ? motion->root : connectOther->root))
169 {
170 auto *connect = new Motion(si_);
171 si_->copyState(connect->state, connectOther->state);
172 connect->parent = motion;
173 connect->root = motion->root;
174 motion->children.push_back(connect);
175 projectionEvaluator_->computeCoordinates(connect->state, xcoord);
176 disc.addMotion(connect, xcoord);
177
178 if (isPathValid(disc, connect, xstate) && isPathValid(otherDisc, connectOther, xstate))
179 {
180 if (startTree)
181 connectionPoint_ = std::make_pair(connectOther->state, motion->state);
182 else
183 connectionPoint_ = std::make_pair(motion->state, connectOther->state);
184
185 /* extract the motions and put them in solution vector */
186
187 std::vector<Motion *> mpath1;
188 while (motion != nullptr)
189 {
190 mpath1.push_back(motion);
191 motion = motion->parent;
192 }
193
194 std::vector<Motion *> mpath2;
195 while (connectOther != nullptr)
196 {
197 mpath2.push_back(connectOther);
198 connectOther = connectOther->parent;
199 }
200
201 if (startTree)
202 mpath1.swap(mpath2);
203
204 auto path(std::make_shared<PathGeometric>(si_));
205 path->getStates().reserve(mpath1.size() + mpath2.size());
206 for (int i = mpath1.size() - 1; i >= 0; --i)
207 path->append(mpath1[i]->state);
208 for (auto &i : mpath2)
209 path->append(i->state);
210
211 pdef_->addSolutionPath(path, false, 0.0, getName());
212 solved = true;
213 break;
214 }
215 }
216 }
217 }
218
219 si_->freeState(xstate);
220
221 OMPL_INFORM("%s: Created %u (%u start + %u goal) states in %u cells (%u start (%u on boundary) + %u goal (%u on "
222 "boundary))",
223 getName().c_str(), dStart_.getMotionCount() + dGoal_.getMotionCount(), dStart_.getMotionCount(),
224 dGoal_.getMotionCount(), dStart_.getCellCount() + dGoal_.getCellCount(), dStart_.getCellCount(),
225 dStart_.getGrid().countExternal(), dGoal_.getCellCount(), dGoal_.getGrid().countExternal());
226
227 return solved ? base::PlannerStatus::EXACT_SOLUTION : status;
228}
229
231{
232 std::vector<Motion *> mpath;
233
234 /* construct the solution path */
235 while (motion != nullptr)
236 {
237 mpath.push_back(motion);
238 motion = motion->parent;
239 }
240
241 std::pair<base::State *, double> lastValid;
242 lastValid.first = temp;
243
244 /* check the path */
245 for (int i = mpath.size() - 1; i >= 0; --i)
246 if (!mpath[i]->valid)
247 {
248 if (si_->checkMotion(mpath[i]->parent->state, mpath[i]->state, lastValid))
249 mpath[i]->valid = true;
250 else
251 {
252 Motion *parent = mpath[i]->parent;
253 removeMotion(disc, mpath[i]);
254
255 // add the valid part of the path, if sufficiently long
256 if (lastValid.second > minValidPathFraction_)
257 {
258 auto *reAdd = new Motion(si_);
259 si_->copyState(reAdd->state, lastValid.first);
260 reAdd->parent = parent;
261 reAdd->root = parent->root;
262 parent->children.push_back(reAdd);
263 reAdd->valid = true;
265 projectionEvaluator_->computeCoordinates(reAdd->state, coord);
266 disc.addMotion(reAdd, coord);
267 }
268
269 return false;
270 }
271 }
272 return true;
273}
274
276{
277 /* remove from grid */
278
280 projectionEvaluator_->computeCoordinates(motion->state, coord);
281 disc.removeMotion(motion, coord);
282
283 /* remove self from parent list */
284
285 if (motion->parent != nullptr)
286 {
287 for (unsigned int i = 0; i < motion->parent->children.size(); ++i)
288 if (motion->parent->children[i] == motion)
289 {
290 motion->parent->children.erase(motion->parent->children.begin() + i);
291 break;
292 }
293 }
294
295 /* remove children */
296 for (auto &i : motion->children)
297 {
298 i->parent = nullptr;
299 removeMotion(disc, i);
300 }
301
302 freeMotion(motion);
303}
304
306{
307 if (motion->state != nullptr)
308 si_->freeState(motion->state);
309 delete motion;
310}
311
313{
314 Planner::clear();
315
316 sampler_.reset();
317 dStart_.clear();
318 dGoal_.clear();
319 connectionPoint_ = std::make_pair<base::State *, base::State *>(nullptr, nullptr);
320}
321
323{
324 Planner::getPlannerData(data);
325 dStart_.getPlannerData(data, 1, true, nullptr);
326 dGoal_.getPlannerData(data, 2, false, nullptr);
327
328 // Insert the edge connecting the two trees
329 data.addEdge(data.vertexIndex(connectionPoint_.first), data.vertexIndex(connectionPoint_.second));
330}
The exception type for ompl.
Definition Exception.h:47
Cell * getCell(const Coord &coord) const
Get the cell at a specified coordinate.
Definition GridN.h:123
Abstract definition of a goal region that can be sampled.
Object containing planner generated vertex and edge data. It is assumed that all vertices are unique,...
unsigned int vertexIndex(const PlannerDataVertex &v) const
Return the index for the vertex associated with the given data. INVALID_INDEX is returned if this ver...
virtual bool addEdge(unsigned int v1, unsigned int v2, const PlannerDataEdge &edge=PlannerDataEdge(), Cost weight=Cost(1.0))
Adds a directed edge between the given vertex indexes. An optional edge structure and weight can be s...
Encapsulate a termination condition for a motion planner. Planners will call operator() to decide whe...
PlannerInputStates pis_
Utility class to extract valid input states.
Definition Planner.h:407
ProblemDefinitionPtr pdef_
The user set problem definition.
Definition Planner.h:404
const std::string & getName() const
Get the name of the planner.
Definition Planner.cpp:56
SpaceInformationPtr si_
The space information for which planning is done.
Definition Planner.h:401
virtual void checkValidity()
Check to see if the planner is in a working state (setup has been called, a goal was set,...
Definition Planner.cpp:106
Definition of an abstract state.
Definition State.h:50
One-level discretization used for KPIECE.
typename Grid::Coord Coord
The datatype for the maintained grid coordinates.
unsigned int addMotion(Motion *motion, const Coord &coord, double dist=0.0)
Add a motion to the grid containing motions. As a hint, dist specifies the distance to the goal from ...
typename Grid::Cell Cell
The datatype for the maintained grid cells.
void selectMotion(Motion *&smotion, Cell *&scell)
Select a motion and the cell it is part of from the grid of motions. This is where preference is give...
Representation of a motion for this algorithm.
Definition LBKPIECE1.h:167
std::vector< Motion * > children
The set of motions descending from the current motion.
Definition LBKPIECE1.h:191
Motion * parent
The parent motion in the exploration tree.
Definition LBKPIECE1.h:185
const base::State * root
The root state (start state) that leads to this motion.
Definition LBKPIECE1.h:179
base::State * state
The state contained by this motion.
Definition LBKPIECE1.h:182
double getBorderFraction() const
Get the fraction of time to focus exploration on boundary.
Definition LBKPIECE1.h:135
base::ProjectionEvaluatorPtr projectionEvaluator_
The employed projection evaluator.
Definition LBKPIECE1.h:211
double maxDistance_
The maximum length of a motion to be added to a tree.
Definition LBKPIECE1.h:227
void setMinValidPathFraction(double fraction)
When extending a motion, the planner can decide to keep the first valid part of it,...
Definition LBKPIECE1.h:146
void removeMotion(Discretization< Motion > &disc, Motion *motion)
Remove a motion from a tree of motions.
double getRange() const
Get the range the planner is using.
Definition LBKPIECE1.h:117
std::pair< base::State *, base::State * > connectionPoint_
The pair of states in each tree connected during planning. Used for PlannerData computation.
Definition LBKPIECE1.h:233
void clear() override
Clear all internal datastructures. Planner settings are not affected. Subsequent calls to solve() wil...
double minValidPathFraction_
When extending a motion, the planner can decide to keep the first valid part of it,...
Definition LBKPIECE1.h:224
double getMinValidPathFraction() const
Get the value of the fraction set by setMinValidPathFraction().
Definition LBKPIECE1.h:152
RNG rng_
The random number generator.
Definition LBKPIECE1.h:230
void getPlannerData(base::PlannerData &data) const override
Get information about the current run of the motion planner. Repeated calls to this function will upd...
void setBorderFraction(double bp)
Set the fraction of time for focusing on the border (between 0 and 1). This is the minimum fraction u...
Definition LBKPIECE1.h:127
void setup() override
Perform extra configuration steps, if needed. This call will also issue a call to ompl::base::SpaceIn...
Definition LBKPIECE1.cpp:58
void setRange(double distance)
Set the range the planner is supposed to use.
Definition LBKPIECE1.h:111
LBKPIECE1(const base::SpaceInformationPtr &si)
Constructor.
Definition LBKPIECE1.cpp:42
bool isPathValid(Discretization< Motion > &disc, Motion *motion, base::State *temp)
Since solutions are computed in a lazy fashion, once trees are connected, the solution found needs to...
void freeMotion(Motion *motion)
Free the memory for a motion.
base::StateSamplerPtr sampler_
The employed state sampler.
Definition LBKPIECE1.h:208
Discretization< Motion > dStart_
The start tree.
Definition LBKPIECE1.h:214
base::PlannerStatus solve(const base::PlannerTerminationCondition &ptc) override
Function that can solve the motion planning problem. This function can be called multiple times on th...
Definition LBKPIECE1.cpp:72
Discretization< Motion > dGoal_
The goal tree.
Definition LBKPIECE1.h:217
This class contains methods that automatically configure various parameters for motion planning....
Definition SelfConfig.h:59
void configurePlannerRange(double &range)
Compute what a good length for motion segments is.
void configureProjectionEvaluator(base::ProjectionEvaluatorPtr &proj)
If proj is undefined, it is set to the default projection reported by base::StateSpace::getDefaultPro...
#define OMPL_INFORM(fmt,...)
Log a formatted information string.
Definition Console.h:68
#define OMPL_ERROR(fmt,...)
Log a formatted error string.
Definition Console.h:64
This namespace contains sampling based planning routines shared by both planning under geometric cons...
@ GOAL_SAMPLEABLE_REGION
This bit is set if casting to sampleable goal regions (ompl::base::GoalSampleableRegion) is possible.
Definition GoalTypes.h:56
A class to store the exit status of Planner::solve().
StatusType
The possible values of the status returned by a planner.
@ INVALID_START
Invalid start state or no start state specified.
@ EXACT_SOLUTION
The planner found an exact solution.
@ INVALID_GOAL
Invalid goal state.
@ UNRECOGNIZED_GOAL_TYPE
The goal is of a type that a planner does not recognize.
@ TIMEOUT
The planner failed to find a solution.