IntegratedQueue.h
1 /*********************************************************************
2 * Software License Agreement (BSD License)
3 *
4 * Copyright (c) 2014, University of Toronto
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: Jonathan Gammell */
36 
37 #ifndef OMPL_GEOMETRIC_PLANNERS_BITSTAR_DATASTRUCTURES_INTEGRATEDQUEUE_
38 #define OMPL_GEOMETRIC_PLANNERS_BITSTAR_DATASTRUCTURES_INTEGRATEDQUEUE_
39 
40 //STL/Boost/etc.:
41 //std::pair
42 #include <utility>
43 //std::vector
44 #include <vector>
45 //std::list
46 #include <list>
47 //std::multimap
48 #include <map>
49 //std::unordered_map
50 #include <unordered_map>
51 //For std::function
52 #include <functional>
53 
54 //OMPL:
55 //The cost class:
56 #include "ompl/base/Cost.h"
57 //The optimization objective class:
58 #include "ompl/base/OptimizationObjective.h"
59 //The nearest neighbours structure
60 #include "ompl/datastructures/NearestNeighbors.h"
61 
62 //BIT*:
63 //I am member class of the BITstar class, so I need to include it's definition to be aware of the class BITstar. It has a forward declaration to me.
64 #include "ompl/geometric/planners/bitstar/BITstar.h"
65 //The vertex class:
66 #include "ompl/geometric/planners/bitstar/datastructures/Vertex.h"
67 
68 namespace ompl
69 {
70  namespace geometric
71  {
72 
73 
91  {
92  public:
94  //Data typedefs:
96  typedef std::pair<ompl::base::Cost, ompl::base::Cost> CostPair;
98 
100  //Function typedefs:
102  typedef std::function<ompl::base::Cost (const VertexConstPtr&)> VertexHeuristicFunc;
103 
105  typedef std::function<ompl::base::Cost (const VertexConstPtrPair&)> EdgeHeuristicFunc;
106 
108  typedef std::function<double (const VertexConstPtr&, const VertexConstPtr&)> DistanceFunc;
109 
111  typedef std::function<unsigned int (const VertexPtr&, std::vector<VertexPtr>*)> NeighbourhoodFunc;
113 
114 
115 
117  //Public functions:
119  //std::make_shared can only take 9 arguments, so be careful:
121 
122  virtual ~IntegratedQueue();
123 
127  void setDelayedRewiring(bool delayRewiring);
128 
130  bool getDelayedRewiring() const;
131 
133  //Insert and erase
135  void insertVertex(const VertexPtr& newVertex);
136 
138  void insertEdge(const VertexPtrPair& newEdge);
139 
141  void eraseVertex(const VertexPtr& oldVertex, bool disconnectParent, const VertexPtrNNPtr& vertexNN, const VertexPtrNNPtr& freeStateNN, std::vector<VertexPtr>* recycledVertices);
143 
145  //Access the queue
148 
151 
154 
157 
159  void popFrontEdge(VertexPtrPair* bestEdge);
160 
164 
166  //Queue maintenance
168  void hasSolution();
169 
171  void setThreshold(const ompl::base::Cost& costThreshold);
172 
175 
177  void removeEdgesTo(const VertexPtr& cVertex);
178 
180  void removeEdgesFrom(const VertexPtr& pVertex);
181 
183  void pruneEdgesTo(const VertexPtr& cVertex);
184 
186  void pruneEdgesFrom(const VertexPtr& pVertex);
187 
189  void markVertexUnsorted(const VertexPtr& vertex);
190 
192  std::pair<unsigned int, unsigned int> prune(const VertexPtr& pruneStartPtr, const VertexPtrNNPtr& vertexNN, const VertexPtrNNPtr& freeStateNN, std::vector<VertexPtr>* recycledVertices);
193 
195  std::pair<unsigned int, unsigned int> resort(const VertexPtrNNPtr& vertexNN, const VertexPtrNNPtr& freeStateNN, std::vector<VertexPtr>* recycledVertices);
196 
198  void finish();
199 
201  void reset();
202 
204  void clear();
206 
208  //Queue info:
210  bool vertexPruneCondition(const VertexPtr& vertex) const;
211 
213  bool samplePruneCondition(const VertexPtr& vertex) const;
214 
216  bool edgePruneCondition(const VertexPtrPair& edge) const;
217 
219  unsigned int numEdges() const;
220 
222  unsigned int numVertices() const;
223 
225  unsigned int numEdgesTo(const VertexPtr& cVertex) const;
226 
228  unsigned int numEdgesFrom(const VertexPtr& pVertex) const;
229 
231  bool isSorted() const;
232 
234  bool isReset() const;
235 
237  bool isEmpty();
238 
240  bool isVertexExpanded(const VertexConstPtr& vertex) const;
241 
243  void listVertices(std::vector<VertexConstPtr>* vertexQueue);
244 
246  void listEdges(std::vector<std::pair<VertexConstPtr, VertexConstPtr> >* edgeQueue);
249 
250 
251 
252 
253  private:
255  //Helpful typedefs:
257  typedef std::multimap<ompl::base::Cost, VertexPtr, std::function<bool (const ompl::base::Cost&, const ompl::base::Cost&)> > CostToVertexMMap;
258 
260  typedef std::multimap<CostPair, VertexPtrPair, std::function<bool (const CostPair&, const CostPair&)> > CostToVertexPtrPairMMap;
261 
263  typedef CostToVertexMMap::iterator VertexQueueIter;
264 
266  typedef std::unordered_map<BITstar::VertexId, VertexQueueIter> VertexIdToVertexQueueIterUMap;
267 
269  typedef CostToVertexPtrPairMMap::iterator EdgeQueueIter;
270 
272  typedef std::list<EdgeQueueIter> EdgeQueueIterList;
273 
275  typedef std::unordered_map<BITstar::VertexId, EdgeQueueIterList> VertexIdToEdgeQueueIterListUMap;
277 
278 
280  //High level primitives:
282  void updateQueue();
283 
285  void expandNextVertex();
286 
288  void expandVertex(const VertexPtr& vertex);
289 
291  void queueupEdge(const VertexPtr& parent, const VertexPtr& child);
292 
294  void processKNearest(unsigned int k, const VertexConstPtr& vertex, std::vector<VertexPtr>* kNearSamples, std::vector<VertexPtr>* kNearVertices);
296 
297 
299  //Vertex helper functions:
301  void reinsertVertex(const VertexPtr& unorderedVertex);
302 
304  std::pair<unsigned int, unsigned int> pruneBranch(const VertexPtr& branchBase, const VertexPtrNNPtr& vertexNN, const VertexPtrNNPtr& freeStateNN, std::vector<VertexPtr>* recycledVertices);
305 
307  void disconnectParent(const VertexPtr& oldVertex, bool cascadeCostUpdates);
308 
310  void vertexInsertHelper(const VertexPtr& newVertex, bool expandIfBeforeToken);
311 
313  //This is *NOT* by const-reference so that the oldVertex pointer doesn't go out of scope on me... which was happening if it was being called with an iter->second where the iter gets deleted in this function...
314  unsigned int vertexRemoveHelper(VertexPtr oldVertex, const VertexPtrNNPtr& vertexNN, const VertexPtrNNPtr& freeStateNN, std::vector<VertexPtr>* recycledVertices, bool removeLookups);
316 
318  //Edge helper functions:
320  void edgeInsertHelper(const VertexPtrPair& newEdge, EdgeQueueIter positionHint);
321 
323  void edgeRemoveHelper(const EdgeQueueIter& oldEdgeIter, bool rmIncomingLookup, bool rmOutgoingLookup);
324 
326  void rmIncomingLookup(const EdgeQueueIter& mmapIterToRm);
327 
329  void rmOutgoingLookup(const EdgeQueueIter& mmapIterToRm);
330 
332  void rmEdgeLookupHelper(VertexIdToEdgeQueueIterListUMap& lookup, const BITstar::VertexId& idx, const EdgeQueueIter& mmapIterToRm);
334 
335 
336 
337 
339  //Base-queue basic helper functions:
341  ompl::base::Cost vertexQueueValue(const VertexPtr& vertex) const;
342 
344  CostPair edgeQueueValue(const VertexPtrPair& edge) const;
345 
347  bool vertexQueueComparison(const ompl::base::Cost& lhs, const ompl::base::Cost& rhs) const;
348 
350  bool edgeQueueComparison(const CostPair& lhs, const CostPair& rhs) const;
352 
354  //Cost helpers
356  bool isCostWorseThan(const ompl::base::Cost& a, const ompl::base::Cost& b) const;
357 
359  bool isCostNotEquivalentTo(const ompl::base::Cost& a, const ompl::base::Cost& b) const;
360 
362  bool isCostBetterThanOrEquivalentTo(const ompl::base::Cost& a, const ompl::base::Cost& b) const;
363 
365  bool isCostWorseThanOrEquivalentTo(const ompl::base::Cost& a, const ompl::base::Cost& b) const;
367 
368 
369 
371  //Member variables:
374 
376  DistanceFunc distanceFunc_;
377 
379  NeighbourhoodFunc nearSamplesFunc_;
380 
382  NeighbourhoodFunc nearVerticesFunc_;
383 
385  VertexHeuristicFunc lowerBoundHeuristicVertexFunc_;
386 
388  VertexHeuristicFunc currentHeuristicVertexFunc_;
389 
391  EdgeHeuristicFunc lowerBoundHeuristicEdgeFunc_;
392 
394  EdgeHeuristicFunc currentHeuristicEdgeFunc_;
395 
397  EdgeHeuristicFunc currentHeuristicEdgeTargetFunc_;
398 
400  bool delayRewiring_;
401 
403  bool outgoingLookupTables_;
404 
406  bool incomingLookupTables_;
407 
409  CostToVertexMMap vertexQueue_;
410 
412  VertexQueueIter vertexToExpand_;
413 
415  CostToVertexPtrPairMMap edgeQueue_;
416 
418  VertexIdToVertexQueueIterUMap vertexIterLookup_;
419 
421  VertexIdToEdgeQueueIterListUMap outgoingEdges_;
422 
424  VertexIdToEdgeQueueIterListUMap incomingEdges_;
425 
427  std::list<VertexPtr> resortVertices_;
428 
430  ompl::base::Cost costThreshold_;
431 
433  bool hasSolution_;
435  }; //class: IntegratedQueue
436  } //geometric
437 } //ompl
438 #endif //OMPL_GEOMETRIC_PLANNERS_BITSTAR_DATASTRUCTURES_INTEGRATEDQUEUE_
439 
std::function< ompl::base::Cost(const VertexConstPtr &)> VertexHeuristicFunc
A std::function definition of a heuristic function for a vertex.
void pruneEdgesTo(const VertexPtr &cVertex)
Prune edges in the edge queue that lead to the given vertex using the prune function.
void listVertices(std::vector< VertexConstPtr > *vertexQueue)
Get a copy of the vertices in the vertex queue that are left to be expanded. This is expensive and is...
void clear()
Clear the queue to the state of construction.
void setDelayedRewiring(bool delayRewiring)
Delay considering rewiring edges until an initial solution is found. This improves the time required ...
bool samplePruneCondition(const VertexPtr &vertex) const
The condition used to prune disconnected samples from the free set. Compares lowerBoundHeuristicVerte...
void markVertexUnsorted(const VertexPtr &vertex)
Mark the queue as requiring resorting downstream of the specified vertex.
std::pair< ompl::base::Cost, ompl::base::Cost > CostPair
A typedef for a pair of costs, i.e., the edge sorting key.
ompl::base::Cost lowerBoundHeuristicEdge(const VertexConstPtrPair &edgePair) const
Calculates a heuristic estimate of the cost of a solution constrained to go through an edge...
Definition: BITstar.cpp:1682
void eraseVertex(const VertexPtr &oldVertex, bool disconnectParent, const VertexPtrNNPtr &vertexNN, const VertexPtrNNPtr &freeStateNN, std::vector< VertexPtr > *recycledVertices)
Erase a vertex from the vertex expansion queue. Will disconnect the vertex from its parent and remove...
unsigned int numVertices() const
Returns the number of vertices left to expand. This has nontrivial cost, as the token must be moved t...
std::shared_ptr< NearestNeighbors< VertexPtr > > VertexPtrNNPtr
The OMPL::NearestNeighbors structure.
Definition: BITstar.h:137
bool edgePruneCondition(const VertexPtrPair &edge) const
The condition used to prune edge (i.e., vertex-pair) out of the queue. Compares lowerBoundHeuristicEd...
unsigned int numEdgesTo(const VertexPtr &cVertex) const
Get the number of edges in the queue pointing to a specific vertex.
std::function< ompl::base::Cost(const VertexConstPtrPair &)> EdgeHeuristicFunc
A std::function definition of a heuristic function for an edge.
std::pair< unsigned int, unsigned int > resort(const VertexPtrNNPtr &vertexNN, const VertexPtrNNPtr &freeStateNN, std::vector< VertexPtr > *recycledVertices)
Resort the queue, only reinserting edges/vertices if their lower-bound heuristic is less then the thr...
unsigned int numEdges() const
Returns the number of edges in the queue.
bool isSorted() const
Return whether the queue is still sorted.
std::function< double(const VertexConstPtr &, const VertexConstPtr &)> DistanceFunc
A std::function definition for the distance between two vertices.
VertexPtr frontVertex()
Get the best vertex on the queue without incrementing the vertex queue.
void pruneEdgesFrom(const VertexPtr &pVertex)
Prune edges in the edge queue that leave from the given vertex using the prune function.
std::shared_ptr< const Vertex > VertexConstPtr
A constant vertex shared pointer.
Definition: BITstar.h:125
void insertEdge(const VertexPtrPair &newEdge)
Insert an edge into the edge processing queue. Edges are removed from the processing queue...
bool isVertexExpanded(const VertexConstPtr &vertex) const
Returns whether a given vertex has been expanded or not.
CostPair frontEdgeValue()
Get the value of the best edge on the queue, leaving it on the edge queue.
VertexPtrPair frontEdge()
Get the best edge on the queue, leaving it on the edge queue.
bool isEmpty()
Returns true if the queue is empty. In the case where the edge queue is empty but the vertex queue is...
A queue of edges to be processed that integrates both the expansion of Vertices and the ordering of t...
ompl::base::Cost currentHeuristicEdgeTarget(const VertexConstPtrPair &edgePair) const
Calculates a heuristic estimate of the cost of a path to the target of an edge, dependent on the cost...
Definition: BITstar.cpp:1696
void reset()
Reset the queue, clearing all the edge containers and moving the vertex expansion token to the start...
void setThreshold(const ompl::base::Cost &costThreshold)
Set the threshold of the queue.
ompl::base::Cost lowerBoundHeuristicVertex(const VertexConstPtr &vertex) const
Calculates a heuristic estimate of the cost of a solution constrained to pass through a vertex...
Definition: BITstar.cpp:1669
unsigned int numEdgesFrom(const VertexPtr &pVertex) const
Get the number of edges in the queue coming from a specific vertex.
std::pair< VertexPtr, VertexPtr > VertexPtrPair
A pair of vertices, i.e., an edge.
Definition: BITstar.h:133
ompl::base::Cost getThreshold() const
Get the threshold of the queue.
VertexPtrPair popFrontEdge()
Pop the best edge off the queue, removing it from the edge queue in the process.
ompl::base::Cost currentHeuristicEdge(const VertexConstPtrPair &edgePair) const
Calculates a heuristic estimate of the cost of a solution constrained to go through an edge...
Definition: BITstar.cpp:1689
A shared pointer wrapper for ompl::base::OptimizationObjective.
IntegratedQueue(const ompl::base::OptimizationObjectivePtr &opt, const DistanceFunc &distanceFunc, const NeighbourhoodFunc &nearSamplesFunc, const NeighbourhoodFunc &nearVerticesFunc, const VertexHeuristicFunc &lowerBoundHeuristicVertex, const VertexHeuristicFunc &currentHeuristicVertex, const EdgeHeuristicFunc &lowerBoundHeuristicEdge, const EdgeHeuristicFunc &currentHeuristicEdge, const EdgeHeuristicFunc &currentHeuristicEdgeTarget)
Construct an integrated queue.
std::pair< unsigned int, unsigned int > prune(const VertexPtr &pruneStartPtr, const VertexPtrNNPtr &vertexNN, const VertexPtrNNPtr &freeStateNN, std::vector< VertexPtr > *recycledVertices)
Prune the vertex queue of vertices whose their lower-bound heuristic is greater then the threshold...
ompl::base::Cost currentHeuristicVertex(const VertexConstPtr &vertex) const
Calculates a heuristic estimate of the cost of a solution constrained to pass through a vertex...
Definition: BITstar.cpp:1676
void removeEdgesFrom(const VertexPtr &pVertex)
Erase all edges in the edge queue that leave from the given vertex.
bool vertexPruneCondition(const VertexPtr &vertex) const
The condition used to prune vertices out of the queue. Compares lowerBoundHeuristicVertex to the give...
ompl::base::Cost frontVertexValue()
Get the value of the best vertex on the queue without incrementing the vertex queue.
void removeEdgesTo(const VertexPtr &cVertex)
Erase all edges in the edge queue that lead to the given vertex.
void listEdges(std::vector< std::pair< VertexConstPtr, VertexConstPtr > > *edgeQueue)
Get a copy of the edge queue. This is expensive and is only meant for animations/debugging.
void insertVertex(const VertexPtr &newVertex)
Insert a vertex into the vertex expansion queue. Vertices remain in the vertex queue until pruned or ...
std::function< unsigned int(const VertexPtr &, std::vector< VertexPtr > *)> NeighbourhoodFunc
A std::function definition for the neighbourhood of a vertex .
bool getDelayedRewiring() const
Get whether BIT* is delaying rewiring until a solution is found.
std::shared_ptr< Vertex > VertexPtr
A vertex shared pointer.
Definition: BITstar.h:120
bool isReset() const
Returns true if the queue is reset. This means that no edges have been expanded and the vertex expans...
Definition of a cost value. Can represent the cost of a motion or the cost of a state.
Definition: Cost.h:47
void hasSolution()
Mark that a solution has been found.
unsigned int VertexId
The vertex id type.
Definition: BITstar.h:131
void finish()
Finish the queue, clearing all the edge containers and moving the vertex expansion token to the end...