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

#include <search.h>

Inheritance diagram for AStarSearch:
Collaboration diagram for AStarSearch:

Public Member Functions

 AStarSearch (Problem *problem)
 
std::shared_ptr< Nodesearch () override
 
 ~AStarSearch ()
 
- Public Member Functions inherited from Search
 Search (Problem *problem)
 
virtual ~Search ()
 

Additional Inherited Members

- Public Attributes inherited from Search
Problemproblem
 

Detailed Description

Definition at line 78 of file search.h.

Constructor & Destructor Documentation

◆ AStarSearch()

AStarSearch::AStarSearch ( Problem problem)
inline

Definition at line 80 of file search.h.

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

◆ ~AStarSearch()

AStarSearch::~AStarSearch ( )

Definition at line 80 of file search.cpp.

80{ }

Member Function Documentation

◆ search()

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

Implements Search.

Definition at line 82 of file search.cpp.

82 {
83 std::priority_queue<std::shared_ptr<Node>, std::vector<std::shared_ptr<Node>>, NodeComparator> frontier;
84 // Initialize the root node with the problem's initial state
85 std::unordered_set<std::shared_ptr<State>> explored; // Set of explored states
86 auto initial_state = problem->initial_state();
87 auto root = std::make_shared<Node>(
88 nullptr, // Parent node
89 std::shared_ptr<State>(initial_state), // Initial state (shared_ptr)
90 nullptr, // No action
91 0, // Path cost
92 problem->heuristic(initial_state) // Heuristic value
93 );
94 frontier.push(root);
95
96 while (!frontier.empty()) {
97 auto node = frontier.top();
98 frontier.pop();
99
100 // Check if the goal state is reached
101 State *state = node->state.get();
102 if (problem->goal_test(state)) {
103 return node;
104 }
105
106 // Skip the node if it has already been explored
107 if (explored.find(node->state) != explored.end()) {
108 continue;
109 }
110 explored.insert(node->state);
111
112 // Expand the node by generating its child nodes
113 for (const auto &action : problem->actions(node->state)) {
114 auto child = std::make_shared<Node>(
115 std::shared_ptr<Node>(node), // Parent node
116 action->effect,
117 action, // Pointer to the action
118 node->path_cost + action->cost, // Path cost
119 problem->heuristic(action->effect.get()) // Heuristic value
120 );
121 frontier.push(child);
122 }
123 }
124
125 // Return nullptr if no solution is found
126 return nullptr;
127}
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: