Loading...
Searching...
No Matches
Vertex.cpp
1/*********************************************************************
2 * Software License Agreement (BSD License)
3 *
4 * Copyright (c) 2019, University of Oxford
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 University of Toronto 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// Authors: Marlin Strub
36
37#include "ompl/geometric/planners/informedtrees/eitstar/Vertex.h"
38
39#include <algorithm>
40#include <limits>
41
42#include "ompl/geometric/planners/informedtrees/eitstar/State.h"
43
44namespace ompl
45{
46 namespace geometric
47 {
48 namespace eitstar
49 {
50 namespace
51 {
52 std::size_t generateId()
53 {
54 static std::size_t id{0u};
55 return id++;
56 }
57 } // namespace
58
59 Vertex::Vertex(const std::shared_ptr<State> &state,
60 const std::shared_ptr<ompl::base::OptimizationObjective> &objective,
61 const Direction &direction)
62 : id_(generateId())
63 , objective_(objective)
64 , edgeCost_(objective->infiniteCost())
65 , state_(state)
66 , direction_(direction)
67 {
68 }
69
70 Vertex::~Vertex()
71 {
72 if (direction_ == Direction::FORWARD)
73 {
74 state_->setCurrentCostToCome(objective_->infiniteCost());
75 }
76 else
77 {
78 assert(direction_ == Direction::REVERSE);
79 state_->setAdmissibleCostToGo(objective_->infiniteCost());
80 state_->setEstimatedCostToGo(objective_->infiniteCost());
81 state_->setEstimatedEffortToGo(std::numeric_limits<std::size_t>::max());
82 }
83 }
84
85 std::size_t Vertex::getId() const
86 {
87 return id_;
88 }
89
90 std::shared_ptr<State> Vertex::getState() const
91 {
92 return state_;
93 }
94
95 const std::vector<std::shared_ptr<Vertex>> &Vertex::getChildren() const
96 {
97 return children_;
98 }
99
100 bool Vertex::hasChildren() const
101 {
102 return !children_.empty();
103 }
104
105 std::vector<std::shared_ptr<Vertex>>
106 Vertex::updateCurrentCostOfChildren(const std::shared_ptr<ompl::base::OptimizationObjective> &objective)
107 {
108 std::vector<std::shared_ptr<Vertex>> accumulatedChildren = children_;
109 for (auto &child : children_)
110 {
111 child->getState()->setCurrentCostToCome(
112 objective->combineCosts(state_->getCurrentCostToCome(), child->getEdgeCost()));
113 const auto childsAccumulatedChildren = child->updateCurrentCostOfChildren(objective);
114 accumulatedChildren.insert(accumulatedChildren.end(), childsAccumulatedChildren.cbegin(),
115 childsAccumulatedChildren.cend());
116 }
117
118 return accumulatedChildren;
119 }
120
121 void Vertex::addChild(const std::shared_ptr<Vertex> &vertex)
122 {
123 assert(this);
124 assert(vertex);
125 assert((!parent_.lock()) || parent_.lock()->getId() != vertex->getId());
126 children_.emplace_back(vertex);
127 }
128
129 void Vertex::removeChild(const std::shared_ptr<Vertex> &vertex)
130 {
131 assert(this);
132 assert(vertex);
133
134 // Find the child that is to be removed.
135 const auto it = std::find_if(children_.begin(), children_.end(), [&vertex](const auto &child)
136 { return child->getId() == vertex->getId(); });
137
138 // If the provided vertex is not a child, this is a bug.
139 assert(it != children_.end());
140
141 // Do the good ol' swap and pop.
142 std::iter_swap(it, children_.rbegin());
143 children_.pop_back();
144 }
145
146 std::weak_ptr<Vertex> Vertex::getParent() const
147 {
148 return parent_;
149 }
150
151 bool Vertex::isParent(const std::shared_ptr<Vertex> &vertex) const
152 {
153 if (!static_cast<bool>(vertex) || parent_.expired())
154 {
155 return false;
156 }
157
158 return vertex->getId() == parent_.lock()->getId();
159 }
160
161 std::weak_ptr<Vertex> Vertex::getTwin() const
162 {
163 return twin_;
164 }
165
166 void Vertex::setEdgeCost(const ompl::base::Cost &edgeCost)
167 {
168 edgeCost_ = edgeCost;
169 }
170
171 ompl::base::Cost Vertex::getEdgeCost() const
172 {
173 return edgeCost_;
174 }
175
176 void Vertex::updateParent(const std::shared_ptr<Vertex> &vertex)
177 {
178 assert(std::find_if(children_.begin(), children_.end(), [&vertex](const auto &child)
179 { return vertex->getId() == child->getId(); }) == children_.end());
180
181 // If we already have a parent, update its children.
182 if (auto parent = parent_.lock())
183 {
184 parent->removeChild(shared_from_this());
185 }
186
187 // Remember the new parent.
188 parent_ = vertex;
189 }
190
191 void Vertex::resetParent()
192 {
193 parent_.reset();
194 }
195
196 void Vertex::setTwin(const std::shared_ptr<Vertex> &vertex)
197 {
198 twin_ = vertex;
199 }
200
201 void Vertex::clearChildren()
202 {
203 children_.clear();
204 }
205
206 std::size_t Vertex::getExpandTag() const
207 {
208 return expandTag_;
209 }
210
211 void Vertex::registerExpansionInReverseSearch(std::size_t expandTag)
212 {
213 expandTag_ = expandTag;
214 }
215
216 void Vertex::callOnBranch(const std::function<void(const std::shared_ptr<eitstar::State> &)> &function)
217 {
218 // Call it on the underlying state.
219 function(state_);
220
221 // Recursively call it on all children.
222 for (const auto &child : children_)
223 {
224 child->callOnBranch(function);
225 }
226 }
227
228 } // namespace eitstar
229
230 } // namespace geometric
231
232} // namespace ompl
Definition of a cost value. Can represent the cost of a motion or the cost of a state.
Definition Cost.h:48
This namespace contains code that is specific to planning under geometric constraints.
Main namespace. Contains everything in this library.