libmcts
A Monte Carlo Tree Search Library
mcts.hpp
1 #ifndef MCTS_H
2 #define MCTS_H
3 
4 #include "root_node.hpp"
5 
6 namespace mcts {
7 
8 // ----------------------------------------------------------------------
16 // ----------------------------------------------------------------------
17 template <typename Context, typename Config, typename WrapNode,
18  typename ResultType>
19 class MCTS {
20  typedef typename INode<Context, Config>::node_t node_t;
22 
23 public:
24  // ----------------------------------------------------------------------
30  // ----------------------------------------------------------------------
31  MCTS(const Context &context, Config *config)
32  : tree(new root_node_t(context, config)), iteration_cnt(0) {}
33 
34  virtual ~MCTS() { delete tree; }
35 
36  // ----------------------------------------------------------------------
44  // ----------------------------------------------------------------------
45  void iterate() {
46  node_t *selected_leaf = tree->select_recusively();
47  double value = selected_leaf->simulate();
48  selected_leaf->backpropagate(value);
49  ++iteration_cnt;
50  }
51 
52  // ----------------------------------------------------------------------
59  // ----------------------------------------------------------------------
60  ResultType result() {
61  return ResultType(tree->config()->move_selector()->select(tree));
62  }
63 
64  // ----------------------------------------------------------------------
68  // ----------------------------------------------------------------------
69  const node_t *root() const { return tree; }
70 
71  // ----------------------------------------------------------------------
75  // ----------------------------------------------------------------------
76  unsigned nb_nodes() { return tree->count_recursive(); }
77 
78  // ----------------------------------------------------------------------
83  // ----------------------------------------------------------------------
84  unsigned nb_iterations() { return iteration_cnt; }
85 
86 private:
88  node_t *tree;
89  unsigned iteration_cnt;
90 };
91 }
92 
93 #endif
special node that is the root of each tree. Its merely a wrapper that wrapps around a inner node.
Definition: root_node.hpp:18
unsigned nb_iterations()
gets the number of performed iterations. the counter is increased each time the function iterate() is...
Definition: mcts.hpp:84
mainclass to interact with the library. it holds the root of the searchtree.
Definition: mcts.hpp:19
MCTS(const Context &context, Config *config)
creates a new empty mcts object. only a root node exists.
Definition: mcts.hpp:31
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
ResultType result()
gets the current result node. The node is selected according to a metric defined in a selection strat...
Definition: mcts.hpp:60
void iterate()
executes one interation step. Each iteration consists of the following actions:
Definition: mcts.hpp:45
const node_t * root() const
gets the root of the node.
Definition: mcts.hpp:69
virtual Config * config() const =0
gets the config object
virtual double simulate() const =0
applies a simulation strategy to a terminal node.
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
unsigned nb_nodes()
gets the number of nodes the tree is currently holding.
Definition: mcts.hpp:76