ProductGraph.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: Matt Maly */
36 
37 #ifndef OMPL_CONTROL_PLANNERS_LTL_PRODUCTGRAPH_
38 #define OMPL_CONTROL_PLANNERS_LTL_PRODUCTGRAPH_
39 
40 #include "ompl/base/State.h"
41 #include "ompl/control/planners/ltl/Automaton.h"
42 #include "ompl/control/planners/ltl/PropositionalDecomposition.h"
43 #include "ompl/util/ClassForward.h"
44 #include <boost/graph/adjacency_list.hpp>
45 #include <functional>
46 #include <unordered_map>
47 #include <map>
48 #include <ostream>
49 #include <vector>
50 
51 namespace ompl
52 {
53  namespace control
54  {
56 
57  OMPL_CLASS_FORWARD(ProductGraph);
59 
68  {
69  public:
70  class State;
71 
73 
74  struct HashState
75  {
76  std::size_t operator()(const State &s) const;
77  };
79 
84  class State
85  {
86  friend class ProductGraph;
87  public:
90  State(void)
91  : decompRegion(-1),
92  cosafeState(-1),
93  safeState(-1) { }
94 
96  State(const State& s)
97  : decompRegion(s.decompRegion),
98  cosafeState(s.cosafeState),
99  safeState(s.safeState) { }
100 
104  bool operator==(const State& s) const;
105 
109  bool isValid(void) const;
110 
111 
112  friend struct HashState;
113 
115  friend std::ostream& operator<<(std::ostream& out, const State& s);
116 
118  int getDecompRegion(void) const;
119 
121  int getCosafeState(void) const;
122 
124  int getSafeState(void) const;
125 
126  private:
127  int decompRegion;
128  int cosafeState;
129  int safeState;
130  };
131 
134  ProductGraph(
135  const PropositionalDecompositionPtr& decomp,
136  const AutomatonPtr& cosafetyAut,
137  const AutomatonPtr& safetyAut
138  );
139 
143  ProductGraph(
144  const PropositionalDecompositionPtr& decomp,
145  const AutomatonPtr& cosafetyAut
146  );
147 
148  ~ProductGraph();
149 
153 
156  const AutomatonPtr& getCosafetyAutom() const;
157 
160  const AutomatonPtr& getSafetyAutom() const;
161 
170  std::vector<State*> computeLead(State* start, const std::function<double(State*, State*)>& edgeWeight);
171 
173  void clear();
174 
180  void buildGraph(State* start, const std::function<void(State*)>& initialize = ProductGraph::noInit);
181 
188  bool isSolution(const State* s) const;
189 
191  State* getStartState(void) const;
192 
195  double getRegionVolume(const State* s);
196 
199  int getCosafeAutDistance(const State* s) const;
200 
203  int getSafeAutDistance(const State* s) const;
204 
208  State* getState(const base::State* cs) const;
209 
213  State* getState(const base::State* cs, int cosafe, int safe) const;
214 
218  State* getState(const State* parent, int nextRegion) const;
219 
224  State* getState(const State* parent, const base::State* cs) const;
225 
228  State* getState(int region, int cosafe, int safe) const
229  {
230  State s;
231  s.decompRegion = region;
232  s.cosafeState = cosafe;
233  s.safeState = safe;
234  State*& ret = stateToPtr_[s];
235  if (ret == nullptr) ret = new State(s);
236  return ret;
237  }
238 
239  protected:
240  static void noInit(State* s)
241  {
242  }
243  struct Edge
244  {
245  double cost;
246  };
247 
248  typedef boost::adjacency_list<boost::vecS, boost::vecS, boost::directedS, State*, Edge> GraphType;
249  typedef boost::graph_traits<GraphType>::vertex_descriptor Vertex;
250  typedef boost::graph_traits<GraphType>::vertex_iterator VertexIter;
251  typedef boost::property_map<GraphType, boost::vertex_index_t>::type VertexIndexMap;
252  typedef boost::graph_traits<GraphType>::edge_iterator EdgeIter;
253 
255  AutomatonPtr cosafety_;
256  AutomatonPtr safety_;
257  GraphType graph_;
258  State* startState_;
259  std::vector<State*> solutionStates_;
260 
261  /* Only one State pointer will be allocated for each possible State
262  in the ProductGraph. There will exist situations in which
263  all we have are the component values (region, automaton states)
264  of a State and we want the actual State pointer.
265  We use this map to access it. */
266  mutable std::unordered_map<State, State*, HashState> stateToPtr_;
267 
268  /* Map from State pointer to the index of the corresponding vertex
269  in the graph. */
270  std::unordered_map<State*, int> stateToIndex_;
271  };
272  }
273 }
274 #endif
double getRegionVolume(const State *s)
Helper method to return the volume of the PropositionalDecomposition region corresponding to the give...
A shared pointer wrapper for ompl::control::PropositionalDecomposition.
State * getState(const base::State *cs) const
Returns a ProductGraph State with initial co-safety and safety Automaton states, and the Propositiona...
A ProductGraph represents the weighted, directed, graph-based Cartesian product of a PropositionalDec...
Definition: ProductGraph.h:67
std::ostream & operator<<(std::ostream &out, const ScopedState< T > &state)
Overload stream output operator. Calls ompl::base::StateSpace::printState()
Definition: ScopedState.h:497
int getSafeAutDistance(const State *s) const
Helper method to return the distance from a given State&#39;s safety state to an accepting state in the s...
State * getStartState(void) const
Returns the initial State of this ProductGraph.
Main namespace. Contains everything in this library.
Definition: Cost.h:42
const PropositionalDecompositionPtr & getDecomp() const
Returns the PropositionalDecomposition contained within this ProductGraph.
int getCosafeAutDistance(const State *s) const
Helper method to return the distance from a given State&#39;s co-safety state to an accepting state in th...
std::vector< State * > computeLead(State *start, const std::function< double(State *, State *)> &edgeWeight)
Returns a shortest-path sequence of ProductGraph states, beginning with a given initial State and end...
const AutomatonPtr & getSafetyAutom() const
Returns the safe Automaton contained within this ProductGraph.
A State of a ProductGraph represents a vertex in the graph-based Cartesian product represented by the...
Definition: ProductGraph.h:84
State(void)
Creates a State without any assigned PropositionalDecomposition region or Automaton states...
Definition: ProductGraph.h:90
Definition of an abstract state.
Definition: State.h:50
void clear()
Clears all memory belonging to this ProductGraph.
State(const State &s)
Basic copy constructor for State.
Definition: ProductGraph.h:96
A shared pointer wrapper for ompl::control::Automaton.
State * getState(int region, int cosafe, int safe) const
Returns the ProductGraph state corresponding to the given region, co-safety state, and safety state.
Definition: ProductGraph.h:228
bool isSolution(const State *s) const
Returns whether the given State is an accepting State in this ProductGraph. We call a State accepting...
ProductGraph(const PropositionalDecompositionPtr &decomp, const AutomatonPtr &cosafetyAut, const AutomatonPtr &safetyAut)
Initializes a ProductGraph with a given PropositionalDecomposition, co-safe Automaton, and safe Automaton.
void buildGraph(State *start, const std::function< void(State *)> &initialize=ProductGraph::noInit)
Constructs this ProductGraph beginning with a given initial State, using a breadth-first search...
const AutomatonPtr & getCosafetyAutom() const
Returns the co-safe Automaton contained within this ProductGraph.