batching_pomp  1.0
This is an implementation of an algorithmic framework for anytime motion planning on large dense roadmaps.
Selector.hpp
1 /***********************************************************************
2 Copyright (c) 2017, Shushman Choudhury
3 All rights reserved.
4 Authors: Shushman Choudhury <shushmanchoudhury@gmail.com>
5 
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions are
8 met:
9  Redistributions of source code must retain the above copyright
10  notice, this list of conditions and the following disclaimer.
11  Redistributions in binary form must reproduce the above copyright
12  notice, this list of conditions and the following disclaimer in the
13  documentation and/or other materials provided with the distribution.
14 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
15 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
16 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
17 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
18 HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
19 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
20 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 *************************************************************************/
26 
27 #ifndef BATCHING_POMP_UTIL_SELECTOR_HPP_
28 #define BATCHING_POMP_UTIL_SELECTOR_HPP_
29 
30 #include <algorithm>
31 #include <stdexcept>
32 #include <memory>
33 #include <functional>
34 #include <boost/property_map/dynamic_property_map.hpp>
35 #include <boost/graph/graphml.hpp>
36 #include <boost/graph/adjacency_list.hpp>
37 
38 namespace batching_pomp {
39 namespace util {
40 
42 
46 template<class Graph>
47 class Selector
48 {
49 
50 typedef boost::graph_traits<Graph> GraphTypes;
51 typedef typename GraphTypes::edge_descriptor Edge;
52 
53 public:
54 
57  Selector(const std::string& _type)
58  : mType{_type}
59  {
60  if(mType == "normal") {
61 
62  mSelectEdgeFnPtr = std::bind(&Selector<Graph>::selectEdgesNormal,this,std::placeholders::_1,std::placeholders::_2);
63  }
64  else if(mType == "alternate") {
65  mSelectEdgeFnPtr = std::bind(&Selector<Graph>::selectEdgesAlternate,this,std::placeholders::_1,std::placeholders::_2);
66  }
67  else if(mType == "failfast") {
68  mSelectEdgeFnPtr = std::bind(&Selector<Graph>::selectEdgesFailFast,this,std::placeholders::_1,std::placeholders::_2);
69  }
70  else if(mType == "maxinf") {
71  mSelectEdgeFnPtr = std::bind(&Selector<Graph>::selectEdgesMaxInf,this,std::placeholders::_1,std::placeholders::_2);
72  }
73  else {
74  throw std::invalid_argument("Invalid selector type specified - "+mType+"!");
75  }
76  }
77 
78  std::string getType()
79  {
80  return mType;
81  }
82 
84  std::vector<Edge> selectEdges(const Graph& g, const std::vector<Edge>& epath) const
85  {
86  return mSelectEdgeFnPtr(g,epath);
87  }
88 
89 
90 private:
91  // For a pair of edge and probability of collision
92  using EdgePair = std::pair<double,Edge>;
93 
94  // Just return in order of edges on path
95  std::vector<Edge> selectEdgesNormal(const Graph& g, const std::vector<Edge>& epath) const
96  {
97  return epath;
98  }
99 
100  // Return reverse order of edges
101  std::vector<Edge> selectEdgesAlternate(const Graph& g, const std::vector<Edge>& epath) const
102  {
103  std::vector<Edge> reverseEpath(epath);
104  std::reverse(reverseEpath.begin(),reverseEpath.end());
105  return reverseEpath;
106  }
107 
108  // Return in order of highest probability of collision
109  std::vector<Edge> selectEdgesFailFast(const Graph& g, const std::vector<Edge>& epath) const
110  {
111  std::vector < EdgePair > edgeProbList;
112  edgeProbList.reserve(epath.size());
113  for(Edge e : epath) {
114  edgeProbList.push_back(std::make_pair(g[e].probFree,e));
115  }
116 
117  std::sort(edgeProbList.begin(), edgeProbList.end());
118 
119  std::vector<Edge> orderedEdges;
120  orderedEdges.reserve(edgeProbList.size());
121  for(EdgePair ep : edgeProbList) {
122  orderedEdges.push_back(ep.second);
123  }
124 
125  return orderedEdges;
126  }
127 
128  // Return in order or most uncertain (closest to 0.5 probability)
129  std::vector<Edge> selectEdgesMaxInf(const Graph& g, const std::vector<Edge>& epath) const
130  {
131  std::vector < EdgePair > edgeProbList;
132  edgeProbList.reserve(epath.size());
133  for(Edge e : epath) {
134  edgeProbList.push_back( std::make_pair(std::fabs(g[e].probFree-0.5),e) );
135  }
136 
137  std::sort(edgeProbList.begin(), edgeProbList.end());
138 
139  std::vector<Edge> orderedEdges;
140  orderedEdges.reserve(edgeProbList.size());
141  for(EdgePair ep : edgeProbList) {
142  orderedEdges.push_back(ep.second);
143  }
144 
145  return orderedEdges;
146  }
147 
148  std::string mType;
149  std::function< std::vector<Edge>(const Graph& g, const std::vector<Edge>& epath) > mSelectEdgeFnPtr;
150 
151 };
152 
153 } // namespace util
154 } // namespace batching_pomp
155 
156 #endif // BATCHING_POMP_UTIL_SELECTOR_HPP_
Definition: BatchingManager.hpp:36
A selector assigns an ordering for evaluating edges on a candidate path.
Definition: Selector.hpp:47
std::vector< Edge > selectEdges(const Graph &g, const std::vector< Edge > &epath) const
Call edge selector function pointer.
Definition: Selector.hpp:84
Selector(const std::string &_type)
Definition: Selector.hpp:57