Loading...
Searching...
No Matches
EST.cpp
1/*********************************************************************
2 * Software License Agreement (BSD License)
3 *
4 * Copyright (c) 2015, Rice University
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 Rice University 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: Ryan Luna */
36
37#include "ompl/geometric/planners/est/EST.h"
38#include "ompl/base/goals/GoalSampleableRegion.h"
39#include "ompl/tools/config/SelfConfig.h"
40#include <limits>
41#include <cassert>
42
43ompl::geometric::EST::EST(const base::SpaceInformationPtr &si) : base::Planner(si, "EST")
44{
45 specs_.approximateSolutions = true;
46 specs_.directed = true;
47
48 Planner::declareParam<double>("range", this, &EST::setRange, &EST::getRange, "0.:1.:10000.");
49 Planner::declareParam<double>("goal_bias", this, &EST::setGoalBias, &EST::getGoalBias, "0.:.05:1.");
50}
51
52ompl::geometric::EST::~EST()
53{
54 freeMemory();
55}
56
58{
59 Planner::setup();
62
63 // Make the neighborhood radius smaller than sampling range to keep probabilities relatively high for rejection
64 // sampling
66
67 if (!nn_)
69 nn_->setDistanceFunction([this](const Motion *a, const Motion *b) { return distanceFunction(a, b); });
70}
71
73{
74 Planner::clear();
75 sampler_.reset();
76 freeMemory();
77 if (nn_)
78 nn_->clear();
79
80 motions_.clear();
81 pdf_.clear();
82 lastGoalMotion_ = nullptr;
83}
84
86{
87 for (auto &motion : motions_)
88 {
89 if (motion->state != nullptr)
90 si_->freeState(motion->state);
91 delete motion;
92 }
93}
94
96{
98 base::Goal *goal = pdef_->getGoal().get();
99 auto *goal_s = dynamic_cast<base::GoalSampleableRegion *>(goal);
100
101 if (goal_s == nullptr)
102 {
103 OMPL_ERROR("%s: Unknown type of goal", getName().c_str());
105 }
106
107 if (!goal_s->couldSample())
108 {
109 OMPL_ERROR("%s: Insufficient states in sampleable goal region", getName().c_str());
111 }
112
113 std::vector<Motion *> neighbors;
114
115 while (const base::State *st = pis_.nextStart())
116 {
117 auto *motion = new Motion(si_);
118 si_->copyState(motion->state, st);
119
120 nn_->nearestR(motion, nbrhoodRadius_, neighbors);
121 addMotion(motion, neighbors);
122 }
123
124 if (motions_.empty())
125 {
126 OMPL_ERROR("%s: There are no valid initial states!", getName().c_str());
128 }
129
130 if (!sampler_)
131 sampler_ = si_->allocValidStateSampler();
132
133 OMPL_INFORM("%s: Starting planning with %u states already in datastructure", getName().c_str(), motions_.size());
134
135 Motion *solution = nullptr;
136 Motion *approxsol = nullptr;
137 double approxdif = std::numeric_limits<double>::infinity();
138 base::State *xstate = si_->allocState();
139 auto *xmotion = new Motion();
140
141 while (!ptc)
142 {
143 // Select a state to expand from
144 Motion *existing = pdf_.sample(rng_.uniform01());
145 assert(existing);
146
147 // Sample random state in the neighborhood (with goal biasing)
148 if (rng_.uniform01() < goalBias_ && goal_s->canSample())
149 {
150 goal_s->sampleGoal(xstate);
151
152 // Compute neighborhood of candidate motion
153 xmotion->state = xstate;
154 nn_->nearestR(xmotion, nbrhoodRadius_, neighbors);
155 }
156 else
157 {
158 // Sample a state in the neighborhood
159 if (!sampler_->sampleNear(xstate, existing->state, maxDistance_))
160 continue;
161
162 // Compute neighborhood of candidate state
163 xmotion->state = xstate;
164 nn_->nearestR(xmotion, nbrhoodRadius_, neighbors);
165
166 // reject state with probability proportional to neighborhood density
167 if (!neighbors.empty())
168 {
169 double p = 1.0 - (1.0 / neighbors.size());
170 if (rng_.uniform01() < p)
171 continue;
172 }
173 }
174
175 // Is motion good?
176 if (si_->checkMotion(existing->state, xstate))
177 {
178 // create a motion
179 auto *motion = new Motion(si_);
180 si_->copyState(motion->state, xstate);
181 motion->parent = existing;
182
183 // add it to everything
184 addMotion(motion, neighbors);
185
186 // done?
187 double dist = 0.0;
188 bool solved = goal->isSatisfied(motion->state, &dist);
189 if (solved)
190 {
191 approxdif = dist;
192 solution = motion;
193 break;
194 }
195 if (dist < approxdif)
196 {
197 approxdif = dist;
198 approxsol = motion;
199 }
200 }
201 }
202
203 bool solved = false;
204 bool approximate = false;
205 if (solution == nullptr)
206 {
207 solution = approxsol;
208 approximate = true;
209 }
210
211 if (solution != nullptr)
212 {
213 lastGoalMotion_ = solution;
214
215 // construct the solution path
216 std::vector<Motion *> mpath;
217 while (solution != nullptr)
218 {
219 mpath.push_back(solution);
220 solution = solution->parent;
221 }
222
223 // set the solution path
224 auto path(std::make_shared<PathGeometric>(si_));
225 for (int i = mpath.size() - 1; i >= 0; --i)
226 path->append(mpath[i]->state);
227 pdef_->addSolutionPath(path, approximate, approxdif, getName());
228 solved = true;
229 }
230
231 si_->freeState(xstate);
232 delete xmotion;
233
234 OMPL_INFORM("%s: Created %u states", getName().c_str(), motions_.size());
235
236 return {solved, approximate};
237}
238
239void ompl::geometric::EST::addMotion(Motion *motion, const std::vector<Motion *> &neighbors)
240{
241 // Updating neighborhood size counts
242 for (auto neighbor : neighbors)
243 {
244 PDF<Motion *>::Element *elem = neighbor->element;
245 double w = pdf_.getWeight(elem);
246 pdf_.update(elem, w / (w + 1.));
247 }
248
249 // now add new motion to the data structures
250 motion->element = pdf_.add(motion, 1. / (neighbors.size() + 1.)); // +1 for self
251 motions_.push_back(motion);
252 nn_->add(motion);
253}
254
256{
257 Planner::getPlannerData(data);
258
259 if (lastGoalMotion_ != nullptr)
261
262 for (auto motion : motions_)
263 {
264 if (motion->parent == nullptr)
265 data.addStartVertex(base::PlannerDataVertex(motion->state));
266 else
267 data.addEdge(base::PlannerDataVertex(motion->parent->state), base::PlannerDataVertex(motion->state));
268 }
269}
A class that will hold data contained in the PDF.
Definition PDF.h:53
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...
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
The definition of a motion.
Definition EST.h:121
PDF< Motion * >::Element * element
A pointer to the corresponding element in the probability distribution function.
Definition EST.h:139
base::State * state
The state contained by the motion.
Definition EST.h:133
Motion * parent
The parent motion in the exploration tree.
Definition EST.h:136
EST(const base::SpaceInformationPtr &si)
Constructor.
Definition EST.cpp:43
double getGoalBias() const
Get the goal bias the planner is using.
Definition EST.h:90
PDF< Motion * > pdf_
The probability distribution function over states in the tree.
Definition EST.h:155
void clear() override
Clear all internal datastructures. Planner settings are not affected. Subsequent calls to solve() wil...
Definition EST.cpp:72
double goalBias_
The fraction of time the goal is picked as the state to expand towards (if such a state is available)...
Definition EST.h:168
void setGoalBias(double goalBias)
In the process of randomly selecting states in the state space to attempt to go towards,...
Definition EST.h:84
std::shared_ptr< NearestNeighbors< Motion * > > nn_
A nearest-neighbors datastructure containing the tree of motions.
Definition EST.h:149
std::vector< Motion * > motions_
The set of all states in the tree.
Definition EST.h:152
void setRange(double distance)
Set the range the planner is supposed to use.
Definition EST.h:100
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 EST.cpp:255
void freeMemory()
Free the memory allocated by this planner.
Definition EST.cpp:85
double getRange() const
Get the range the planner is using.
Definition EST.h:109
void setup() override
Perform extra configuration steps, if needed. This call will also issue a call to ompl::base::SpaceIn...
Definition EST.cpp:57
void addMotion(Motion *motion, const std::vector< Motion * > &neighbors)
Add a motion to the exploration tree.
Definition EST.cpp:239
RNG rng_
The random number generator.
Definition EST.h:177
Motion * lastGoalMotion_
The most recent goal motion. Used for PlannerData computation.
Definition EST.h:180
base::ValidStateSamplerPtr sampler_
Valid state sampler.
Definition EST.h:164
double maxDistance_
The maximum length of a motion to be added to a tree.
Definition EST.h:171
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 EST.cpp:95
double nbrhoodRadius_
The radius considered for neighborhood.
Definition EST.h:174
double distanceFunction(const Motion *a, const Motion *b) const
Compute distance between motions (actually distance between contained states).
Definition EST.h:143
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.
@ INVALID_GOAL
Invalid goal state.
@ UNRECOGNIZED_GOAL_TYPE
The goal is of a type that a planner does not recognize.