libmcts
A Monte Carlo Tree Search Library
inode.hpp
1 #ifndef INODE_H
2 #define INODE_H
3 
4 #include <vector>
5 #include <algorithm>
6 #include "ivisitor.hpp"
7 
8 namespace mcts {
9 using std::vector;
10 
11 // ----------------------------------------------------------------------
17 // ----------------------------------------------------------------------
18 template <typename Context, typename Config> class INode {
20 
21 public:
23 
24  virtual ~INode() {}
25 
26  // ----------------------------------------------------------------------
29  // ----------------------------------------------------------------------
30  virtual void expand() = 0;
31 
32  // ----------------------------------------------------------------------
36  // ----------------------------------------------------------------------
37  virtual node_t *parent() const = 0;
38 
39  // ----------------------------------------------------------------------
43  // ----------------------------------------------------------------------
44  virtual node_t *select_child() = 0;
45 
46  // ----------------------------------------------------------------------
51  // ----------------------------------------------------------------------
52  virtual node_t *select_recusively() = 0;
53 
54  // ----------------------------------------------------------------------
58  // ----------------------------------------------------------------------
59  virtual Config *config() const = 0;
60 
61  // ----------------------------------------------------------------------
66  // ----------------------------------------------------------------------
67  virtual int nb_samples() const = 0;
68 
69  // ----------------------------------------------------------------------
73  // ----------------------------------------------------------------------
74  virtual const vector<node_t *> children() const = 0;
75 
76  // ----------------------------------------------------------------------
80  // ----------------------------------------------------------------------
81  virtual void add_child(node_t *child) = 0;
82 
83  // ----------------------------------------------------------------------
87  // ----------------------------------------------------------------------
88  virtual const Context context() const = 0;
89 
90  // ----------------------------------------------------------------------
94  // ----------------------------------------------------------------------
95  virtual double ev() const = 0;
96 
97  // ----------------------------------------------------------------------
101  // ----------------------------------------------------------------------
102  virtual double std_dev() const = 0;
103 
104  // ----------------------------------------------------------------------
108  // ----------------------------------------------------------------------
109  virtual double variance() const = 0;
110 
111  // ----------------------------------------------------------------------
115  // ----------------------------------------------------------------------
116  virtual double simulate() const = 0;
117 
118  // ----------------------------------------------------------------------
122  // ----------------------------------------------------------------------
123  virtual void accept(visitor_t *visitor) = 0;
124 
125  // ----------------------------------------------------------------------
130  // ----------------------------------------------------------------------
131  virtual void backpropagate(const double &value) = 0;
132 
133  // ----------------------------------------------------------------------
138  // ----------------------------------------------------------------------
139  virtual unsigned count_recursive() const {
140  unsigned sum = 0;
141  vector<node_t *> children = this->children();
142  for (unsigned i = 0; i < children.size(); ++i) {
143  sum += children[i]->count_recursive();
144  }
145  return 1 + sum;
146  }
147 };
148 }
149 
150 #endif
virtual node_t * select_child()=0
selects a childnode according to a configurable metric
virtual void expand()=0
expand the current context and add all possible derived contexts as children.
virtual void backpropagate(const double &value)=0
applies a backpropagation strategy to traverse the simulated result back up in the tree.
basic interface for a node in the tree. Every node has to implement this functions.
Definition: inode.hpp:18
virtual void accept(visitor_t *visitor)=0
accepts a visitor according to the visitor pattern.
virtual double std_dev() const =0
gets the standard deviation.
virtual node_t * parent() const =0
gets the parent of the current node
virtual Config * config() const =0
gets the config object
virtual const Context context() const =0
gets the context associated with the current node
virtual double variance() const =0
gets the variance.
virtual int nb_samples() const =0
number of times a selection strategy selected the current node.
virtual double simulate() const =0
applies a simulation strategy to a terminal node.
implements interface for visitor objects.
Definition: ivisitor.hpp:15
virtual const vector< node_t * > children() const =0
get the children of the current node
virtual double ev() const =0
gets the current expected value.
virtual void add_child(node_t *child)=0
insert a new node as a child of the current one.
virtual node_t * select_recusively()=0
recursively selects nodes with a given simulation strategy until an terminal node is reached.
virtual unsigned count_recursive() const
recursively counts number of childen beneth this nodes. counts in the current node.
Definition: inode.hpp:139