Symphony 1.0
Loading...
Searching...
No Matches
BreadthFirstSearch Class Reference

Breadth-first search algorithm implementation. More...

#include <search.h>

Inheritance diagram for BreadthFirstSearch:
Collaboration diagram for BreadthFirstSearch:

Public Member Functions

 BreadthFirstSearch (Problem *problem)
 
std::shared_ptr< Nodesearch () override
 Breadth-first search algorithm implementation. The breadth-first search algorithm explores a graph by visiting all the neighbor nodes at the present depth prior to moving on to the nodes at the next depth level.
 
 ~BreadthFirstSearch ()
 
- Public Member Functions inherited from Search
 Search (Problem *problem)
 
virtual ~Search ()
 

Additional Inherited Members

- Public Attributes inherited from Search
Problemproblem
 

Detailed Description

Breadth-first search algorithm implementation.

This class implements the breadth-first search algorithm, which explores a graph by visiting all the neighbor nodes at the present depth prior to moving on to the nodes at the next depth level.

Definition at line 67 of file search.h.

Constructor & Destructor Documentation

◆ BreadthFirstSearch()

BreadthFirstSearch::BreadthFirstSearch ( Problem problem)
inline

Definition at line 69 of file search.h.

69: Search(problem) {}
Problem * problem
Definition search.h:52

◆ ~BreadthFirstSearch()

BreadthFirstSearch::~BreadthFirstSearch ( )

Definition at line 26 of file search.cpp.

26{ }

Member Function Documentation

◆ search()

std::shared_ptr< Node > BreadthFirstSearch::search ( )
overridevirtual

Breadth-first search algorithm implementation. The breadth-first search algorithm explores a graph by visiting all the neighbor nodes at the present depth prior to moving on to the nodes at the next depth level.

Implements Search.

Definition at line 45 of file search.cpp.

45 {
46 std::queue<std::shared_ptr<Node>> frontier;
47 auto initial_state = problem->initial_state();
48 auto root = std::make_shared<Node>(
49 nullptr,
50 std::shared_ptr<State>(initial_state),
51 nullptr,
52 0,
53 problem->heuristic(initial_state)
54 );
55 frontier.push(root);
56 while (!frontier.empty()) {
57 auto node = frontier.front();
58 frontier.pop();
59
60 State *state = node->state.get();
61 if (problem->goal_test(state)) {
62 return node;
63 }
64 #pragma omp parallel for shared(frontier) schedule(dynamic) if (frontier.size() > 1000)
65 for (const auto &action : problem->actions(node->state)) {
66 auto child = std::make_shared<Node>(
67 std::shared_ptr<Node>(node),
68 action->effect,
69 action,
70 node->path_cost + action->cost,
71 problem->heuristic(action->effect.get())
72 );
73 frontier.push(child);
74 }
75 }
76 // Return nullptr if no solution is found
77 return nullptr;
78}
virtual bool goal_test(State *state)=0
Tests if a given state satisfies the goal condition.
virtual double heuristic(State *state)=0
Computes a heuristic estimate for a given state.
virtual State * initial_state()
Retrieves the initial state of the problem.
Definition definitions.h:80
Represents an abstract state in a problem.
Definition definitions.h:18

References Problem::actions(), Problem::goal_test(), Problem::heuristic(), Problem::initial_state(), and Search::problem.


The documentation for this class was generated from the following files: