10 #ifndef SYMBOLIC_PLANNING_BREADTH_FIRST_SEARCH_H_
11 #define SYMBOLIC_PLANNING_BREADTH_FIRST_SEARCH_H_
24 template <
typename NodeT>
30 const NodeT& root,
size_t max_depth,
bool verbose =
false,
31 std::chrono::microseconds us_timeout = std::chrono::microseconds(0))
32 : max_depth_(max_depth),
35 timeout_(us_timeout) {}
44 const size_t max_depth_;
46 const std::chrono::microseconds timeout_;
51 template <
typename NodeT>
54 using iterator_category = std::input_iterator_tag;
55 using value_type = std::vector<NodeT>;
56 using difference_type = ptrdiff_t;
57 using pointer =
const value_type*;
58 using reference =
const value_type&;
63 queue_({{bfs_->root_, std::make_shared<std::vector<NodeT>>()}}) {}
67 bool operator==(
const iterator& other)
const {
68 return IsFinished() && other.IsFinished();
71 bool operator!=(
const iterator& other)
const {
return !(*
this == other); }
73 reference operator*()
const {
return *ancestors_; }
77 bool IsFinished()
const {
return queue_.empty() && !ancestors_; }
79 std::queue<std::pair<NodeT, std::shared_ptr<std::vector<NodeT>>>> queue_;
80 std::shared_ptr<std::vector<NodeT>> ancestors_;
83 template <
typename NodeT>
86 const auto t_start = std::chrono::high_resolution_clock::now();
88 while (!queue_.empty()) {
90 if (bfs_->timeout_.count() > 0 &&
91 std::chrono::high_resolution_clock::now() - t_start > bfs_->timeout_) {
92 std::queue<std::pair<NodeT, std::shared_ptr<std::vector<NodeT>>>> empty;
93 std::swap(queue_, empty);
97 std::pair<NodeT, std::shared_ptr<std::vector<NodeT>>>& front =
101 ancestors_ = std::make_shared<std::vector<NodeT>>(*front.second);
102 ancestors_->push_back(std::move(front.first));
104 const NodeT& node = ancestors_->back();
107 if (bfs_->verbose_ && ancestors_->size() > depth) {
108 depth = ancestors_->size();
109 std::cout <<
"BFS depth: " << depth - 1 << std::endl;
114 if (bfs_->verbose_) {
115 std::cout <<
"Goal state reached: " << node << std::endl;
121 if (ancestors_->size() > bfs_->max_depth_)
continue;
124 if (bfs_->verbose_) {
125 for (
const NodeT& node : *ancestors_) {
126 std::cout << node << std::endl;
128 std::cout <<
"====================" << std::endl;
130 for (
const NodeT& child : node) {
132 if (bfs_->verbose_) {
133 std::cout << child << std::endl << std::endl;
136 queue_.emplace(child, ancestors_);
145 #endif // SYMBOLIC_PLANNING_BREADTH_FIRST_SEARCH_H_