PlannerData.h
1 /*********************************************************************
2 * Software License Agreement (BSD License)
3 *
4 * Copyright (c) 2012, Rice University
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 Rice University 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 /* Author: Ryan Luna, Luis G. Torres */
36 
37 #ifndef OMPL_BASE_PLANNER_DATA_
38 #define OMPL_BASE_PLANNER_DATA_
39 
40 #include <iostream>
41 #include <vector>
42 #include <map>
43 #include <set>
44 #include "ompl/base/State.h"
45 #include "ompl/base/Cost.h"
46 #include "ompl/base/SpaceInformation.h"
47 #include "ompl/util/ClassForward.h"
48 #include <functional>
49 #include <boost/serialization/access.hpp>
50 
51 namespace ompl
52 {
53  namespace base
54  {
60  {
61  public:
63  PlannerDataVertex(const State *st, int tag = 0) : state_(st), tag_(tag) {}
66  virtual ~PlannerDataVertex() {}
67 
69  virtual int getTag() const { return tag_; }
71  virtual void setTag(int tag) { tag_ = tag; }
73  virtual const State* getState() const { return state_; }
74 
76  virtual PlannerDataVertex* clone() const
77  {
78  return new PlannerDataVertex(*this);
79  }
80 
82  virtual bool operator==(const PlannerDataVertex &rhs) const
83  {
84  // States should be unique
85  return state_ == rhs.state_;
86  }
87 
90  bool operator!=(const PlannerDataVertex &rhs) const
91  {
92  return !(*this == rhs);
93  }
94 
95  protected:
97 
98  friend class boost::serialization::access;
99  template <class Archive>
100  void serialize(Archive & ar, const unsigned int /*version*/)
101  {
102  ar & tag_;
103  // Serialization of the state pointer is handled by PlannerDataStorage
104  }
105 
107  const State *state_;
109  int tag_;
110 
111  friend class PlannerData;
112  friend class PlannerDataStorage;
113  };
114 
117  {
118  public:
119  PlannerDataEdge() {}
120  virtual ~PlannerDataEdge() {}
122  virtual PlannerDataEdge* clone() const { return new PlannerDataEdge(); }
123 
125  virtual bool operator==(const PlannerDataEdge &rhs) const
126  {
127  return this == &rhs;
128  }
129 
132  bool operator!=(const PlannerDataEdge &rhs) const
133  {
134  return !(*this == rhs);
135  }
136 
137  protected:
138 
139  friend class boost::serialization::access;
140  template <class Archive>
141  void serialize(Archive & /*ar*/, const unsigned int /*version*/)
142  {
143  }
144  };
145 
147  OMPL_CLASS_FORWARD(StateStorage);
148  OMPL_CLASS_FORWARD(PlannerData);
149 
150  // Forward declaration for PlannerData::computeEdgeWeights
151  class OptimizationObjective;
153 
158  class PlannerData
164  {
165  public:
166  class Graph;
167 
169  static const PlannerDataEdge NO_EDGE;
173  static const unsigned int INVALID_INDEX;
174 
175  // non-copyable
176  PlannerData(const PlannerData&) = delete;
177  PlannerData& operator=(const PlannerData&) = delete;
178 
180  PlannerData(const SpaceInformationPtr &si);
182  virtual ~PlannerData();
183 
186 
191  unsigned int addVertex(const PlannerDataVertex &st);
196  unsigned int addStartVertex(const PlannerDataVertex &v);
201  unsigned int addGoalVertex(const PlannerDataVertex &v);
204  bool markStartState(const State *st);
207  bool markGoalState(const State *st);
210  bool tagState(const State *st, int tag);
214  virtual bool removeVertex(const PlannerDataVertex &st);
218  virtual bool removeVertex(unsigned int vIndex);
221  virtual bool addEdge(unsigned int v1, unsigned int v2,
222  const PlannerDataEdge &edge = PlannerDataEdge(),
223  Cost weight = Cost(1.0));
228  virtual bool addEdge(const PlannerDataVertex &v1, const PlannerDataVertex &v2,
229  const PlannerDataEdge &edge = PlannerDataEdge(),
230  Cost weight = Cost(1.0));
232  virtual bool removeEdge(unsigned int v1, unsigned int v2);
235  virtual bool removeEdge(const PlannerDataVertex &v1, const PlannerDataVertex &v2);
237  virtual void clear();
245  virtual void decoupleFromPlanner();
246 
250 
252  unsigned int numEdges() const;
254  unsigned int numVertices() const;
256  unsigned int numStartVertices() const;
258  unsigned int numGoalVertices() const;
259 
263 
265  bool vertexExists(const PlannerDataVertex &v) const;
268  const PlannerDataVertex& getVertex(unsigned int index) const;
271  PlannerDataVertex& getVertex(unsigned int index);
274  const PlannerDataVertex& getStartVertex(unsigned int i) const;
277  PlannerDataVertex& getStartVertex(unsigned int i);
280  const PlannerDataVertex& getGoalVertex(unsigned int i) const;
283  PlannerDataVertex& getGoalVertex(unsigned int i);
287  unsigned int getStartIndex(unsigned int i) const;
291  unsigned int getGoalIndex(unsigned int i) const;
293  bool isStartVertex(unsigned int index) const;
295  bool isGoalVertex(unsigned int index) const;
299  unsigned int vertexIndex(const PlannerDataVertex &v) const;
300 
304 
306  bool edgeExists(unsigned int v1, unsigned int v2) const;
309  const PlannerDataEdge& getEdge(unsigned int v1, unsigned int v2) const;
312  PlannerDataEdge& getEdge(unsigned int v1, unsigned int v2);
316  unsigned int getEdges(unsigned int v, std::vector<unsigned int>& edgeList) const;
319  unsigned int getEdges(unsigned int v, std::map<unsigned int, const PlannerDataEdge*> &edgeMap) const;
322  unsigned int getIncomingEdges(unsigned int v, std::vector<unsigned int>& edgeList) const;
326  unsigned int getIncomingEdges(unsigned int v, std::map<unsigned int, const PlannerDataEdge*> &edgeMap) const;
332  bool getEdgeWeight(unsigned int v1, unsigned int v2, Cost* weight) const;
336  bool setEdgeWeight(unsigned int v1, unsigned int v2, Cost weight);
339  void computeEdgeWeights(const OptimizationObjective &opt);
342  void computeEdgeWeights();
343 
347 
349  void printGraphviz(std::ostream& out = std::cout) const;
350 
352  void printGraphML(std::ostream& out = std::cout) const;
353 
357 
361  void extractMinimumSpanningTree(unsigned int v,
362  const OptimizationObjective &opt,
363  PlannerData &mst) const;
367  void extractReachable(unsigned int v, PlannerData &data) const;
368 
371  StateStoragePtr extractStateStorage() const;
372 
379  Graph& toBoostGraph();
386  const Graph& toBoostGraph() const;
387 
389 
392 
394  virtual bool hasControls() const;
395 
397  std::map<std::string, std::string> properties;
398 
399  protected:
401  std::map<const State*, unsigned int> stateIndexMap_;
403  std::vector<unsigned int> startVertexIndices_;
405  std::vector<unsigned int> goalVertexIndices_;
406 
411  std::set<State*> decoupledStates_;
412 
413  private:
414  void freeMemory();
415 
416  // Abstract pointer that points to the Boost.Graph structure.
417  // Obscured to prevent unnecessary inclusion of BGL throughout the
418  // rest of the code.
419  void* graphRaw_;
420  };
421  }
422 }
423 
424 #endif
const State * state_
The state represented by this vertex.
Definition: PlannerData.h:107
Object that handles loading/storing a PlannerData object to/from a binary stream. Serialization of ve...
SpaceInformationPtr si_
The space information instance for this data.
Definition: PlannerData.h:408
Object containing planner generated vertex and edge data. It is assumed that all vertices are unique...
Definition: PlannerData.h:163
void computeEdgeWeights()
Computes all edge weights using state space distance (i.e. getSpaceInformation()->distance()) ...
Wrapper class for the Boost.Graph representation of the PlannerData. This class inherits from a boost...
StateStoragePtr extractStateStorage() const
Extract a ompl::base::GraphStateStorage object from this PlannerData. Memory for states is copied (th...
std::vector< unsigned int > startVertexIndices_
A mutable listing of the vertices marked as start states. Stored in sorted order. ...
Definition: PlannerData.h:403
int tag_
A generic integer tag for this state. Not used for equivalence checking.
Definition: PlannerData.h:109
bool vertexExists(const PlannerDataVertex &v) const
Check whether a vertex exists with the given vertex data.
virtual void decoupleFromPlanner()
Creates a deep copy of the states contained in the vertices of this PlannerData structure so that whe...
Definition: PlannerData.cpp:79
PlannerDataVertex(const State *st, int tag=0)
Constructor. Takes a state pointer and an optional integer tag.
Definition: PlannerData.h:63
virtual bool hasControls() const
Indicate whether any information about controls (ompl::control::Control) is stored in this instance...
unsigned int addGoalVertex(const PlannerDataVertex &v)
Adds the given vertex to the graph data, and marks it as a start vertex. The vertex index is returned...
void extractReachable(unsigned int v, PlannerData &data) const
Extracts the subset of PlannerData reachable from the vertex with index v. For tree structures...
bool markStartState(const State *st)
Mark the given state as a start vertex. If the given state does not exist in a vertex, false is returned.
Graph & toBoostGraph()
Extract a Boost.Graph object from this PlannerData.
unsigned int addVertex(const PlannerDataVertex &st)
Adds the given vertex to the graph data. The vertex index is returned. Duplicates are not added...
unsigned int numGoalVertices() const
Returns the number of goal vertices.
const PlannerDataVertex & getStartVertex(unsigned int i) const
Retrieve a reference to the ith start vertex object. If i is greater than the number of start vertice...
std::vector< unsigned int > goalVertexIndices_
A mutable listing of the vertices marked as goal states. Stored in sorted order.
Definition: PlannerData.h:405
unsigned int getStartIndex(unsigned int i) const
Returns the index of the ith start state. INVALID_INDEX is returned if i is out of range...
bool getEdgeWeight(unsigned int v1, unsigned int v2, Cost *weight) const
Returns the weight of the edge between the given vertex indices. If there exists an edge between v1 a...
unsigned int getIncomingEdges(unsigned int v, std::vector< unsigned int > &edgeList) const
Returns a list of vertices with outgoing edges to the vertex with index v. The number of edges connec...
bool setEdgeWeight(unsigned int v1, unsigned int v2, Cost weight)
Sets the weight of the edge between the given vertex indices. If an edge between v1 and v2 does not e...
unsigned int getEdges(unsigned int v, std::vector< unsigned int > &edgeList) const
Returns a list of the vertex indexes directly connected to vertex with index v (outgoing edges)...
std::set< State * > decoupledStates_
A list of states that are allocated during the decoupleFromPlanner method. These states are freed by ...
Definition: PlannerData.h:411
Base class for a vertex in the PlannerData structure. All derived classes must implement the clone an...
Definition: PlannerData.h:59
virtual bool removeVertex(const PlannerDataVertex &st)
Removes the vertex associated with the given data. If the vertex does not exist, false is returned...
bool isGoalVertex(unsigned int index) const
Returns true if the given vertex index is marked as a goal vertex.
const PlannerDataVertex & getGoalVertex(unsigned int i) const
Retrieve a reference to the ith goal vertex object. If i is greater than the number of goal vertices...
bool tagState(const State *st, int tag)
Set the integer tag associated with the given state. If the given state does not exist in a vertex...
unsigned int numEdges() const
Retrieve the number of edges in this structure.
bool operator!=(const PlannerDataVertex &rhs) const
Returns true if this vertex is not equal to the argument. This is the complement of the == operator...
Definition: PlannerData.h:90
void printGraphML(std::ostream &out=std::cout) const
Writes a GraphML file of this structure to the given stream.
unsigned int numVertices() const
Retrieve the number of vertices in this structure.
unsigned int vertexIndex(const PlannerDataVertex &v) const
Return the index for the vertex associated with the given data. INVALID_INDEX is returned if this ver...
virtual bool addEdge(unsigned int v1, unsigned int v2, const PlannerDataEdge &edge=PlannerDataEdge(), Cost weight=Cost(1.0))
Adds a directed edge between the given vertex indexes. An optional edge structure and weight can be s...
PlannerDataVertex(const PlannerDataVertex &rhs)
Copy constructor.
Definition: PlannerData.h:65
A shared pointer wrapper for ompl::base::SpaceInformation.
const PlannerDataVertex & getVertex(unsigned int index) const
Retrieve a reference to the vertex object with the given index. If this vertex does not exist...
void printGraphviz(std::ostream &out=std::cout) const
Writes a Graphviz dot file of this structure to the given stream.
virtual void clear()
Clears the entire data structure.
Definition: PlannerData.cpp:73
unsigned int addStartVertex(const PlannerDataVertex &v)
Adds the given vertex to the graph data, and marks it as a start vertex. The vertex index is returned...
Definition of an abstract state.
Definition: State.h:50
virtual ~PlannerData()
Destructor.
Definition: PlannerData.cpp:62
virtual PlannerDataEdge * clone() const
Return a clone of this object, allocated from the heap.
Definition: PlannerData.h:122
static const PlannerDataVertex NO_VERTEX
Representation for a non-existant vertex.
Definition: PlannerData.h:171
Abstract definition of optimization objectives.
bool edgeExists(unsigned int v1, unsigned int v2) const
Check whether an edge between vertex index v1 and index v2 exists.
static const unsigned int INVALID_INDEX
Representation of an invalid vertex index.
Definition: PlannerData.h:173
const PlannerDataEdge & getEdge(unsigned int v1, unsigned int v2) const
Retrieve a reference to the edge object connecting vertices with indexes v1 and v2. If this edge does not exist, NO_EDGE is returned.
bool markGoalState(const State *st)
Mark the given state as a goal vertex. If the given state does not exist in a vertex, false is returned.
virtual int getTag() const
Returns the integer tag associated with this vertex.
Definition: PlannerData.h:69
bool isStartVertex(unsigned int index) const
Returns true if the given vertex index is marked as a start vertex.
virtual void setTag(int tag)
Set the integer tag associated with this vertex.
Definition: PlannerData.h:71
unsigned int numStartVertices() const
Returns the number of start vertices.
void extractMinimumSpanningTree(unsigned int v, const OptimizationObjective &opt, PlannerData &mst) const
Extracts the minimum spanning tree of the data rooted at the vertex with index v. The minimum spannin...
Base class for a PlannerData edge.
Definition: PlannerData.h:116
const SpaceInformationPtr & getSpaceInformation() const
Return the instance of SpaceInformation used in this PlannerData.
std::map< const State *, unsigned int > stateIndexMap_
A mapping of states to vertex indexes. For fast lookup of vertex index.
Definition: PlannerData.h:401
virtual PlannerDataVertex * clone() const
Return a clone of this object, allocated from the heap.
Definition: PlannerData.h:76
virtual bool operator==(const PlannerDataEdge &rhs) const
Returns true if the edges point to the same memory.
Definition: PlannerData.h:125
virtual const State * getState() const
Retrieve the state associated with this vertex.
Definition: PlannerData.h:73
unsigned int getGoalIndex(unsigned int i) const
Returns the index of the ith goal state. INVALID_INDEX is returned if i is out of range Indexes are v...
virtual bool removeEdge(unsigned int v1, unsigned int v2)
Removes the edge between vertex indexes v1 and v2. Success is returned.
virtual bool operator==(const PlannerDataVertex &rhs) const
Equivalence operator. Return true if the state pointers are equal.
Definition: PlannerData.h:82
Definition of a cost value. Can represent the cost of a motion or the cost of a state.
Definition: Cost.h:47
static const PlannerDataEdge NO_EDGE
Representation for a non-existant edge.
Definition: PlannerData.h:166
std::map< std::string, std::string > properties
Any extra properties (key-value pairs) the planner can set.
Definition: PlannerData.h:397
bool operator!=(const PlannerDataEdge &rhs) const
Returns true if the edges do not point to the same memory. This is the complement of the == operator...
Definition: PlannerData.h:132