faif
TreeNodeImpl.hpp
1 #ifndef FAIF_SEARCHING_TREE_NODE_HPP
2 #define FAIF_SEARCHING_TREE_NODE_HPP
3 
4 #include <algorithm>
5 #include "Node.hpp"
6 
7 namespace faif {
8 
9  namespace search {
10 
11  /**
12  \brief the template to create the node in tree-based search methods
13 
14  \note The class should not be used by library user, it is used internally by search algorithms
15  */
16  template<typename T>
17  class TreeNode {
18  public:
19  typedef std::vector< TreeNode<T>* > Children;
20 
21  /** Constructor */
22  TreeNode(const typename Node<T>::PNode& point, TreeNode<T>* parent = 0L)
23  : point_(point), parent_(parent), weight_(0.0), eval_(false), level_(0)
24  {
25  if (parent_) level_ = parent_->level_ + 1;
26  //cout << "TreeNode c-tor level " << level_ << "point: " << *point_.get() << endl;
27  }
28 
29  /** Constructor */
30  TreeNode(const typename Node<T>::PNode& point, TreeNode<T>* parent, double weight)
31  : point_(point), parent_(parent), weight_(weight), eval_(false), level_(0)
32  {
33  if (parent_) level_ = parent_->level_ + 1;
34  //cout << "TreeNode c-tor level " << level_ << "point: " << *point_.get() << endl;
35  }
36 
37 
38  /** Destructor */
40  eraseChildren();
41  //cout << "d-tor" << endl;
42  }
43 
44  /** accessor */
45  boost::shared_ptr<T> getPoint() const {return point_; }
46 
47  /** accessor */
48  const TreeNode<T>* getParent() const {return parent_; }
49 
50  /** accessor, the children. Creates the children in the first call */
51  Children getChildren();
52 
53  /** accessor, the children. Creates the children in the first call.
54  Calculates the weight as a sum of weight of parent and weight of child. */
55  Children getChildrenWithWeight();
56 
57  /** accessor */
58  double getWeight() const { return weight_; }
59 
60  /** accessor */
61  short getLevel() const { return level_; }
62 
63  /** erase the child tree nodes */
64  void eraseChildren();
65 
66  /** generate the path from given state to the root */
67  typename Node<T>::Path generatePathToRoot() const;
68  private:
69  typename Node<T>::PNode point_; //!< the node member
70  TreeNode<T>* parent_; //!< pointer to the parent
71  Children children_;
72 
73  double weight_; //!< the weight of the node (used by informed search algorithms)
74 
75  bool eval_; //!< the flag if the children were generated
76  short level_; //!< the node level
77  };
78 
79 
80  /** check if the node is twice on the path */
81  template<typename T>
82  bool checkNodeInPath( const TreeNode<T>& n) {
83 
84  const T& state = *(n.getPoint());
85 
86  for( const TreeNode<T>* p = n.getParent(); p != 0L; p = p->getParent() ) {
87  if( *(p->getPoint()) == state )
88  return true;
89  }
90  return false;
91  }
92 
93  /** accessor, the children. Creates the children in the first call */
94  template<typename T>
95  typename TreeNode<T>::Children TreeNode<T>::getChildren() {
96  if (!eval_) {
97  std::vector< boost::shared_ptr<T> > ch = point_->getChildren();
98 
99  for (unsigned i = 0; i < ch.size(); ++i) {
100  TreeNode<T>* p = new TreeNode(ch[i], this );
101  children_.push_back(p);
102  }
103  eval_ = true;
104  }
105  return children_;
106  }
107 
108  /** accessor, the children. Creates the children in the first call.
109  Calculates the weight as a sum of weight of parent and weight of child. */
110  template<typename T>
111  typename TreeNode<T>::Children TreeNode<T>::getChildrenWithWeight() {
112  if (!eval_) {
113  std::vector< boost::shared_ptr<T> > ch = point_->getChildren();
114 
115  for (unsigned i = 0; i < ch.size(); ++i) {
116  TreeNode<T>* p = new TreeNode(ch[i], this, ch[i]->getWeight() + weight_ );
117  children_.push_back(p);
118  }
119  eval_ = true;
120  }
121  return children_;
122  }
123 
124  /** erase the child tree nodes */
125  template<typename T>
127  for(typename Children::iterator i = children_.begin(); i != children_.end(); ++i )
128  delete *i;
129  children_.clear();
130  eval_ = false;
131  }
132 
133  /** generate the path from given state to the root */
134  template<typename T>
136 
137  typename Node<T>::Path path;
138 
139  for(const TreeNode<T>* s = this; s != 0L; ) {
140  path.push_back( s->getPoint() );
141  s = s->getParent();
142  }
143  return path;
144  }
145 
146  } //namespace search
147 } //namespace faif
148 
149 #endif //FAIF_SEARCHING_TREE_NODE_HPP
TreeNode(const typename Node< T >::PNode &point, TreeNode< T > *parent=0L)
Definition: TreeNodeImpl.hpp:22
Definition: Chain.h:17
void eraseChildren()
Definition: TreeNodeImpl.hpp:126
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
TreeNode(const typename Node< T >::PNode &point, TreeNode< T > *parent, double weight)
Definition: TreeNodeImpl.hpp:30
~TreeNode()
Definition: TreeNodeImpl.hpp:39
Children getChildrenWithWeight()
Definition: TreeNodeImpl.hpp:111
Children getChildren()
Definition: TreeNodeImpl.hpp:95
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
const TreeNode< T > * getParent() const
Definition: TreeNodeImpl.hpp:48