Skip to content

Commit d56038b

Browse files
authored
Visitor Templates for BGL Solver (#80)
* Created header for event visitors * Added event visitor as template argument to solver base classes * Revised implementations of Dijkstra search to use event visitor template parameter * Revised explicit template instantiations of BGL solver classes * Updated unit test solver factory to work with new BGL event visitor template parameter * Updated unit test * Updated benchmarks * Removed unnecessary FloatType template parameter from visitors * Implemented macro to simplify explicit template instantiation of BGL solvers * Clang formatting * Added check of target vertex cost to Dijkstra search method
1 parent 228dfc0 commit d56038b

13 files changed

Lines changed: 304 additions & 374 deletions

File tree

descartes_light/solvers/include/descartes_light/solvers/bgl/bgl_dijkstra_solver.h

Lines changed: 11 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -2,75 +2,41 @@
22
#define DESCARTES_LIGHT_SOLVERS_BGL_BGL_DIJKSTRA_SOLVER_H
33

44
#include <descartes_light/solvers/bgl/bgl_solver.h>
5+
#include <descartes_light/solvers/bgl/event_visitors.h>
56

67
namespace descartes_light
78
{
89
/**
910
* @brief BGL solver implementation that constructs vertices and edges in the build function and uses Dijkstra's
1011
* algorithm with a default visitor to search the graph
1112
*/
12-
template <typename FloatType>
13-
class BGLDijkstraSVSESolver : public BGLSolverBaseSVSE<FloatType>
13+
template <typename FloatType, typename Visitors>
14+
class BGLDijkstraSVSESolver : public BGLSolverBaseSVSE<FloatType, Visitors>
1415
{
1516
public:
16-
using BGLSolverBaseSVSE<FloatType>::BGLSolverBaseSVSE;
17+
using BGLSolverBaseSVSE<FloatType, Visitors>::BGLSolverBaseSVSE;
1718

1819
SearchResult<FloatType> search() override;
1920
};
2021

21-
using BGLDijkstraSVSESolverF = BGLDijkstraSVSESolver<float>;
22-
using BGLDijkstraSVSESolverD = BGLDijkstraSVSESolver<double>;
23-
24-
/**
25-
* @brief BGL solver implementation that constructs vertices and edges in the build function and uses Dijkstra's
26-
* algorithm with a visitor that terminates the search once a vertex in the last rung of the graph is encountered rather
27-
* than allowing it to continue until the distance to all nodes in the graph has been calculated
28-
*/
29-
template <typename FloatType>
30-
class BGLEfficientDijkstraSVSESolver : public BGLSolverBaseSVSE<FloatType>
31-
{
32-
public:
33-
using BGLSolverBaseSVSE<FloatType>::BGLSolverBaseSVSE;
34-
35-
SearchResult<FloatType> search() override;
36-
};
37-
38-
using BGLEfficientDijkstraSVSESolverF = BGLEfficientDijkstraSVSESolver<float>;
39-
using BGLEfficientDijkstraSVSESolverD = BGLEfficientDijkstraSVSESolver<double>;
22+
using BGLDijkstraSVSESolverF = BGLDijkstraSVSESolver<float, early_terminator<boost::on_examine_vertex>>;
23+
using BGLDijkstraSVSESolverD = BGLDijkstraSVSESolver<double, early_terminator<boost::on_examine_vertex>>;
4024

4125
/**
4226
* @brief BGL solver implementation that constructs vertices build function and uses Dijkstra's
4327
* algorithm with an edge-adding visitor to search the graph
4428
*/
45-
template <typename FloatType>
46-
class BGLDijkstraSVDESolver : public BGLSolverBaseSVDE<FloatType>
47-
{
48-
public:
49-
using BGLSolverBaseSVDE<FloatType>::BGLSolverBaseSVDE;
50-
51-
SearchResult<FloatType> search() override;
52-
};
53-
54-
using BGLDijkstraSVDESolverF = BGLDijkstraSVDESolver<float>;
55-
using BGLDijkstraSVDESolverD = BGLDijkstraSVDESolver<double>;
56-
57-
/**
58-
* @brief BGL solver implementation that constructs vertices in the build function and uses Dijkstra's
59-
* algorithm with a visitor that adds edges and terminates the search once a vertex in the last rung of
60-
* the graph is encountered rather than allowing it to continue until the distance to all nodes in the
61-
* graph has been calculated
62-
*/
63-
template <typename FloatType>
64-
class BGLEfficientDijkstraSVDESolver : public BGLSolverBaseSVDE<FloatType>
29+
template <typename FloatType, typename Visitors>
30+
class BGLDijkstraSVDESolver : public BGLSolverBaseSVDE<FloatType, Visitors>
6531
{
6632
public:
67-
using BGLSolverBaseSVDE<FloatType>::BGLSolverBaseSVDE;
33+
using BGLSolverBaseSVDE<FloatType, Visitors>::BGLSolverBaseSVDE;
6834

6935
SearchResult<FloatType> search() override;
7036
};
7137

72-
using BGLEfficientDijkstraSVDESolverF = BGLEfficientDijkstraSVDESolver<float>;
73-
using BGLEfficientDijkstraSVDESolverD = BGLEfficientDijkstraSVDESolver<double>;
38+
using BGLDijkstraSVDESolverF = BGLDijkstraSVDESolver<float, early_terminator<boost::on_examine_vertex>>;
39+
using BGLDijkstraSVDESolverD = BGLDijkstraSVDESolver<double, early_terminator<boost::on_examine_vertex>>;
7440

7541
} // namespace descartes_light
7642

descartes_light/solvers/include/descartes_light/solvers/bgl/bgl_solver.h

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,11 @@ namespace descartes_light
3131
/**
3232
* @brief Partial implementation for solvers leveraging the Boost Graph Library
3333
*/
34-
template <typename FloatType>
34+
template <typename FloatType, typename Visitors>
3535
class BGLSolverBase : public Solver<FloatType>
3636
{
3737
public:
38-
BGLSolverBase(unsigned num_threads = std::thread::hardware_concurrency());
38+
BGLSolverBase(Visitors event_visitors, unsigned num_threads = std::thread::hardware_concurrency());
3939

4040
inline const BGLGraph<FloatType>& getGraph() const { return graph_; }
4141

@@ -56,6 +56,9 @@ class BGLSolverBase : public Solver<FloatType>
5656
*/
5757
std::vector<typename State<FloatType>::ConstPtr> toStates(const std::vector<VertexDesc<FloatType>>& path) const;
5858

59+
/** @brief Event visitors for custom behavior in the search */
60+
Visitors event_visitors_;
61+
/** @brief Number of threads for parallel processing */
5962
unsigned num_threads_;
6063
/** @brief Graph representation of the planning problem */
6164
BGLGraph<FloatType> graph_;
@@ -73,11 +76,11 @@ class BGLSolverBase : public Solver<FloatType>
7376
* @details Constructs only vertices in the build function (i.e. statically) with the assumption that edges will be
7477
* added during the search (i.e. dynamically)
7578
*/
76-
template <typename FloatType>
77-
class BGLSolverBaseSVDE : public BGLSolverBase<FloatType>
79+
template <typename FloatType, typename Visitors>
80+
class BGLSolverBaseSVDE : public BGLSolverBase<FloatType, Visitors>
7881
{
7982
public:
80-
using BGLSolverBase<FloatType>::BGLSolverBase;
83+
using BGLSolverBase<FloatType, Visitors>::BGLSolverBase;
8184

8285
BuildStatus buildImpl(const std::vector<typename WaypointSampler<FloatType>::ConstPtr>& trajectory,
8386
const std::vector<typename EdgeEvaluator<FloatType>::ConstPtr>& edge_eval,
@@ -91,11 +94,11 @@ class BGLSolverBaseSVDE : public BGLSolverBase<FloatType>
9194
* @brief BGL solver Static Vertex Static Edge (SVSE) partial implementation
9295
* @details Constructs both vertices and edges in the build function (i.e. statically)
9396
*/
94-
template <typename FloatType>
95-
class BGLSolverBaseSVSE : public BGLSolverBaseSVDE<FloatType>
97+
template <typename FloatType, typename Visitors>
98+
class BGLSolverBaseSVSE : public BGLSolverBaseSVDE<FloatType, Visitors>
9699
{
97100
public:
98-
using BGLSolverBaseSVDE<FloatType>::BGLSolverBaseSVDE;
101+
using BGLSolverBaseSVDE<FloatType, Visitors>::BGLSolverBaseSVDE;
99102

100103
BuildStatus buildImpl(const std::vector<typename WaypointSampler<FloatType>::ConstPtr>& trajectory,
101104
const std::vector<typename EdgeEvaluator<FloatType>::ConstPtr>& edge_eval,

descartes_light/solvers/include/descartes_light/solvers/bgl/event_visitor.h

Lines changed: 0 additions & 4 deletions
This file was deleted.
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#ifndef DESCARTES_LIGHT_SOLVERS_BGL_EVENT_VISITORS
2+
#define DESCARTES_LIGHT_SOLVERS_BGL_EVENT_VISITORS
3+
4+
#include <descartes_light/solvers/bgl/boost_graph_types.h>
5+
6+
#include <descartes_light/descartes_macros.h>
7+
DESCARTES_IGNORE_WARNINGS_PUSH
8+
#include <boost/graph/visitors.hpp>
9+
DESCARTES_IGNORE_WARNINGS_POP
10+
11+
namespace descartes_light
12+
{
13+
/**
14+
* @brief Event visitor that terminates the search when a vertex in the last rung of the graph is examined
15+
* @details Throws the vertex descriptor that is the termination of the path once a vertex in the last rung of
16+
* the graph is operated on
17+
*/
18+
template <typename EventType>
19+
struct early_terminator : public boost::base_visitor<early_terminator<EventType>>
20+
{
21+
/** @brief Event filter typedef defining the events for which this visitor can be used */
22+
typedef EventType event_filter;
23+
24+
early_terminator(long last_rung_idx);
25+
26+
template <typename FloatType>
27+
void operator()(VertexDesc<FloatType> u, const BGLGraph<FloatType>& g);
28+
29+
const long last_rung_idx_;
30+
};
31+
32+
/**
33+
* @brief Event visitor that adds all edges to each vertex dynamically as the vertex is discovered during the graph
34+
* search
35+
*/
36+
template <typename FloatType, typename EventType>
37+
struct add_all_edges_dynamically : public boost::base_visitor<add_all_edges_dynamically<FloatType, EventType>>
38+
{
39+
/** @brief Event filter typedef defining the events for which this visitor can be used */
40+
typedef EventType event_filter;
41+
42+
add_all_edges_dynamically(std::vector<typename EdgeEvaluator<FloatType>::ConstPtr> edge_eval,
43+
std::vector<std::vector<VertexDesc<FloatType>>> ladder_rungs);
44+
45+
void operator()(VertexDesc<FloatType> u, const BGLGraph<FloatType>& g);
46+
47+
const std::vector<typename EdgeEvaluator<FloatType>::ConstPtr> eval_;
48+
const std::vector<std::vector<VertexDesc<FloatType>>> ladder_rungs_;
49+
};
50+
51+
/**
52+
* @brief Event visitor for updating vertex cost
53+
*/
54+
struct cost_recorder : public boost::base_visitor<cost_recorder>
55+
{
56+
/** @brief Event filter typedef defining the events for which this visitor can be used */
57+
typedef boost::on_tree_edge event_filter;
58+
59+
template <typename FloatType>
60+
void operator()(EdgeDesc<FloatType> e, const BGLGraph<FloatType>& g);
61+
};
62+
63+
} // namespace descartes_light
64+
65+
#endif // DESCARTES_LIGHT_SOLVERS_BGL_EVENT_VISITORS_H

0 commit comments

Comments
 (0)