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

#include <search.h>

Inheritance diagram for BeamSearch:
Collaboration diagram for BeamSearch:

Public Member Functions

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

Public Attributes

int beam_width
 
- Public Attributes inherited from Search
Problemproblem
 

Detailed Description

Definition at line 89 of file search.h.

Constructor & Destructor Documentation

◆ BeamSearch()

BeamSearch::BeamSearch ( Problem problem,
int  beam_width 
)
inline

Definition at line 91 of file search.h.

int beam_width
Definition search.h:99
Problem * problem
Definition search.h:52

◆ ~BeamSearch()

BeamSearch::~BeamSearch ( )
override

Definition at line 132 of file search.cpp.

132{ }

Member Function Documentation

◆ search()

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

Implements Search.

Definition at line 134 of file search.cpp.

134 {
135 // Priority queue to maintain the beam
136 using Beam = std::vector<std::shared_ptr<Node>>;
137 Beam beam;
138
139 // Initialize the root node
140 auto initial_state = problem->initial_state();
141 auto root = std::make_shared<Node>(
142 nullptr,
143 std::shared_ptr<State>(initial_state),
144 nullptr,
145 0,
146 problem->heuristic(initial_state)
147 );
148
149 beam.push_back(root);
150
151 while (!beam.empty()) {
152 // Sort beam nodes based on their f(n) = path_cost + heuristic
153 std::sort(beam.begin(), beam.end(), [](const std::shared_ptr<Node>& a, const std::shared_ptr<Node>& b) {
154 return (a->path_cost + a->heuristic) < (b->path_cost + b->heuristic);
155 });
156
157 // Keep only the top k nodes (beam width)
158 if (beam.size() > beam_width) {
159 beam.resize(beam_width);
160 }
161
162 Beam next_beam;
163
164 // Expand each node in the current beam
165 for (const auto& node : beam) {
166 if (problem->goal_test(node->state.get())) {
167 return node; // Goal found
168 }
169
170 // Generate successors
171 for (const auto& action : problem->actions(node->state)) {
172 auto child = std::make_shared<Node>(
173 node,
174 action->effect,
175 action,
176 node->path_cost + action->cost,
177 problem->heuristic(action->effect.get())
178 );
179 next_beam.push_back(child);
180 }
181 }
182
183 beam = std::move(next_beam);
184 }
185
186 return nullptr;
187}
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

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

Member Data Documentation

◆ beam_width

int BeamSearch::beam_width

Definition at line 99 of file search.h.

Referenced by search().


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