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 
44 namespace 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 
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 
139  // If the provided vertex is not a child, this is a bug.
140  assert(it != children_.end());
141 
142  // Do the good ol' swap and pop.
143  std::iter_swap(it, children_.rbegin());
144  children_.pop_back();
145  }
146 
147  std::weak_ptr<Vertex> Vertex::getParent() const
148  {
149  return parent_;
150  }
151 
152  bool Vertex::isParent(const std::shared_ptr<Vertex> &vertex) const
153  {
154  if (!static_cast<bool>(vertex) || parent_.expired())
155  {
156  return false;
157  }
158 
159  return vertex->getId() == parent_.lock()->getId();
160  }
161 
162  std::weak_ptr<Vertex> Vertex::getTwin() const
163  {
164  return twin_;
165  }
166 
167  void Vertex::setEdgeCost(const ompl::base::Cost &edgeCost)
168  {
169  edgeCost_ = edgeCost;
170  }
171 
172  ompl::base::Cost Vertex::getEdgeCost() const
173  {
174  return edgeCost_;
175  }
176 
177  void Vertex::updateParent(const std::shared_ptr<Vertex> &vertex)
178  {
179  assert(std::find_if(children_.begin(), children_.end(), [&vertex](const auto &child) {
180  return vertex->getId() == child->getId();
181  }) == children_.end());
182 
183  // If we already have a parent, update its children.
184  if (auto parent = parent_.lock())
185  {
186  parent->removeChild(shared_from_this());
187  }
188 
189  // Remember the new parent.
190  parent_ = vertex;
191  }
192 
193  void Vertex::resetParent()
194  {
195  parent_.reset();
196  }
197 
198  void Vertex::setTwin(const std::shared_ptr<Vertex> &vertex)
199  {
200  twin_ = vertex;
201  }
202 
203  void Vertex::clearChildren()
204  {
205  children_.clear();
206  }
207 
208  std::size_t Vertex::getExpandTag() const
209  {
210  return expandTag_;
211  }
212 
213  void Vertex::registerExpansionInReverseSearch(std::size_t expandTag)
214  {
215  expandTag_ = expandTag;
216  }
217 
218  void Vertex::callOnBranch(const std::function<void(const std::shared_ptr<eitstar::State> &)> &function)
219  {
220  // Call it on the underlying state.
221  function(state_);
222 
223  // Recursively call it on all children.
224  for (const auto &child : children_)
225  {
226  child->callOnBranch(function);
227  }
228  }
229 
230  } // namespace eitstar
231 
232  } // namespace geometric
233 
234 } // namespace ompl
virtual Cost combineCosts(Cost c1, Cost c2) const
Get the cost that corresponds to combining the costs c1 and c2. Default implementation defines this c...
Definition of a cost value. Can represent the cost of a motion or the cost of a state.
Definition: Cost.h:111
void addChild(const VertexPtr &newChild)
Add a child to this vertex. Does not change this vertex's cost or those of its descendants....
Definition: Vertex.cpp:343
void getChildren(VertexConstPtrVector *children) const
Get the children of a vertex as constant pointers.
Definition: Vertex.cpp:299
void removeChild(const VertexPtr &oldChild)
Remove a child from this vertex. Does not change this vertex's cost or those of its descendants....
Definition: Vertex.cpp:370
BITstar::VertexId getId() const
The (unique) vertex ID.
Definition: Vertex.cpp:138
bool hasChildren() const
Get whether this vertex has any children.
Definition: Vertex.cpp:292
virtual ~Vertex()
Destruct a vertex.
Definition: Vertex.cpp:130
Vertex(const std::shared_ptr< State > &state, const std::shared_ptr< ompl::base::OptimizationObjective > &objective, const Direction &direction)
Constructs the vertex, which must be associated with a state.
Definition: Vertex.cpp:155
VertexConstPtr getParent() const
Get a const pointer to the parent of this vertex.
Definition: Vertex.cpp:198
Main namespace. Contains everything in this library.
Definition: AppBase.h:21