Loading...
Searching...
No Matches
STRIDE.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: Bryant Gipson, Mark Moll, Ioan Sucan */
36
37#include "ompl/geometric/planners/stride/STRIDE.h"
38// enable sampling from the GNAT data structure
39#define GNAT_SAMPLER
40#include "ompl/datastructures/NearestNeighborsGNAT.h"
41#include "ompl/base/goals/GoalSampleableRegion.h"
42#include "ompl/tools/config/SelfConfig.h"
43#include <limits>
44#include <cassert>
45
46ompl::geometric::STRIDE::STRIDE(const base::SpaceInformationPtr &si, bool useProjectedDistance, unsigned int degree,
47 unsigned int minDegree, unsigned int maxDegree, unsigned int maxNumPtsPerLeaf,
48 double estimatedDimension)
49 : base::Planner(si, "STRIDE")
50 , useProjectedDistance_(useProjectedDistance)
51 , degree_(degree)
52 , minDegree_(minDegree)
53 , maxDegree_(maxDegree)
54 , maxNumPtsPerLeaf_(maxNumPtsPerLeaf)
55 , estimatedDimension_(estimatedDimension)
56{
57 specs_.approximateSolutions = true;
58
59 if (estimatedDimension_ < 1.)
60 estimatedDimension_ = si->getStateDimension();
61
62 Planner::declareParam<double>("range", this, &STRIDE::setRange, &STRIDE::getRange, "0.:1.:10000.");
63 Planner::declareParam<double>("goal_bias", this, &STRIDE::setGoalBias, &STRIDE::getGoalBias, "0.:.05:1.");
64 Planner::declareParam<bool>("use_projected_distance", this, &STRIDE::setUseProjectedDistance,
66 Planner::declareParam<unsigned int>("degree", this, &STRIDE::setDegree, &STRIDE::getDegree, "2:20");
67 Planner::declareParam<unsigned int>("max_degree", this, &STRIDE::setMaxDegree, &STRIDE::getMaxDegree, "2:20");
68 Planner::declareParam<unsigned int>("min_degree", this, &STRIDE::setMinDegree, &STRIDE::getMinDegree, "2:20");
69 Planner::declareParam<unsigned int>("max_pts_per_leaf", this, &STRIDE::setMaxNumPtsPerLeaf,
71 Planner::declareParam<double>("estimated_dimension", this, &STRIDE::setEstimatedDimension,
73 Planner::declareParam<double>("min_valid_path_fraction", this, &STRIDE::setMinValidPathFraction,
75}
76
77ompl::geometric::STRIDE::~STRIDE()
78{
79 freeMemory();
80}
81
90
92{
93 tree_.reset(
96 tree_->setDistanceFunction([this](const Motion *a, const Motion *b)
97 { return projectedDistanceFunction(a, b); });
98 else
99 tree_->setDistanceFunction([this](const Motion *a, const Motion *b) { return distanceFunction(a, b); });
100}
101
103{
104 Planner::clear();
105 sampler_.reset();
106 freeMemory();
107 setupTree();
108}
109
111{
112 if (tree_)
113 {
114 std::vector<Motion *> motions;
115 tree_->list(motions);
116 for (auto &motion : motions)
117 {
118 if (motion->state)
119 si_->freeState(motion->state);
120 delete motion;
121 }
122 tree_.reset();
123 }
124}
125
127{
129 base::Goal *goal = pdef_->getGoal().get();
130 auto *goal_s = dynamic_cast<base::GoalSampleableRegion *>(goal);
131
132 while (const base::State *st = pis_.nextStart())
133 {
134 auto *motion = new Motion(si_);
135 si_->copyState(motion->state, st);
136 addMotion(motion);
137 }
138
139 if (tree_->size() == 0)
140 {
141 OMPL_ERROR("%s: There are no valid initial states!", getName().c_str());
143 }
144
145 if (!sampler_)
146 sampler_ = si_->allocValidStateSampler();
147
148 OMPL_INFORM("%s: Starting planning with %u states already in datastructure", getName().c_str(), tree_->size());
149
150 Motion *solution = nullptr;
151 Motion *approxsol = nullptr;
152 double approxdif = std::numeric_limits<double>::infinity();
153 base::State *xstate = si_->allocState();
154
155 while (ptc == false)
156 {
157 /* Decide on a state to expand from */
158 Motion *existing = selectMotion();
159 assert(existing);
160
161 /* sample random state (with goal biasing) */
162 if (goal_s && rng_.uniform01() < goalBias_ && goal_s->canSample())
163 goal_s->sampleGoal(xstate);
164 else if (!sampler_->sampleNear(xstate, existing->state, maxDistance_))
165 continue;
166
167 std::pair<base::State *, double> fail(xstate, 0.0);
168 bool keep = si_->checkMotion(existing->state, xstate, fail) || fail.second > minValidPathFraction_;
169
170 if (keep)
171 {
172 /* create a motion */
173 auto *motion = new Motion(si_);
174 si_->copyState(motion->state, xstate);
175 motion->parent = existing;
176
177 addMotion(motion);
178 double dist = 0.0;
179 bool solved = goal->isSatisfied(motion->state, &dist);
180 if (solved)
181 {
182 approxdif = dist;
183 solution = motion;
184 break;
185 }
186 if (dist < approxdif)
187 {
188 approxdif = dist;
189 approxsol = motion;
190 }
191 }
192 }
193
194 bool solved = false;
195 bool approximate = false;
196 if (solution == nullptr)
197 {
198 solution = approxsol;
199 approximate = true;
200 }
201
202 if (solution != nullptr)
203 {
204 /* construct the solution path */
205 std::vector<Motion *> mpath;
206 while (solution != nullptr)
207 {
208 mpath.push_back(solution);
209 solution = solution->parent;
210 }
211
212 /* set the solution path */
213 auto path(std::make_shared<PathGeometric>(si_));
214 for (int i = mpath.size() - 1; i >= 0; --i)
215 path->append(mpath[i]->state);
216 pdef_->addSolutionPath(path, approximate, approxdif, getName());
217 solved = true;
218 }
219
220 si_->freeState(xstate);
221
222 OMPL_INFORM("%s: Created %u states", getName().c_str(), tree_->size());
223
224 return {solved, approximate};
225}
226
228{
229 tree_->add(motion);
230}
231
236
238{
239 Planner::getPlannerData(data);
240
241 std::vector<Motion *> motions;
242 tree_->list(motions);
243 for (auto &motion : motions)
244 {
245 if (motion->parent == nullptr)
246 data.addStartVertex(base::PlannerDataVertex(motion->state, 1));
247 else
248 data.addEdge(base::PlannerDataVertex(motion->parent->state, 1), base::PlannerDataVertex(motion->state, 1));
249 }
250}
Geometric Near-neighbor Access Tree (GNAT), a data structure for nearest neighbor search.
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,...
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...
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
The definition of a motion.
Definition STRIDE.h:239
Motion * parent
The parent motion in the exploration tree.
Definition STRIDE.h:254
base::State * state
The state contained by the motion.
Definition STRIDE.h:251
double minValidPathFraction_
When extending a motion, the planner can decide to keep the first valid part of it,...
Definition STRIDE.h:319
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 STRIDE.cpp:237
void setupTree()
Initialize GNAT data structure.
Definition STRIDE.cpp:91
base::ProjectionEvaluatorPtr projectionEvaluator_
This algorithm can optionally use a projection to guide the exploration.
Definition STRIDE.h:289
double getMinValidPathFraction() const
Get the value of the fraction set by setMinValidPathFraction().
Definition STRIDE.h:210
void setDegree(unsigned int degree)
Set desired degree of a node in the GNAT.
Definition STRIDE.h:127
void setMinDegree(unsigned int minDegree)
Set minimum degree of a node in the GNAT.
Definition STRIDE.h:137
void setUseProjectedDistance(bool useProjectedDistance)
Set whether nearest neighbors are computed based on distances in a projection of the state rather dis...
Definition STRIDE.h:114
void setEstimatedDimension(double estimatedDimension)
Set estimated dimension of the free space, which is needed to compute the sampling weight for a node ...
Definition STRIDE.h:171
void clear() override
Clear all internal datastructures. Planner settings are not affected. Subsequent calls to solve() wil...
Definition STRIDE.cpp:102
unsigned int degree_
Desired degree of an internal node in the GNAT.
Definition STRIDE.h:305
void setRange(double distance)
Set the range the planner is supposed to use.
Definition STRIDE.h:188
STRIDE(const base::SpaceInformationPtr &si, bool useProjectedDistance=false, unsigned int degree=16, unsigned int minDegree=12, unsigned int maxDegree=18, unsigned int maxNumPtsPerLeaf=6, double estimatedDimension=0.0)
Constructor.
Definition STRIDE.cpp:46
base::ValidStateSamplerPtr sampler_
Valid state sampler.
Definition STRIDE.h:286
bool getUseProjectedDistance() const
Return whether nearest neighbors are computed based on distances in a projection of the state rather ...
Definition STRIDE.h:121
boost::scoped_ptr< NearestNeighborsGNAT< Motion * > > tree_
The exploration tree constructed by this algorithm.
Definition STRIDE.h:292
double maxDistance_
The maximum length of a motion to be added to a tree.
Definition STRIDE.h:299
double getEstimatedDimension() const
Get estimated dimension of the free space, which is needed to compute the sampling weight for a node ...
Definition STRIDE.h:178
Motion * selectMotion()
Select a motion to continue the expansion of the tree from.
Definition STRIDE.cpp:232
void setup() override
Perform extra configuration steps, if needed. This call will also issue a call to ompl::base::SpaceIn...
Definition STRIDE.cpp:82
RNG rng_
The random number generator.
Definition STRIDE.h:322
void setMinValidPathFraction(double fraction)
When extending a motion, the planner can decide to keep the first valid part of it,...
Definition STRIDE.h:204
double getGoalBias() const
Get the goal bias the planner is using.
Definition STRIDE.h:106
double projectedDistanceFunction(const Motion *a, const Motion *b) const
Compute distance between motions (actually distance between projections of contained states).
Definition STRIDE.h:270
void setGoalBias(double goalBias)
In the process of randomly selecting states in the state space to attempt to go towards,...
Definition STRIDE.h:100
double goalBias_
The fraction of time the goal is picked as the state to expand towards (if such a state is available)...
Definition STRIDE.h:296
double estimatedDimension_
Estimate of the local dimensionality of the free space around a state.
Definition STRIDE.h:313
void addMotion(Motion *motion)
Add a motion to the exploration tree.
Definition STRIDE.cpp:227
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 STRIDE.cpp:126
void setMaxNumPtsPerLeaf(unsigned int maxNumPtsPerLeaf)
Set maximum number of elements stored in a leaf node of the GNAT.
Definition STRIDE.h:158
void setMaxDegree(unsigned int maxDegree)
Set maximum degree of a node in the GNAT.
Definition STRIDE.h:147
unsigned int maxNumPtsPerLeaf_
Maximum number of points stored in a leaf node in the GNAT.
Definition STRIDE.h:311
unsigned int minDegree_
Minimum degree of an internal node in the GNAT.
Definition STRIDE.h:307
double distanceFunction(const Motion *a, const Motion *b) const
Compute distance between motions (actually distance between contained states).
Definition STRIDE.h:264
bool useProjectedDistance_
Whether to use distance in the projection (instead of distance in the state space) for the GNAT.
Definition STRIDE.h:303
double getRange() const
Get the range the planner is using.
Definition STRIDE.h:194
unsigned int getDegree() const
Get desired degree of a node in the GNAT.
Definition STRIDE.h:132
void freeMemory()
Free the memory allocated by this planner.
Definition STRIDE.cpp:110
unsigned int maxDegree_
Maximum degree of an internal node in the GNAT.
Definition STRIDE.h:309
unsigned int getMaxDegree() const
Set maximum degree of a node in the GNAT.
Definition STRIDE.h:152
unsigned int getMaxNumPtsPerLeaf() const
Get maximum number of elements stored in a leaf node of the GNAT.
Definition STRIDE.h:164
unsigned int getMinDegree() const
Get minimum degree of a node in the GNAT.
Definition STRIDE.h:142
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...
A class to store the exit status of Planner::solve().
@ INVALID_START
Invalid start state or no start state specified.