Loading...
Searching...
No Matches
LazyRRT.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/LazyRRT.h"
38#include "ompl/base/goals/GoalSampleableRegion.h"
39#include "ompl/tools/config/SelfConfig.h"
40#include <cassert>
41
42ompl::geometric::LazyRRT::LazyRRT(const base::SpaceInformationPtr &si) : base::Planner(si, "LazyRRT")
43{
44 specs_.directed = true;
45
46 Planner::declareParam<double>("range", this, &LazyRRT::setRange, &LazyRRT::getRange, "0.:1.:10000.");
47 Planner::declareParam<double>("goal_bias", this, &LazyRRT::setGoalBias, &LazyRRT::getGoalBias, "0.:.05:1.");
48}
49
50ompl::geometric::LazyRRT::~LazyRRT()
51{
52 freeMemory();
53}
54
56{
57 Planner::setup();
60
61 if (!nn_)
63 nn_->setDistanceFunction([this](const Motion *a, const Motion *b) { return distanceFunction(a, b); });
64}
65
67{
68 Planner::clear();
69 sampler_.reset();
70 freeMemory();
71 if (nn_)
72 nn_->clear();
73 lastGoalMotion_ = nullptr;
74}
75
77{
78 if (nn_)
79 {
80 std::vector<Motion *> motions;
81 nn_->list(motions);
82 for (auto &motion : motions)
83 {
84 if (motion->state != nullptr)
85 si_->freeState(motion->state);
86 delete motion;
87 }
88 }
89}
90
92{
94 base::Goal *goal = pdef_->getGoal().get();
95 auto *goal_s = dynamic_cast<base::GoalSampleableRegion *>(goal);
96
97 while (const base::State *st = pis_.nextStart())
98 {
99 auto *motion = new Motion(si_);
100 si_->copyState(motion->state, st);
101 motion->valid = true;
102 nn_->add(motion);
103 }
104
105 if (nn_->size() == 0)
106 {
107 OMPL_ERROR("%s: There are no valid initial states!", getName().c_str());
109 }
110
111 if (!sampler_)
112 sampler_ = si_->allocStateSampler();
113
114 OMPL_INFORM("%s: Starting planning with %u states already in datastructure", getName().c_str(), nn_->size());
115
116 Motion *solution = nullptr;
117 double distsol = -1.0;
118 auto *rmotion = new Motion(si_);
119 base::State *rstate = rmotion->state;
120 base::State *xstate = si_->allocState();
121
122 bool solutionFound = false;
123
124 while (!ptc && !solutionFound)
125 {
126 /* sample random state (with goal biasing) */
127 if ((goal_s != nullptr) && rng_.uniform01() < goalBias_ && goal_s->canSample())
128 goal_s->sampleGoal(rstate);
129 else
130 sampler_->sampleUniform(rstate);
131
132 /* find closest state in the tree */
133 Motion *nmotion = nn_->nearest(rmotion);
134 assert(nmotion != rmotion);
135 base::State *dstate = rstate;
136
137 /* find state to add */
138 double d = si_->distance(nmotion->state, rstate);
139 if (d > maxDistance_)
140 {
141 si_->getStateSpace()->interpolate(nmotion->state, rstate, maxDistance_ / d, xstate);
142 dstate = xstate;
143 }
144
145 /* create a motion */
146 auto *motion = new Motion(si_);
147 si_->copyState(motion->state, dstate);
148 motion->parent = nmotion;
149 nmotion->children.push_back(motion);
150 nn_->add(motion);
151
152 double dist = 0.0;
153 if (goal->isSatisfied(motion->state, &dist))
154 {
155 distsol = dist;
156 solution = motion;
157 solutionFound = true;
158 lastGoalMotion_ = solution;
159
160 // Check that the solution is valid:
161 // construct the solution path
162 std::vector<Motion *> mpath;
163 while (solution != nullptr)
164 {
165 mpath.push_back(solution);
166 solution = solution->parent;
167 }
168
169 // check each segment along the path for validity
170 for (int i = mpath.size() - 1; i >= 0 && solutionFound; --i)
171 if (!mpath[i]->valid)
172 {
173 if (si_->checkMotion(mpath[i]->parent->state, mpath[i]->state))
174 mpath[i]->valid = true;
175 else
176 {
177 removeMotion(mpath[i]);
178 solutionFound = false;
179 lastGoalMotion_ = nullptr;
180 }
181 }
182
183 if (solutionFound)
184 {
185 // set the solution path
186 auto path(std::make_shared<PathGeometric>(si_));
187 for (int i = mpath.size() - 1; i >= 0; --i)
188 path->append(mpath[i]->state);
189
190 pdef_->addSolutionPath(path, false, distsol, getName());
191 }
192 }
193 }
194
195 si_->freeState(xstate);
196 si_->freeState(rstate);
197 delete rmotion;
198
199 OMPL_INFORM("%s: Created %u states", getName().c_str(), nn_->size());
200
202}
203
205{
206 nn_->remove(motion);
207
208 /* remove self from parent list */
209
210 if (motion->parent != nullptr)
211 {
212 for (unsigned int i = 0; i < motion->parent->children.size(); ++i)
213 if (motion->parent->children[i] == motion)
214 {
215 motion->parent->children.erase(motion->parent->children.begin() + i);
216 break;
217 }
218 }
219
220 /* remove children */
221 for (auto &i : motion->children)
222 {
223 i->parent = nullptr;
224 removeMotion(i);
225 }
226
227 if (motion->state != nullptr)
228 si_->freeState(motion->state);
229 delete motion;
230}
231
233{
234 Planner::getPlannerData(data);
235
236 std::vector<Motion *> motions;
237 if (nn_)
238 nn_->list(motions);
239
240 if (lastGoalMotion_ != nullptr)
242
243 for (auto &motion : motions)
244 {
245 if (motion->parent == nullptr)
246 data.addStartVertex(base::PlannerDataVertex(motion->state));
247 else
248 data.addEdge(base::PlannerDataVertex(motion->parent != nullptr ? motion->parent->state : nullptr),
249 base::PlannerDataVertex(motion->state));
250
251 data.tagState(motion->state, motion->valid ? 1 : 0);
252 }
253}
Abstract definition of a goal region that can be sampled.
Abstract definition of goals.
Definition Goal.h:63
virtual bool isSatisfied(const State *st) const =0
Return true if the state satisfies the goal constraints.
Base class for a vertex in the PlannerData structure. All derived classes must implement the clone an...
Definition PlannerData.h:59
Object containing planner generated vertex and edge data. It is assumed that all vertices are unique,...
bool tagState(const State *st, int tag)
Set the integer tag associated with the given state. If the given state does not exist in a vertex,...
unsigned int addStartVertex(const PlannerDataVertex &v)
Adds the given vertex to the graph data, and marks it as a start vertex. The vertex index is returned...
unsigned int addGoalVertex(const PlannerDataVertex &v)
Adds the given vertex to the graph data, and marks it as a start vertex. The vertex index is returned...
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
PlannerSpecs specs_
The specifications of the planner (its capabilities).
Definition Planner.h:413
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
Representation of a motion.
Definition LazyRRT.h:147
std::vector< Motion * > children
The set of motions that descend from this one.
Definition LazyRRT.h:168
Motion * parent
The parent motion in the exploration tree.
Definition LazyRRT.h:162
base::State * state
The state contained by the motion.
Definition LazyRRT.h:159
double goalBias_
The fraction of time the goal is picked as the state to expand towards (if such a state is available)...
Definition LazyRRT.h:191
void setGoalBias(double goalBias)
Set the goal biasing.
Definition LazyRRT.h:104
LazyRRT(const base::SpaceInformationPtr &si)
Constructor.
Definition LazyRRT.cpp:42
void setRange(double distance)
Set the range the planner is supposed to use.
Definition LazyRRT.h:120
Motion * lastGoalMotion_
The most recent goal motion. Used for PlannerData computation.
Definition LazyRRT.h:200
void removeMotion(Motion *motion)
Remove a motion from the tree datastructure.
Definition LazyRRT.cpp:204
double getGoalBias() const
Get the goal bias the planner is using.
Definition LazyRRT.h:110
base::StateSamplerPtr sampler_
State sampler.
Definition LazyRRT.h:184
double maxDistance_
The maximum length of a motion to be added to a tree.
Definition LazyRRT.h:194
void getPlannerData(base::PlannerData &data) const override
Get information about the current run of the motion planner. Repeated calls to this function will upd...
Definition LazyRRT.cpp:232
double getRange() const
Get the range the planner is using.
Definition LazyRRT.h:126
RNG rng_
The random number generator.
Definition LazyRRT.h:197
double distanceFunction(const Motion *a, const Motion *b) const
Compute distance between motions (actually distance between contained states).
Definition LazyRRT.h:178
std::shared_ptr< NearestNeighbors< Motion * > > nn_
A nearest-neighbors datastructure containing the tree of motions.
Definition LazyRRT.h:187
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 LazyRRT.cpp:91
void setup() override
Perform extra configuration steps, if needed. This call will also issue a call to ompl::base::SpaceIn...
Definition LazyRRT.cpp:55
void freeMemory()
Free the memory allocated by this planner.
Definition LazyRRT.cpp:76
void clear() override
Clear all internal datastructures. Planner settings are not affected. Subsequent calls to solve() wil...
Definition LazyRRT.cpp:66
This class contains methods that automatically configure various parameters for motion planning....
Definition SelfConfig.h:59
static NearestNeighbors< _T > * getDefaultNearestNeighbors(const base::Planner *planner)
Select a default nearest neighbor datastructure for the given space.
Definition SelfConfig.h:105
void configurePlannerRange(double &range)
Compute what a good length for motion segments is.
#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...
A class to store the exit status of Planner::solve().
@ INVALID_START
Invalid start state or no start state specified.
@ EXACT_SOLUTION
The planner found an exact solution.
@ TIMEOUT
The planner failed to find a solution.