State.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/State.h"
38 
39 namespace ompl
40 {
41  namespace geometric
42  {
43  namespace eitstar
44  {
45  namespace
46  {
47  std::size_t generateId()
48  {
49  static std::size_t id{0u};
50  return id++;
51  }
52  } // namespace
53 
54  State::State(const std::shared_ptr<ompl::base::SpaceInformation> &spaceInfo,
55  const std::shared_ptr<ompl::base::OptimizationObjective> &objective)
56  : id_(generateId())
57  , estimatedCostToGo_(objective->infiniteCost())
58  , lowerBoundCostToGo_(objective->infiniteCost())
59  , lowerBoundCostToCome_(objective->infiniteCost())
60  , state_(spaceInfo->allocState())
61  , neighbors_({0u, {}})
62  , spaceInfo_(spaceInfo)
63  , objective_(objective)
64  {
65  if (state_ == nullptr)
66  {
67  OMPL_ERROR("Could not allocate state.");
68  throw std::bad_alloc();
69  }
70  }
71 
73  {
74  spaceInfo_->freeState(state_);
75  }
76 
77  std::size_t State::getId() const
78  {
79  return id_;
80  }
81 
83  {
84  return state_;
85  }
86 
87  bool State::hasForwardVertex() const
88  {
89  return static_cast<bool>(forwardVertex_.lock());
90  }
91 
92  bool State::hasReverseVertex() const
93  {
94  return static_cast<bool>(reverseVertex_.lock());
95  }
96 
97  std::shared_ptr<Vertex> State::asForwardVertex()
98  {
99  // If this state exists as a forward vertex, return this object.
100  if (auto forwardVertex = forwardVertex_.lock())
101  {
102  return forwardVertex;
103  }
104  else // If this state does not yet exist as a forward vertex, create it.
105  {
106  // Create a forward vertex from this state.
107  forwardVertex = std::make_shared<Vertex>(shared_from_this(), objective_, Direction::FORWARD);
108 
109  // If this state has a reverse vertex, the newly created vertex is its twin.
110  if (auto reverseVertex = reverseVertex_.lock())
111  {
112  reverseVertex->setTwin(forwardVertex);
113  forwardVertex->setTwin(reverseVertex);
114  }
115 
116  // Remember this state's forward vertex and return.
117  forwardVertex_ = forwardVertex;
118  return forwardVertex;
119  }
120  }
121 
122  std::shared_ptr<Vertex> State::asReverseVertex()
123  {
124  // If this state exists as a reverse vertex, return this object.
125  if (auto reverseVertex = reverseVertex_.lock())
126  {
127  return reverseVertex;
128  }
129  else // If this state does not yet exist as a reverse vertex, create it.
130  {
131  // Create a reverse vertex from this state.
132  reverseVertex = std::make_shared<Vertex>(shared_from_this(), objective_, Direction::REVERSE);
133 
134  // If this state has a forward vertex, the newly created vertex is its twin.
135  if (auto forwardVertex = forwardVertex_.lock())
136  {
137  forwardVertex->setTwin(reverseVertex);
138  reverseVertex->setTwin(forwardVertex);
139  }
140 
141  // Remember this state's reverse vertex and return.
142  reverseVertex_ = reverseVertex;
143  return reverseVertex;
144  }
145  }
146 
147  void State::blacklist(const std::shared_ptr<State> &state)
148  {
149  blacklist_.insert(state->getId());
150  }
151 
152  void State::whitelist(const std::shared_ptr<State> &state)
153  {
154  whitelist_.insert(state->getId());
155  }
156 
157  bool State::isBlacklisted(const std::shared_ptr<State> &state) const
158  {
159  return blacklist_.find(state->getId()) != blacklist_.end();
160  }
161 
162  bool State::isWhitelisted(const std::shared_ptr<State> &state) const
163  {
164  return whitelist_.find(state->getId()) != whitelist_.end();
165  }
166 
167  void State::setEstimatedEffortToGo(std::size_t effort)
168  {
169  estimatedEffortToGo_ = effort;
170  }
171 
173  {
174  estimatedCostToGo_ = cost;
175  }
176 
178  {
179  admissibleCostToGo_ = cost;
180  }
181 
183  {
184  lowerBoundCostToGo_ = cost;
185  }
186 
188  {
189  lowerBoundCostToCome_ = cost;
190  }
191 
193  {
194  currentCostToCome_ = cost;
195  }
196 
197  void State::setLowerBoundEffortToCome(unsigned int effort)
198  {
199  lowerBoundEffortToCome_ = effort;
200  }
201 
202  void State::setInadmissibleEffortToCome(unsigned int effort)
203  {
204  inadmissibleEffortToCome_ = effort;
205  }
206 
207  unsigned int State::getEstimatedEffortToGo() const
208  {
209  return estimatedEffortToGo_;
210  }
211 
213  {
214  return estimatedCostToGo_;
215  }
216 
218  {
219  return admissibleCostToGo_;
220  }
221 
223  {
224  return lowerBoundCostToGo_;
225  }
226 
228  {
229  return lowerBoundCostToCome_;
230  }
231 
233  {
234  return currentCostToCome_;
235  }
236 
237  unsigned int State::getLowerBoundEffortToCome() const
238  {
239  return lowerBoundEffortToCome_;
240  }
241 
242  unsigned int State::getInadmissibleEffortToCome() const
243  {
244  return inadmissibleEffortToCome_;
245  }
246 
247  const std::vector<std::weak_ptr<State>> State::getSourcesOfIncomingEdgesInForwardQueue() const
248  {
249  return sourcesOfIncomingEdgesInForwardQueue_;
250  }
251 
252  void State::addToSourcesOfIncomingEdgesInForwardQueue(const std::shared_ptr<State> &state) const
253  {
254  sourcesOfIncomingEdgesInForwardQueue_.emplace_back(state);
255  }
256 
257  void State::removeFromSourcesOfIncomingEdgesInForwardQueue(const std::shared_ptr<State> &state) const
258  {
259  const auto iter = std::find_if(
260  sourcesOfIncomingEdgesInForwardQueue_.begin(), sourcesOfIncomingEdgesInForwardQueue_.end(),
261  [&state](const auto &source) { return state->getId() == source.lock()->getId(); });
262 
263  if (iter != sourcesOfIncomingEdgesInForwardQueue_.end())
264  {
265  sourcesOfIncomingEdgesInForwardQueue_.erase(iter);
266  }
267  else
268  {
269  throw std::out_of_range("Unable to remove source from incoming edges in forward queue.");
270  }
271  }
272 
274  {
275  sourcesOfIncomingEdgesInForwardQueue_.clear();
276  }
277 
278  void State::setIncomingCollisionCheckResolution(const std::shared_ptr<State> &source,
279  std::size_t numChecks) const
280  {
281  incomingCollisionCheckResolution_[source->getId()] = numChecks;
282  }
283 
284  std::size_t State::getIncomingCollisionCheckResolution(const std::shared_ptr<State> &source) const
285  {
286  if (incomingCollisionCheckResolution_.find(source->getId()) == incomingCollisionCheckResolution_.end())
287  {
288  return 0u;
289  }
290  else
291  {
292  return incomingCollisionCheckResolution_[source->getId()];
293  }
294  }
295 
296  } // namespace eitstar
297 
298  } // namespace geometric
299 
300 } // namespace ompl
void whitelist(const std::shared_ptr< State > &state)
Whitelists the given state as neighbor.
Definition: State.cpp:248
~State()
Destructs this state, freeing the associated memory.
Definition: State.cpp:168
void setLowerBoundEffortToCome(unsigned int effort)
Sets the lower bound effort to come to this state through the continuous state space.
Definition: State.cpp:293
void blacklist(const std::shared_ptr< State > &state)
Blacklists the given state as neighbor.
Definition: State.cpp:243
ompl::base::Cost getEstimatedCostToGo() const
Returns the best estimate of the cost to go from this state to the goal through the current RGG.
Definition: State.cpp:308
Definition of an abstract state.
Definition: State.h:113
State(const std::shared_ptr< ompl::base::SpaceInformation > &spaceInfo, const std::shared_ptr< ompl::base::OptimizationObjective > &objective)
Constructs the state, allocating the associated memory using information about the underlying space.
Definition: State.cpp:150
void setAdmissibleCostToGo(ompl::base::Cost cost)
Set the admissible estimate of the cost to go from this state to the goal through the current RGG.
Definition: State.cpp:273
Definition of a cost value. Can represent the cost of a motion or the cost of a state.
Definition: Cost.h:111
void setInadmissibleEffortToCome(unsigned int effort)
Sets the inadmissible effort to come to this state through the continuous state space.
Definition: State.cpp:298
ompl::base::Cost getLowerBoundCostToGo() const
Returns the lower bound cost to go from this state to the goal through the continuous state space.
Definition: State.cpp:318
void setIncomingCollisionCheckResolution(const std::shared_ptr< State > &source, std::size_t numChecks) const
Sets the number of collision checks already performed on the edge incoming from source.
Definition: State.cpp:374
unsigned int getInadmissibleEffortToCome() const
Returns the inadmissible effort to come from the start to this state through the continuous state spa...
Definition: State.cpp:338
void setEstimatedCostToGo(ompl::base::Cost cost)
Set the best estimate of the cost to go from this state to the goal through the current RGG.
Definition: State.cpp:268
bool hasForwardVertex() const
Returns whether the state has an associated forward vertex.
Definition: State.cpp:183
unsigned int getEstimatedEffortToGo() const
Get the estimated effort (number of collision detections) to go from this state to the goal through t...
Definition: State.cpp:303
std::size_t getIncomingCollisionCheckResolution(const std::shared_ptr< State > &source) const
Returns the number of collision checks already performed on the edge incoming from source.
Definition: State.cpp:380
void setEstimatedEffortToGo(std::size_t effort)
Set the estimated effort (number of collision detections) to go from this state to the goal through t...
Definition: State.cpp:263
ompl::base::State * raw() const
Returns the raw OMPL version of this state.
Definition: State.cpp:178
std::shared_ptr< Vertex > asForwardVertex()
Returns the state as a reverse vertex.
Definition: State.cpp:193
void resetSourcesOfIncomingEdgesInForwardQueue()
Resets the sources of incoming edges in the forward queue.
Definition: State.cpp:369
void setCurrentCostToCome(ompl::base::Cost cost)
Set the current cost to come from the start to this state.
Definition: State.cpp:288
void setLowerBoundCostToCome(ompl::base::Cost cost)
Set the lower bound cost to come from the start to this state through the continuous state space.
Definition: State.cpp:283
bool isWhitelisted(const std::shared_ptr< State > &state) const
Returns whether the given has been whitelisted.
Definition: State.cpp:258
const std::vector< std::weak_ptr< State > > getSourcesOfIncomingEdgesInForwardQueue() const
Returns the sources of incoming edges in forward queue.
Definition: State.cpp:343
unsigned int getLowerBoundEffortToCome() const
Returns the lower bound effort to come from the start to this state through the continuous state spac...
Definition: State.cpp:333
ompl::base::Cost getLowerBoundCostToCome() const
Returns the lower bound cost to come from the start to this state through the continuous state space.
Definition: State.cpp:323
bool isBlacklisted(const std::shared_ptr< State > &state) const
Returns whether the given state has been blacklisted.
Definition: State.cpp:253
std::shared_ptr< Vertex > asReverseVertex()
Returns the state as a reverse vertex.
Definition: State.cpp:218
void addToSourcesOfIncomingEdgesInForwardQueue(const std::shared_ptr< State > &state) const
Adds a source to sources of incoming edges in forward queue.
Definition: State.cpp:348
bool hasReverseVertex() const
Returns whether the state has an associated reverse vertex.
Definition: State.cpp:188
#define OMPL_ERROR(fmt,...)
Log a formatted error string.
Definition: Console.h:64
void setLowerBoundCostToGo(ompl::base::Cost cost)
Set the lower bound cost to go from this state to the goal through the continuous state space.
Definition: State.cpp:278
std::size_t getId() const
Returns the state's unique id.
Definition: State.cpp:173
void removeFromSourcesOfIncomingEdgesInForwardQueue(const std::shared_ptr< State > &state) const
Removes a source from sources of incoming edges in forward queue.
Definition: State.cpp:353
ompl::base::Cost getAdmissibleCostToGo() const
Returns the admissible estimate of the cost to go from this state to the goal through the current RGG...
Definition: State.cpp:313
ompl::base::Cost getCurrentCostToCome() const
Returns the current cost to come from the start to this state.
Definition: State.cpp:328
Main namespace. Contains everything in this library.
Definition: AppBase.h:21