1#ifndef RAPIDLY_EXPLORING_RANDOM_TREE_H
2#define RAPIDLY_EXPLORING_RANDOM_TREE_H
4#include "cubic_engine/base/cubic_engine_types.h"
5#include "kernel/data_structs/boost_serial_graph.h"
6#include "kernel/base/kernel_consts.h"
8#include"boost/noncopyable.hpp"
26template<
typename NodeData,
typename EdgeData>
27class RRT:
private boost::noncopyable
95 std::pair<adjacency_iterator, adjacency_iterator>
97 return tree_.get_vertex_neighbors(
id);
103 std::pair<adjacency_iterator, adjacency_iterator>
105 return tree_.get_vertex_neighbors(v);
125 return tree_.get_edge(v1, v2);
134 return tree_.get_edge(v1, v2);
140 std::pair<edge_iterator, edge_iterator>
edges()
const{
141 return tree_.edges();
150 return tree_.add_edge(v1, v2);
158 template<
typename MetricTp>
160 const MetricTp& metric)
const;
167 template<
typename MetricTp>
169 const MetricTp& metric)
const;
186 uint_t
n_edges()
const{
return tree_.n_edges();}
196 template<
typename StateSelector,
197 typename MetricTp,
typename DynamicsTp>
199 const StateSelector& state_selector,
200 const MetricTp& metric,
201 DynamicsTp& dynamics);
210 template<
typename StateSelector,
typename MetricTp,
typename DynamicsTp>
211 std::tuple<bool, uint_t, uint_t>
213 const vertex_t& goal,
const StateSelector& state_selector,
214 const MetricTp& metric, DynamicsTp& dynamics, real_t goal_radius);
221 kernel::BoostSerialGraph<vertex_data_t, edge_data_t> tree_;
227 bool show_iterations_;
231template<
typename NodeData,
typename EdgeData>
235 show_iterations_(false)
238template<
typename NodeData,
typename EdgeData>
241 return tree_.add_vertex(data);
244template<
typename NodeTp,
typename EdgeTp>
245template<
typename StateSelector,
typename MetricTp,
typename DynamicsTp>
248 const StateSelector& state_selector,
249 const MetricTp& metric,
250 DynamicsTp& dynamics){
252 std::chrono::time_point<std::chrono::system_clock> start, end;
253 start = std::chrono::system_clock::now();
259 tree_.add_vertex(xinit.data);
263 for(uint_t itr=0; itr<nitrs; ++itr){
265 if(show_iterations_){
266 std::cout<<
"At iteration: "<<itr<<std::endl;
270 auto xrand = state_selector();
274 auto& xnear = find_nearest_neighbor(xrand, metric);
280 auto [xnew, u] = dynamics(xnear, xrand);
283 auto& new_v = add_vertex(xnew);
286 auto new_e = add_edge(xnear.id, new_v.id);
290 end = std::chrono::system_clock::now();
292 if(show_iterations_){
293 std::chrono::duration<real_t> dur = end - start;
294 std::cout<<
"Total build time: "<<dur.count()<<std::endl;
298template<
typename NodeData,
typename EdgeData>
299template<
typename StateSelector,
typename MetricTp,
typename DynamicsTp>
300std::tuple<bool, uint_t, uint_t>
302 const vertex_t& goal,
const StateSelector& state_selector,
303 const MetricTp& metric, DynamicsTp& dynamics, real_t goal_radius){
306 std::chrono::time_point<std::chrono::system_clock> start, end;
307 start = std::chrono::system_clock::now();
314 if( metric(xinit, goal) < goal_radius ){
315 return std::make_tuple(
true, 0, 0);
319 auto& root = tree_.add_vertex(xinit.data);
322 bool goal_found =
false;
325 uint_t last_v_id = kernel::KernelConsts::invalid_size_type();
329 for(uint_t itr=0; (itr<nitrs && !goal_found); ++itr){
336 auto xrand = state_selector();
340 auto& xnear = find_nearest_neighbor(xrand, metric);
346 auto [xnew, u] = dynamics(xnear, xrand);
349 auto& new_v = add_vertex(xnew);
352 auto new_e = add_edge(xnear.id, new_v.id);
357 if(metric(new_v, goal) < goal_radius ){
358 last_v_id = new_v.id;
364 end = std::chrono::system_clock::now();
367 std::chrono::duration<real_t> dur = end - start;
368 std::cout<<
"Total build time: "<<dur.count()<<std::endl;
371 return std::make_tuple(goal_found, root.id, last_v_id);
374template<
typename NodeData,
typename EdgeData>
375template<
typename MetricTp>
378 const MetricTp& metric)
const{
380 auto dist = metric(tree_.get_vertex(0), other);
383 for(uint_t v=1; v< tree_.n_vertices(); ++v){
384 auto vertex_data = tree_.get_vertex(v);
386 auto new_dist = metric(vertex_data, other);
394 return tree_.get_vertex(result);
397template<
typename NodeData,
typename EdgeData>
398template<
typename MetricTp>
401 const MetricTp& metric)
const{
407 auto dist = metric(tree_.get_vertex(0), dummy);
410 for(uint_t v=1; v< tree_.n_vertices(); ++v){
411 auto vertex_data = tree_.get_vertex(v);
413 auto new_dist = metric(vertex_data, dummy);
421 return tree_.get_vertex(result);
The RRT class models a Rapidly-Exploring Random Tree see: http://msl.cs.uiuc.edu/~lavalle/papers/Lav9...
Definition rapidly_exploring_random_tree.h:28
std::pair< adjacency_iterator, adjacency_iterator > get_vertex_neighbors(const vertex_t &v) const
Returns the neighboring vertices for the given vertex id.
Definition rapidly_exploring_random_tree.h:104
RRT()
RRT Default constructor. Creates an empty tree.
Definition rapidly_exploring_random_tree.h:232
const edge_t & get_edge(uint_t v1, uint_t v2) const
get_edge Returns the edge between the vertices v1 and v2
Definition rapidly_exploring_random_tree.h:133
vertex_t & add_vertex(const vertex_t &node)
add_vertex Add a new vertex to the tree
Definition rapidly_exploring_random_tree.h:112
const vertex_t & get_vertex(uint_t v) const
get_vertex Returns the v-th vertex
Definition rapidly_exploring_random_tree.h:78
edge_t & get_edge(uint_t v1, uint_t v2)
get_edge Returns the edge between the vertices v1 and v2
Definition rapidly_exploring_random_tree.h:124
kernel::BoostSerialGraph< vertex_data_t, edge_data_t >::edge_iterator edge_iterator
edge_iterator Edge iterator
Definition rapidly_exploring_random_tree.h:57
EdgeData edge_data_t
edge_data_t The type of the edge data
Definition rapidly_exploring_random_tree.h:39
kernel::BoostSerialGraph< vertex_data_t, edge_data_t >::edge_t edge_t
edge_t The edge type
Definition rapidly_exploring_random_tree.h:51
NodeData vertex_data_t
vertex_data_t The type of node data
Definition rapidly_exploring_random_tree.h:34
edge_t & add_edge(uint_t v1, uint_t v2)
add_edge Add a new edge between the vertices v1 and v2
Definition rapidly_exploring_random_tree.h:149
uint_t n_vertices() const
n_vertices. Returns the number of vertices of the tree
Definition rapidly_exploring_random_tree.h:180
uint_t n_edges() const
n_edges. Returns the number of edges of the tree
Definition rapidly_exploring_random_tree.h:186
void set_show_iterations_flag(bool val)
set_show_iterations_flag Set the show_iterations_ flag
Definition rapidly_exploring_random_tree.h:191
std::pair< edge_iterator, edge_iterator > edges() const
edges Access the edges of the tree
Definition rapidly_exploring_random_tree.h:140
void clear()
clear Clear the underlying tree
Definition rapidly_exploring_random_tree.h:174
vertex_t & get_vertex(uint_t v)
get_vertex Returns the v-th vertex
Definition rapidly_exploring_random_tree.h:73
const vertex_t & find_nearest_neighbor(const vertex_t &other, const MetricTp &metric) const
find_nearest_neighbor find the nearest neighbor of other node in this tree
Definition rapidly_exploring_random_tree.h:377
vertex_t & get_vertex(adjacency_iterator itr)
Access the vertex given the vertex descriptor This is needed when accessing the vertices using the ad...
Definition rapidly_exploring_random_tree.h:84
kernel::BoostSerialGraph< vertex_data_t, edge_data_t >::adjacency_iterator adjacency_iterator
adjacency_iterator Adjacency iterator
Definition rapidly_exploring_random_tree.h:63
std::pair< adjacency_iterator, adjacency_iterator > get_vertex_neighbors(uint_t id) const
Returns the neighboring vertices for the given vertex id.
Definition rapidly_exploring_random_tree.h:96
kernel::BoostSerialGraph< vertex_data_t, edge_data_t >::vertex_t vertex_t
vertex_t the vertex type
Definition rapidly_exploring_random_tree.h:45
const vertex_t & get_vertex(adjacency_iterator itr) const
Access the vertex given the vertex descriptor This is needed when accessing the vertices using the ad...
Definition rapidly_exploring_random_tree.h:90
void build(uint_t nitrs, const vertex_t &xinit, const StateSelector &state_selector, const MetricTp &metric, DynamicsTp &dynamics)
Build the tree.
Definition rapidly_exploring_random_tree.h:247
Definition rapidly_exploring_random_tree.h:13