Loading...
Searching...
No Matches
SyclopRRT.cpp
1/*********************************************************************
2 * Software License Agreement (BSD License)
3 *
4 * Copyright (c) 2011, 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: Matt Maly */
36
37#include "ompl/control/planners/syclop/SyclopRRT.h"
38#include "ompl/base/goals/GoalSampleableRegion.h"
39#include "ompl/tools/config/SelfConfig.h"
40
42{
44 sampler_ = si_->allocStateSampler();
45 controlSampler_ = siC_->allocDirectedControlSampler();
46 lastGoalMotion_ = nullptr;
47
48 // Create a default GNAT nearest neighbors structure if the user doesn't want
49 // the default regionalNN check from the discretization
50 if (!nn_ && !regionalNN_)
51 {
53 nn_->setDistanceFunction([this](Motion *a, const Motion *b) { return distanceFunction(a, b); });
54 }
55}
56
58{
60 freeMemory();
61 if (nn_)
62 nn_->clear();
63 lastGoalMotion_ = nullptr;
64}
65
67{
68 Planner::getPlannerData(data);
69 std::vector<Motion *> motions;
70 if (nn_)
71 nn_->list(motions);
72 double delta = siC_->getPropagationStepSize();
73
74 if (lastGoalMotion_ != nullptr)
76
77 for (auto &motion : motions)
78 {
79 if (motion->parent != nullptr)
80 {
81 if (data.hasControls())
82 data.addEdge(base::PlannerDataVertex(motion->parent->state), base::PlannerDataVertex(motion->state),
83 control::PlannerDataEdgeControl(motion->control, motion->steps * delta));
84 else
85 data.addEdge(base::PlannerDataVertex(motion->parent->state), base::PlannerDataVertex(motion->state));
86 }
87 else
88 data.addStartVertex(base::PlannerDataVertex(motion->state));
89 }
90}
91
93{
94 auto *motion = new Motion(siC_);
95 si_->copyState(motion->state, s);
96 siC_->nullControl(motion->control);
97
98 if (nn_)
99 nn_->add(motion);
100 return motion;
101}
102
103void ompl::control::SyclopRRT::selectAndExtend(Region &region, std::vector<Motion *> &newMotions)
104{
105 auto *rmotion = new Motion(siC_);
106 base::StateSamplerPtr sampler(si_->allocStateSampler());
107 std::vector<double> coord(decomp_->getDimension());
108 decomp_->sampleFromRegion(region.index, rng_, coord);
109 decomp_->sampleFullState(sampler, coord, rmotion->state);
110
111 Motion *nmotion;
112 if (regionalNN_)
113 {
114 /* Instead of querying the nearest neighbors datastructure over the entire tree of motions,
115 * here we perform a linear search over all motions in the selected region and its neighbors. */
116 std::vector<int> searchRegions;
117 decomp_->getNeighbors(region.index, searchRegions);
118 searchRegions.push_back(region.index);
119
120 std::vector<Motion *> motions;
121 for (const auto &i : searchRegions)
122 {
123 const std::vector<Motion *> &regionMotions = getRegionFromIndex(i).motions;
124 motions.insert(motions.end(), regionMotions.begin(), regionMotions.end());
125 }
126
127 std::vector<Motion *>::const_iterator i = motions.begin();
128 nmotion = *i;
129 double minDistance = distanceFunction(rmotion, nmotion);
130 ++i;
131 while (i != motions.end())
132 {
133 Motion *m = *i;
134 const double dist = distanceFunction(rmotion, m);
135 if (dist < minDistance)
136 {
137 nmotion = m;
138 minDistance = dist;
139 }
140 ++i;
141 }
142 }
143 else
144 {
145 assert(nn_);
146 nmotion = nn_->nearest(rmotion);
147 }
148
149 unsigned int duration =
150 controlSampler_->sampleTo(rmotion->control, nmotion->control, nmotion->state, rmotion->state);
151 if (duration >= siC_->getMinControlDuration())
152 {
153 rmotion->steps = duration;
154 rmotion->parent = nmotion;
155 newMotions.push_back(rmotion);
156 if (nn_)
157 nn_->add(rmotion);
158 lastGoalMotion_ = rmotion;
159 }
160 else
161 {
162 si_->freeState(rmotion->state);
163 siC_->freeControl(rmotion->control);
164 delete rmotion;
165 }
166}
167
169{
170 if (nn_)
171 {
172 std::vector<Motion *> motions;
173 nn_->list(motions);
174 for (auto m : motions)
175 {
176 if (m->state != nullptr)
177 si_->freeState(m->state);
178 if (m->control != nullptr)
179 siC_->freeControl(m->control);
180 delete m;
181 }
182 }
183}
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...
virtual bool hasControls() const
Indicate whether any information about controls (ompl::control::Control) is stored in this instance.
SpaceInformationPtr si_
The space information for which planning is done.
Definition Planner.h:401
Definition of an abstract state.
Definition State.h:50
Representation of an edge in PlannerData for planning with controls. This structure encodes a specifi...
Definition PlannerData.h:61
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 SyclopRRT.cpp:66
Motion * lastGoalMotion_
The most recent goal motion. Used for PlannerData computation.
Definition SyclopRRT.h:116
void clear() override
Clear all internal datastructures. Planner settings are not affected. Subsequent calls to solve() wil...
Definition SyclopRRT.cpp:57
void selectAndExtend(Region &region, std::vector< Motion * > &newMotions) override
Select a Motion from the given Region, and extend the tree from the Motion. Add any new motions creat...
Syclop::Motion * addRoot(const base::State *s) override
Add State s as a new root in the low-level tree, and return the Motion corresponding to s.
Definition SyclopRRT.cpp:92
double distanceFunction(const Motion *a, const Motion *b) const
Compute distance between motions (actually distance between contained states).
Definition SyclopRRT.h:105
void setup() override
Perform extra configuration steps, if needed. This call will also issue a call to ompl::base::SpaceIn...
Definition SyclopRRT.cpp:41
void freeMemory()
Free the memory allocated by this planner.
Representation of a motion.
Definition Syclop.h:257
base::State * state
The state contained by the motion.
Definition Syclop.h:267
Control * control
The control contained by the motion.
Definition Syclop.h:269
Representation of a region in the Decomposition assigned to Syclop.
Definition Syclop.h:278
int index
The index of the graph node corresponding to this region.
Definition Syclop.h:311
const Region & getRegionFromIndex(const int rid) const
Returns a reference to the Region object with the given index. Assumes the index is valid.
Definition Syclop.h:357
void setup() override
Perform extra configuration steps, if needed. This call will also issue a call to ompl::base::SpaceIn...
Definition Syclop.cpp:48
const SpaceInformation * siC_
Handle to the control::SpaceInformation object.
Definition Syclop.h:383
void clear() override
Clear all internal datastructures. Planner settings are not affected. Subsequent calls to solve() wil...
Definition Syclop.cpp:58
RNG rng_
Random number generator.
Definition Syclop.h:389
DecompositionPtr decomp_
The high level decomposition used to focus tree expansion.
Definition Syclop.h:386
static NearestNeighbors< _T > * getDefaultNearestNeighbors(const base::Planner *planner)
Select a default nearest neighbor datastructure for the given space.
Definition SelfConfig.h:105