libmove3d-planners
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Friends Macros Groups Pages
AStar.hpp
1 /*
2  * A_Star.h
3  *
4  * This header file contains declerations of four classes
5  *
6  * 1) Tree_Element
7  * 2) Queue_Element
8  * 3) Prioritize_Queue_Elements
9  * 4) A_Star
10  *
11  * */
12 
13 #ifndef A_STAR_H
14 #define A_STAR_H
15 
16 #include "API/Search/AStar/State.hpp"
17 #include <queue>
18 #include <vector>
19 
20 namespace API
21 {
27  class TreeNode
28  {
29  public:
30  TreeNode() : _Parent(NULL) {}
32  ~TreeNode() {}
33 
34  TreeNode* getParent() const { return _Parent;}
35  API::State* getState() const { return _State;}
36 
37  private:
38  TreeNode *_Parent;
39  API::State *_State;
40  };
41 
42 
49  {
50  public:
51  QueueElement() : _Node(NULL) {}
52  QueueElement(TreeNode* te) : _Node(te) {}
53  ~QueueElement() {}
54 
55  TreeNode* getTreeNode() { return _Node; }
56 
57  friend class PrioritizeQueueElements;
58 
59  private:
60  TreeNode* _Node;
61  };
62 
69  {
70  public:
71  int operator()(QueueElement &x, QueueElement &y)
72  {
73  return x.getTreeNode()->getState()->f() > y.getTreeNode()->getState()->f();
74  }
75  };
76 
77 
84  class AStar
85  {
86  public:
87  AStar() :
88  _Goal(NULL),
89  _GoalIsDefined(false)
90  {}
91 
92  AStar(API::State* goal) :
93  _Goal(goal),
94  _GoalIsDefined(true)
95  {}
96 
97  ~AStar() {}
98 
99  std::vector<API::State*> solve(API::State* initial_state);
100 
101  private:
102  API::State* _Goal;
103  void setGoal(API::State* goal) { _Goal = goal; }
104 
105  bool _GoalIsDefined;
106  bool isGoal(API::State* state);
107 
108  std::vector<API::State*> getSolution(QueueElement qEl);
109 
110  void cleanStates();
111 
112  TreeNode *_Root; /* root of the A-star tree */
113  TreeNode *_SolutionLeaf; /* keeps the solution leaf after solve is called */
114 
115  std::priority_queue <QueueElement, std::vector<QueueElement>, PrioritizeQueueElements> _OpenSet;
116 
117  std::vector<API::State*> _Solution; /* This array is allocated when solve is called */
118  std::vector<API::State*> _Explored;
119 
120  enum {NOT_FOUND,FOUND} _AStarState; /* keeps if a solution exists after solve is called */
121  };
122 }
123 #endif
124 
Basic block to be used in the priority queue.
Definition: AStar.hpp:48
std::vector< API::State * > solve(API::State *initial_state)
A Star solving function (main)
Definition: AStar.cpp:76
Function used for sorting tree nodes in the priority queue.
Definition: AStar.hpp:68
This class keeps a pointer to the A-star search tree, an instant of priority_queue of "Queue_Element"...
Definition: AStar.hpp:84
++
Definition: State.hpp:20
This class is the node class to implement the search tree.
Definition: AStar.hpp:27