faif
UnifiedCost.h
1 #ifndef FAIF_UNIFIED_COST_SEARCH_HPP
2 #define FAIF_UNIFIED_COST_SEARCH_HPP
3 
4 #include <vector>
5 #include <deque>
6 #include <functional>
7 
8 #include <boost/bind.hpp>
9 #include <boost/function.hpp>
10 #include <boost/concept_check.hpp>
11 
12 #include "TreeNodeImpl.hpp"
13 
14 namespace faif {
15  namespace search {
16 
17  /** \brief the comparizon used by searchUnifiedCost */
18  template<typename T>
19  struct compareWeight {
20  bool operator()(const TreeNode<T>* a, const TreeNode<T>* b) {
21  return a->getWeight() > b->getWeight();
22  }
23  };
24 
25  /**
26  \brief uniform-cost search algorithm (informed search)
27 
28  the uniform-cost search algorithm, traversing a tree structure,
29  visit the next node which has the least total cost from the root.
30  Algorithm use the 'getWeight' function of node.
31 
32  Function is protected against the cycles in the graph.
33  The maximum depth of tree is limited.
34  */
35  template<typename T>
36  typename Node<T>::Path searchUnifiedCost(boost::shared_ptr<T> start, int max = 200) {
37 
38  //the methods to create tree-like structures are required
39  BOOST_CONCEPT_ASSERT((NodeWithChildrenConcept<T>));
40  BOOST_CONCEPT_ASSERT((NodeWithFinalFlagConcept<T>));
41  //the methods to return a weight of node is required
42  BOOST_CONCEPT_ASSERT((TreeNodeWeightConcept<T>));
43 
44  std::priority_queue<TreeNode<T>*, std::deque<TreeNode<T>*>, compareWeight<T> > buffer;
45 
46  TreeNode<T> root(start);
47  TreeNode<T>* curr = &root;
48 
49  buffer.push(curr);
50 
51  while (!buffer.empty()) {
52  curr = buffer.top();
53  buffer.pop();
54 
55  //check if the node is twice on the path from node to root
56  if ( checkNodeInPath( *curr ) )
57  continue;
58 
59  //check if the node is the final node
60  if (curr->getPoint()->isFinal() )
61  break;
62 
63  //check if the maximum depth
64  if (curr->getLevel() == max)
65  continue;
66 
67  typename TreeNode<T>::Children ch = curr->getChildrenWithWeight();
68  for( typename TreeNode<T>::Children::const_iterator it = ch.begin(); it != ch.end(); ++it )
69  buffer.push( *it );
70  }
71 
72  typename Node<T>::Path path;
73 
74  if (curr->getPoint()->isFinal() )
75  path = curr->generatePathToRoot();
76 
77  return path;
78  }
79 
80  } //namespace search
81 }//namespace faif
82 
83 #endif //FAIF_UNIFIED_COST_SEARCH_HPP
the comparizon used by searchUnifiedCost
Definition: UnifiedCost.h:19
Definition: Chain.h:17
the concept for node with children
Definition: Node.hpp:44
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
Children getChildrenWithWeight()
Definition: TreeNodeImpl.hpp:111
Node< T >::Path generatePathToRoot() const
Definition: TreeNodeImpl.hpp:135
double getWeight() const
Definition: TreeNodeImpl.hpp:58
Node< T >::Path searchUnifiedCost(boost::shared_ptr< T > start, int max=200)
uniform-cost search algorithm (informed search)
Definition: UnifiedCost.h:36
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