ForwardQueue.h
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 #ifndef OMPL_GEOMETRIC_PLANNERS_INFORMEDTREES_EITSTAR_FORWARD_QUEUE_
38 #define OMPL_GEOMETRIC_PLANNERS_INFORMEDTREES_EITSTAR_FORWARD_QUEUE_
39 
40 #include <array>
41 #include <map>
42 #include <utility>
43 #include <vector>
44 
45 #include "ompl/base/Cost.h"
46 #include "ompl/base/samplers/InformedStateSampler.h"
47 #include "ompl/datastructures/BinaryHeap.h"
48 #include "ompl/datastructures/NearestNeighbors.h"
49 
50 #include "ompl/geometric/planners/informedtrees/eitstar/Direction.h"
51 #include "ompl/geometric/planners/informedtrees/eitstar/Edge.h"
52 #include "ompl/geometric/planners/informedtrees/eitstar/Vertex.h"
53 
54 #include <unordered_map>
55 
56 namespace ompl
57 {
58  namespace geometric
59  {
60  namespace eitstar
61  {
62  struct pair_hash
63  {
64  std::size_t operator()(const std::pair<std::size_t, std::size_t> &k) const
65  {
66  // This method to combine hashes is the same as boost::hash_combine
67  // https://www.boost.org/doc/libs/1_67_0/boost/container_hash/hash.hpp
68  // For a discussion see e.g. https://stackoverflow.com/a/35991300
69  std::hash<std::size_t> hasher;
70 
71  std::size_t seed = hasher(k.first);
72  seed ^= hasher(k.second) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
73 
74  return seed;
75  }
76  };
77 
78  class ForwardQueue
79  {
80  public:
82  ForwardQueue(const std::shared_ptr<const ompl::base::OptimizationObjective> &objective,
83  const std::shared_ptr<const ompl::base::StateSpace> &space);
84 
86  ~ForwardQueue() = default;
87 
89  bool empty() const;
90 
92  std::size_t size() const;
93 
95  void insertOrUpdate(const Edge &edge);
96 
98  void insertOrUpdate(const std::vector<Edge> &edges);
99 
101  void remove(const Edge &edge);
102 
104  void updateIfExists(const Edge &edge);
105 
107  Edge peek(double suboptimalityFactor);
108 
110  Edge pop(double suboptimalityFactor);
111 
114 
117  bool containsOpenTargets(std::size_t reverseSearchTag) const;
118 
120  void clear();
121 
123  std::vector<Edge> getEdges() const;
124 
126  void rebuild();
127 
129  unsigned int getMinEffortToCome() const;
130 
132  std::size_t estimateEffort(const Edge &edge) const;
133 
134  private:
136  struct EdgeKeys
137  {
138  EdgeKeys(const ompl::base::Cost &lowerBound, const ompl::base::Cost &estimated,
139  const std::size_t effort)
140  : lowerBoundCost(lowerBound), estimatedCost(estimated), estimatedEffort(effort){};
141  ompl::base::Cost lowerBoundCost;
142  ompl::base::Cost estimatedCost;
143  std::size_t estimatedEffort;
144  };
145 
147  std::pair<EdgeKeys, Edge> makeElement(const Edge &edge) const;
148 
149  using Container =
150  std::unordered_map<std::pair<std::size_t, std::size_t>, std::pair<EdgeKeys, Edge>, pair_hash>;
151 
153  Container::const_iterator getFrontIter(double suboptimalityFactor);
154 
156  inline std::pair<EdgeKeys, Edge> &get(Container::iterator &it) const
157  {
158  return it->second;
159  }
160 
162  inline const std::pair<EdgeKeys, Edge> &get(Container::const_iterator &it) const
163  {
164  return it->second;
165  }
166 
168  Container::iterator getBestCostEstimateEdge();
169 
171  Container::iterator getLowerBoundCostEdge();
172 
174  Container::const_iterator getLowerBoundCostEdge() const;
175 
177  Container::const_iterator getBestCostEstimateEdge() const;
178 
180  ompl::base::Cost inflateCost(const ompl::base::Cost &cost, double factor) const;
181 
183  ompl::base::Cost estimateCost(const Edge &edge) const;
184 
186  ompl::base::Cost lowerBoundCost(const Edge &edge) const;
187 
189  std::shared_ptr<const ompl::base::OptimizationObjective> objective_;
190 
192  std::shared_ptr<const ompl::base::StateSpace> space_;
193 
195  Container queue_{};
196 
198  Container::const_iterator front_;
199 
201  const bool cacheQueueLookup_ = true;
202 
204  bool modifiedQueue_ = true;
205 
207  mutable unsigned int cachedMinEdgeEffort_{0u};
208  };
209  } // namespace eitstar
210 
211  } // namespace geometric
212 
213 } // namespace ompl
214 
215 #endif // OMPL_GEOMETRIC_PLANNERS_INFORMEDTREES_EITSTAR_FORWARD_QUEUE_
std::vector< Edge > getEdges() const
Copies all edges into a vector and returns the vector.
A struct for basic edge data.
Definition: Edge.h:152
Definition of a cost value. Can represent the cost of a motion or the cost of a state.
Definition: Cost.h:111
void insertOrUpdate(const Edge &edge)
Inserts or updates an edge in the queue.
Edge pop(double suboptimalityFactor)
Returns and deletes the top element of the queue.
bool containsOpenTargets(std::size_t reverseSearchTag) const
Returns whether the queue contains edges with targets that are open or unconnected in the reverse sea...
std::size_t size() const
Returns how many elements are in the queue.
void updateIfExists(const Edge &edge)
Update an edge in the queue. Does nothing if the edge is not in the queue.
Edge peek(double suboptimalityFactor)
Returns a copy to the next edge.
ForwardQueue(const std::shared_ptr< const ompl::base::OptimizationObjective > &objective, const std::shared_ptr< const ompl::base::StateSpace > &space)
Constructs the queue given the objective and state space.
unsigned int getMinEffortToCome() const
Returns the minimum effort that remains.
std::size_t estimateEffort(const Edge &edge) const
Estimates the effort that remains to validate a solution through an edge.
void clear()
Clears the queue, i.e., deletes all elements from it.
~ForwardQueue()=default
Destructs the queue.
ompl::base::Cost getLowerBoundOnOptimalSolutionCost() const
Returns a lower bound on the resolution-optimal solution cost.
bool empty() const
Returns whether the queue is empty.
void remove(const Edge &edge)
Removes an edge from the queue. Throws if the edge is not in the queue.
void rebuild()
Rebuilds the queue.
Main namespace. Contains everything in this library.
Definition: AppBase.h:21