6#include <unordered_set>
14 switch (search_algorithm_index) {
30 std::vector<Action> actions;
31 auto parent = current->
parent;
33 while (parent !=
nullptr) {
34 actions.push_back(*current->
action);
35 current = parent.get();
39 for (
auto it = actions.rbegin(); it != actions.rend(); ++it) {
40 std::cout << it->name << std::endl;
46 std::queue<std::shared_ptr<Node>> frontier;
48 auto root = std::make_shared<Node>(
50 std::shared_ptr<State>(initial_state),
56 while (!frontier.empty()) {
57 auto node = frontier.front();
60 State *state = node->state.get();
64 #pragma omp parallel for shared(frontier) schedule(dynamic) if (frontier.size() > 1000)
66 auto child = std::make_shared<Node>(
67 std::shared_ptr<Node>(node),
70 node->path_cost + action->cost,
83 std::priority_queue<std::shared_ptr<Node>, std::vector<std::shared_ptr<Node>>,
NodeComparator> frontier;
85 std::unordered_set<std::shared_ptr<State>> explored;
87 auto root = std::make_shared<Node>(
89 std::shared_ptr<State>(initial_state),
96 while (!frontier.empty()) {
97 auto node = frontier.top();
101 State *state = node->state.get();
107 if (explored.find(node->state) != explored.end()) {
110 explored.insert(node->state);
114 auto child = std::make_shared<Node>(
115 std::shared_ptr<Node>(node),
118 node->path_cost + action->cost,
121 frontier.push(child);
136 using Beam = std::vector<std::shared_ptr<Node>>;
141 auto root = std::make_shared<Node>(
143 std::shared_ptr<State>(initial_state),
149 beam.push_back(root);
151 while (!beam.empty()) {
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);
165 for (
const auto& node : beam) {
172 auto child = std::make_shared<Node>(
176 node->path_cost + action->cost,
179 next_beam.push_back(child);
183 beam = std::move(next_beam);
std::shared_ptr< Node > search() override
std::shared_ptr< Node > search() override
Breadth-first search algorithm implementation.
std::shared_ptr< Node > search() override
Breadth-first search algorithm implementation. The breadth-first search algorithm explores a graph by...
std::shared_ptr< Node > parent
const std::shared_ptr< Action > action
Represents an abstract problem that needs to be solved.
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 std::vector< std::shared_ptr< Action > > actions(std::shared_ptr< State > state)=0
Retrieves the set of actions applicable to a given state.
virtual State * initial_state()
Retrieves the initial state of the problem.
Represents an abstract state in a problem.
Search * create_search(SearchAlgorithmIndex search_algorithm_index, Problem *problem)
Factory method to create a search object based on the specified search algorithm.