42 #ifndef OMPL_DATASTRUCTURES_DYNAMICSSSP_H 43 #define OMPL_DATASTRUCTURES_DYNAMICSSSP_H 50 #include <boost/graph/graph_traits.hpp> 51 #include <boost/graph/adjacency_list.hpp> 52 #include <unordered_set> 61 graph_ =
new Graph(0);
68 void addVertex(std::size_t
id)
70 distance_.push_back((
id == 0) ? 0 : std::numeric_limits<double>::infinity());
71 parent_.push_back(NO_ID);
72 boost::add_vertex(
id, *graph_);
77 void addEdge(std::size_t v, std::size_t w,
double weight,
bool collectVertices,
78 std::list<std::size_t> &affectedVertices)
81 WeightProperty edge_property(weight);
82 boost::add_edge(v, w, edge_property, *graph_);
85 assert((distance_[v] == std::numeric_limits<double>::infinity()) ||
86 (distance_[w] == std::numeric_limits<double>::infinity()) ||
87 (distance_[w] + weight != distance_[w]));
89 std::vector<double> cost(boost::num_vertices(*graph_),
90 std::numeric_limits<double>::infinity());
92 IsLessThan isLessThan(cost);
93 Queue queue(isLessThan);
95 if (distance_[v] + weight < distance_[w])
97 distance_[w] = distance_[v] + weight;
104 WeightMap weights = boost::get(boost::edge_weight_t(), *graph_);
105 while (!queue.empty())
108 std::size_t u = *(queue.begin());
109 queue.erase(queue.begin());
112 affectedVertices.push_back(u);
114 boost::out_edges(u, *graph_);
117 boost::graph_traits<Graph>::out_edge_iterator ei, ei_end;
118 for (boost::tie(ei, ei_end) = boost::out_edges(u, *graph_); ei != ei_end; ++ei)
120 std::size_t x = boost::target(*ei, *graph_);
121 double edgeWeight = boost::get(weights, *ei);
123 if (distance_[u] + edgeWeight < distance_[x])
125 distance_[x] = distance_[u] + edgeWeight;
129 auto qIter = queue.find(x);
130 if (qIter != queue.end())
133 cost[x] = distance_[x] - distance_[v];
140 void removeEdge(std::size_t v, std::size_t w,
bool collectVertices, std::list<std::size_t> &affectedVertices)
143 boost::remove_edge(v, w, *graph_);
148 std::list<std::size_t> workSet;
149 IntSet affectedVerticesSet;
150 workSet.push_back(w);
152 while (!workSet.empty())
155 std::size_t u = workSet.front();
158 affectedVerticesSet.insert(u);
160 boost::graph_traits<Graph>::out_edge_iterator ei, ei_end;
161 for (boost::tie(ei, ei_end) = boost::out_edges(u, *graph_); ei != ei_end; ++ei)
163 std::size_t x = boost::target(*ei, *graph_);
165 workSet.push_back(x);
169 WeightMap weights = boost::get(boost::edge_weight_t(), *graph_);
172 IsLessThan isLessThan(distance_);
173 Queue queue(isLessThan);
174 for (
auto set_iter = affectedVerticesSet.begin(); set_iter != affectedVerticesSet.end(); ++set_iter)
176 std::size_t a = *set_iter;
177 distance_[a] = std::numeric_limits<double>::infinity();
181 boost::graph_traits<Graph>::in_edge_iterator ei, ei_end;
182 for (boost::tie(ei, ei_end) = boost::in_edges(a, *graph_); ei != ei_end; ++ei)
184 std::size_t b = boost::source(*ei, *graph_);
185 if (affectedVerticesSet.find(b) == affectedVerticesSet.end())
187 double edgeWeight = boost::get(weights, *ei);
189 if (distance_[b] + edgeWeight < distance_[a])
191 distance_[a] = distance_[b] + edgeWeight;
196 if (distance_[a] != std::numeric_limits<double>::infinity())
200 while (!queue.empty())
203 std::size_t a = *queue.begin();
204 queue.erase(queue.begin());
207 affectedVertices.push_back(a);
210 boost::graph_traits<Graph>::out_edge_iterator ei, ei_end;
211 for (boost::tie(ei, ei_end) = boost::out_edges(a, *graph_); ei != ei_end; ++ei)
213 int c = boost::target(*ei, *graph_);
214 double edgeWeight = boost::get(weights, *ei);
216 if (distance_[a] + edgeWeight < distance_[c])
218 distance_[c] = distance_[a] + edgeWeight;
222 auto qIter = queue.find(c);
223 if (qIter != queue.end())
232 double getShortestPathCost(std::size_t u)
const 234 return this->distance_[u];
237 std::size_t getShortestPathParent(std::size_t u)
const 243 using WeightProperty = boost::property<boost::edge_weight_t, double>;
244 using Graph = boost::adjacency_list<boost::vecS,
246 boost::bidirectionalS,
249 using WeightMap = boost::property_map<Graph, boost::edge_weight_t>::type;
251 static const int NO_ID = -1;
256 IsLessThan(std::vector<double> &cost) : cost_(cost)
260 bool operator()(std::size_t id1, std::size_t id2)
const 262 return (cost_[id1] < cost_[id2]);
266 std::vector<double> &cost_;
269 using Queue = std::set<std::size_t, IsLessThan>;
270 using QueueIter = Queue::iterator;
271 using IntSet = std::unordered_set<std::size_t>;
272 using IntSetIter = IntSet::iterator;
276 std::vector<double> distance_;
278 std::vector<std::size_t> parent_;
282 #endif // OMPL_DATASTRUCTURES_DYNAMICSSSP_H Main namespace. Contains everything in this library.