1 #ifndef AI_GENERALIZED_ASTAR_H 2 #define AI_GENERALIZED_ASTAR_H 6 #include <KrisLibrary/utils/stl_tr1.h> 7 #include <KrisLibrary/structs/IndexedPriorityQueue.h> 33 template <
class S,
class C>
83 virtual bool IsGoal(
const S& s) =0;
84 virtual void Successors(
const S& s,std::vector<S>&
successors,std::vector<C>& costs) =0;
85 virtual C Heuristic(
const S& s) {
return zero; }
89 virtual void Visit(
const S& s,
Node* n) {}
90 virtual Node* VisitedStateNode(
const S& s) {
return NULL; }
113 std::vector<C> costs;
144 template <
class S,
class C>
149 std::map<S,Node*> visited;
152 virtual void Visit(
const S& s,
Node* n) { visited[s]=n;}
153 virtual Node* VisitedStateNode(
const S& s) {
154 typename std::map<S,Node*>::const_iterator i=visited.find(s);
155 if(i==visited.end())
return NULL;
162 template <
class S,
class C>
167 UNORDERED_MAP_TEMPLATE<S,Node*> visited;
170 virtual void Visit(
const S& s,
Node* n) { visited[s]=n;}
171 virtual Node* VisitedStateNode(
const S& s) {
172 typename UNORDERED_MAP_TEMPLATE<S,Node*>::const_iterator i=visited.find(s);
173 if(i==visited.end())
return NULL;
179 template <
class S,
class C>
185 template <
class S,
class C>
192 template <
class S,
class C>
203 path.push_back(start);
210 root.
f=Heuristic(start);
220 template <
class S,
class C>
230 template <
class S,
class C>
233 if(
fringe.empty())
return false;
250 std::reverse(
path.begin(),
path.end());
277 std::reverse(
path.begin(),
path.end());
283 if(n->
g + costs[i] < visited->
g) {
288 for(
size_t c=0;c<p->
children.size();c++)
295 visited->
g = n->
g + costs[i];
298 fringe.refresh(visited,std::pair<C,C>(visited->
f,-visited->
g));
308 child->
g = n->
g + costs[i];
312 fringe.insert(child,std::pair<C,C>(child->
f,-child->
g));
319 template <
class S,
class C>
325 template <
class S,
class C>
332 template <
class S,
class C>
338 template <
class S,
class C>
342 for(
size_t i=0;i<n.
children.size();i++)
347 template <
class S,
class C>
351 return fringe.top().first.first;
virtual bool OnExpand(Node *n)
Definition: GeneralizedAStar.h:94
Node root
The A* search tree.
Definition: GeneralizedAStar.h:105
std::vector< S > path
Upon successful termination, path contains the path from start to goal.
Definition: GeneralizedAStar.h:119
Contains a priority queue with an associated index, allowing updates of the priority value...
Definition: IndexedPriorityQueue.h:36
Node * goal
Upon successful termination, goal contains the goal node.
Definition: GeneralizedAStar.h:117
virtual void ClearVisited()
Definition: GeneralizedAStar.h:169
std::vector< S > successors
Temporary variables – slightly reduces the number of memory allocations.
Definition: GeneralizedAStar.h:112
Definition: GeneralizedAStar.h:163
Definition: GeneralizedAStar.h:9
std::vector< Node * > children
list of pointers to children
Definition: GeneralizedAStar.h:51
C zero
Definition: GeneralizedAStar.h:102
bool testGoalOnGeneration
Definition: GeneralizedAStar.h:98
S data
state data
Definition: GeneralizedAStar.h:47
void SetStart(const S &start)
Resets the search from the given start state.
Definition: GeneralizedAStar.h:193
Definition: GeneralizedAStar.h:37
C TopPriority() const
Returns the priority of the next node to be expanded.
Definition: GeneralizedAStar.h:348
int NumDescendents(const Node &n) const
Returns the number of descendents of n.
Definition: GeneralizedAStar.h:339
Definition: GeneralizedAStar.h:145
C g
cost from start
Definition: GeneralizedAStar.h:43
virtual void ClearVisited()
Definition: GeneralizedAStar.h:151
A tree graph structure, represented directly at the node level.
Definition: Tree.h:25
bool GoalFound() const
Returns true if the goal has been found.
Definition: GeneralizedAStar.h:75
int NumNodes() const
Returns the number of nodes in the tree.
Definition: GeneralizedAStar.h:326
int NumExpanded() const
Returns the number of previously expanded nodes.
Definition: GeneralizedAStar.h:333
Definition: GeneralizedAStar.h:34
Node * parent
pointer to parent
Definition: GeneralizedAStar.h:49
virtual void ClearVisited()
Definition: GeneralizedAStar.h:88
Definition: GeneralizedAStar.h:125
bool SearchStep()
Performs a single iteration of search.
Definition: GeneralizedAStar.h:231
C GoalCost() const
Definition: GeneralizedAStar.h:78
virtual bool IsGoal(const S &s)=0
The following must be overloaded by the subclass.
IndexedPriorityQueue< Node *, std::pair< C, C > > fringe
Definition: GeneralizedAStar.h:110
bool SearchFailed()
Returns true if search failed.
Definition: GeneralizedAStar.h:320
bool Search()
Performs search until a goal is reached.
Definition: GeneralizedAStar.h:221
Definition: GeneralizedAStar.h:135
const std::vector< S > & GoalPath() const
Returns path of states to the goal.
Definition: GeneralizedAStar.h:80
C f
cost from start + heuristic
Definition: GeneralizedAStar.h:45