Symphony 1.0
Loading...
Searching...
No Matches
search.h
Go to the documentation of this file.
1#ifndef SEARCH_H
2#define SEARCH_H
3
4
5#include <map>
6#include "definitions.h"
7#include <memory>
8
9
10/* @brief Node class for search algorithms.
11 *
12 * This class represents a node in the search tree, which contains a state, a parent node, an action, the path cost to reach this node, and a heuristic value.
13 * The parent node is a shared pointer to prevent circular references and memory leaks.
14 * The state is a smart pointer to manage memory and prevent memory leaks.
15 * The action is a pointer to an immutable Action object.
16 */
17class Node {
18public:
19 /* @brief Smart pointer to manage parent memory
20 */
21 std::shared_ptr<Node> parent; // Weak pointer to prevent circular references
22 /* @brief Smart pointer to manage state memory.
23 */
24 std::shared_ptr<State> state; // Smart pointer to manage state memory
25 /* @brief Pointer to an immutable Action object.
26 */
27 const std::shared_ptr<Action> action; // Pointer to an immutable Action object
28 double path_cost; // Cost to reach this node
29 double heuristic; // Heuristic value (if applicable)
30
31 /* @brief Default constructor for the Node class.
32 */
33 Node() : action(nullptr), path_cost(0), heuristic(0) {}
34
35 // Parameterized constructor
36 Node(std::shared_ptr<Node> parent, std::shared_ptr<State> state, const std::shared_ptr<Action> action, double path_cost, double heuristic)
38};
39
40
41/* @brief Abstract class for search algorithms.
42 *
43 * This class defines the structure of a search algorithm, which is used to explore a problem space and find a solution. The search algorithm is responsible for traversing the graph of states and actions to find a path from the initial state to a goal state.
44 * All Symphony search algorithms should inherit from this class and implement the search method.
45 * All methods must use smart pointers to manage memory and prevent memory leaks, they are all responsible for freeing the memory they allocate.
46 */
47class Search {
48public:
50 virtual ~Search() {}
51 virtual std::shared_ptr<Node> search() = 0;
53};
54
55class Solution {
56public:
58 void print();
60};
61
67class BreadthFirstSearch : public Search {
68public:
74 std::shared_ptr<Node> search() override;
76};
77
78class AStarSearch : public Search {
79public:
81 std::shared_ptr<Node> search() override;
83};
84
85/* @brief Beam search algorithm implementation.
86 *
87 * This class implements the beam search algorithm, which is a heuristic search algorithm that explores a graph by expanding the most promising nodes in a limited set of nodes called the beam width.
88 */
89class BeamSearch : public Search {
90public:
92 /* @brief Beam search algorithm implementation.
93 *
94 * This class implements the beam search algorithm, which is a heuristic search algorithm that explores a graph by expanding the most promising nodes in the search tree.
95 * Beam search is one approach to limit the memory used by A* search. Beam search limits the size of the frontier, keeping only the k best f(n) nodes. A good selection of k will help make beam search fast and potentially near-optimal. Beam search is incomplete and sub-optimal. Another option for beam search is to keep all nodes with f(n') > d f(n) where f(n) is the current best.
96 */
97 std::shared_ptr<Node> search() override;
98 ~BeamSearch() override;
100};
101
108
116Search *create_search(SearchAlgorithmIndex search_algorithm_index, Problem *problem); // DEFINED IN search.cpp
117
118#endif //SEARCH_H
AStarSearch(Problem *problem)
Definition search.h:80
std::shared_ptr< Node > search() override
Definition search.cpp:82
int beam_width
Definition search.h:99
BeamSearch(Problem *problem, int beam_width)
Definition search.h:91
std::shared_ptr< Node > search() override
Definition search.cpp:134
~BeamSearch() override
Definition search.cpp:132
Breadth-first search algorithm implementation.
Definition search.h:67
BreadthFirstSearch(Problem *problem)
Definition search.h:69
std::shared_ptr< Node > search() override
Breadth-first search algorithm implementation. The breadth-first search algorithm explores a graph by...
Definition search.cpp:45
Definition search.h:17
std::shared_ptr< Node > parent
Definition search.h:21
double path_cost
Definition search.h:28
Node(std::shared_ptr< Node > parent, std::shared_ptr< State > state, const std::shared_ptr< Action > action, double path_cost, double heuristic)
Definition search.h:36
std::shared_ptr< State > state
Definition search.h:24
double heuristic
Definition search.h:29
const std::shared_ptr< Action > action
Definition search.h:27
Node()
Definition search.h:33
Represents an abstract problem that needs to be solved.
Definition definitions.h:63
Search(Problem *problem)
Definition search.h:49
Problem * problem
Definition search.h:52
virtual std::shared_ptr< Node > search()=0
virtual ~Search()
Definition search.h:50
Node * node
Definition search.h:59
void print()
Definition search.cpp:28
Solution(Node *node)
Definition search.h:57
Abstract classes for states, actions, and problems that need to be implemented by the user.
SearchAlgorithmIndex
Definition search.h:102
@ A_STAR
Definition search.h:105
@ UNIFORM_COST_SEARCH
Definition search.h:104
@ BEAM_SEARCH
Definition search.h:106
@ BREADTH_FIRST_SEARCH
Definition search.h:103
Search * create_search(SearchAlgorithmIndex search_algorithm_index, Problem *problem)
Factory method to create a search object based on the specified search algorithm.
Definition search.cpp:13