faif
AStar.h
1 #ifndef FAIF_ASTAR_SEARCH_HPP
2 #define FAIF_ASTAR_SEARCH_HPP
3 
4 #include <vector>
5 #include <queue>
6 
7 #include <boost/bind.hpp>
8 #include <boost/concept_check.hpp>
9 
10 #include "TreeNodeImpl.hpp"
11 
12 namespace faif {
13  namespace search {
14 
15  /** \brief the comparizon used by AStar */
16  template<typename T>
18 
19  bool operator()(const TreeNode<T>* a, const TreeNode<T>* b) {
20  return (a->getWeight() + a->getPoint()->getHeuristic()) >
21  (b->getWeight() + b->getPoint()->getHeuristic());
22  }
23  };
24  /**
25  \brief A* (A star) search algorithm
26 
27  heuristic searching tree-like structures, finds the least-cost path
28  from initial node to goal node, uses a distance function which is a sum
29  of path-cost and a heuristic estimate of the distance to the goal
30 
31  Function is protected against the cycles in the graph.
32  The maximum depth of tree is limited.
33  */
34  template<typename T>
35  typename Node<T>::Path searchAStar(boost::shared_ptr<T> start, int max = 200) {
36 
37  //the methods to create tree-like structures are required
38  BOOST_CONCEPT_ASSERT((NodeWithChildrenConcept<T>));
39  BOOST_CONCEPT_ASSERT((NodeWithFinalFlagConcept<T>));
40  //the methods to return a weight of node is required
41  BOOST_CONCEPT_ASSERT((TreeNodeWeightConcept<T>));
42  //the methods to return a heuristic to the goal is required
43  BOOST_CONCEPT_ASSERT((TreeNodeHeuristicConcept<T>));
44 
45  std::priority_queue<TreeNode<T>*, std::deque<TreeNode<T>*>, compareWeightAndHeuristic<T> > buffer;
46 
47  TreeNode<T> root(start);
48  TreeNode<T>* curr = &root;
49 
50  buffer.push(curr);
51 
52  while (!buffer.empty()) {
53  curr = buffer.top();
54  buffer.pop();
55 
56  //check if the node is twice on the path from node to root
57  if ( checkNodeInPath( *curr ) )
58  continue;
59 
60  //check if the node is the final node
61  if (curr->getPoint()->isFinal() )
62  break;
63 
64  //check if the maximum depth
65  if (curr->getLevel() == max)
66  continue;
67 
68  typename TreeNode<T>::Children ch = curr->getChildrenWithWeight();
69  for( typename TreeNode<T>::Children::const_iterator it = ch.begin(); it != ch.end(); ++it )
70  buffer.push( *it );
71  }
72 
73  typename Node<T>::Path path;
74 
75  if (curr->getPoint()->isFinal() )
76  path = curr->generatePathToRoot();
77 
78  return path;
79 
80  }
81 
82  } //namespace search
83 }//namespace faif
84 
85 
86 #endif //FAIF_ASTAR_SEARCH_HPP
87 
Definition: Chain.h:17
the concept for heuristic search algorithms, it check the presence of &#39;getHeuristic&#39; method...
Definition: Node.hpp:85
the concept for node with children
Definition: Node.hpp:44
the comparizon used by AStar
Definition: AStar.h:17
boost::shared_ptr< T > getPoint() const
Definition: TreeNodeImpl.hpp:45
the struct to create node in search space from individual
Definition: Node.hpp:26
the template to create the node in tree-based search methods
Definition: TreeNodeImpl.hpp:17
the concept for node with final flag for search in tree-like structures The function &#39;searchDepthFirs...
Definition: Node.hpp:60
Node< T >::Path searchAStar(boost::shared_ptr< T > start, int max=200)
A* (A star) search algorithm.
Definition: AStar.h:35
Children getChildrenWithWeight()
Definition: TreeNodeImpl.hpp:111
Node< T >::Path generatePathToRoot() const
Definition: TreeNodeImpl.hpp:135
double getWeight() const
Definition: TreeNodeImpl.hpp:58
short getLevel() const
Definition: TreeNodeImpl.hpp:61
bool checkNodeInPath(const TreeNode< T > &n)
Definition: TreeNodeImpl.hpp:82
the concept for informed search algorithms, it check the presence of &#39;getWeight&#39; method, used by informed search functions e.g. &#39;searchUniformCost&#39;
Definition: Node.hpp:73