libmcts
A Monte Carlo Tree Search Library
inner_node.hpp
1 #ifndef INNER_NODE_H
2 #define INNER_NODE_H
3 
4 #include <stdexcept>
5 #include "inode.hpp"
6 #include "ibackpropagation_strategy.hpp"
7 
8 namespace mcts {
9 
10 // ----------------------------------------------------------------------
17 // ----------------------------------------------------------------------
18 template <typename Context, typename Config>
19 class InnerNode : public INode<Context, Config> {
20  typedef typename INode<Context, Config>::node_t node_t;
22 
24  Context context_;
25 
27  Config *config_;
28  node_t *parent_;
29  vector<node_t *> children_;
30 
31 public:
34 
35  InnerNode(const Context &context, Config *config, node_t *parent,
36  IBackpropagationStrategy *backprop_strat)
37  : context_(context), config_(config), parent_(parent),
38  backprop_strat_(backprop_strat) {}
39 
40  virtual void expand() = 0;
41 
42  virtual node_t *select_child() = 0;
43 
44  virtual void accept(visitor_t *visitor) { visitor->visit(this); }
45 
46  virtual const vector<node_t *> children() const { return children_; }
47 
48  virtual void add_child(node_t *child) { children_.push_back(child); }
49 
50  virtual Config *config() const { return config_; }
51 
52  virtual const Context context() const { return context_; }
53 
54  virtual node_t *parent() const { return parent_; }
55 
57  if (children_.size() == 0)
58  expand();
59  node_t *selec = select_child();
60  node_t *selectedChild = selec->select_recusively();
61  return selectedChild;
62  }
63 
64  virtual double simulate() const {
65  throw std::runtime_error("not supported.");
66  }
67 
68  virtual double ev() const { return backprop_strat_->ev(); }
69 
70  virtual double std_dev() const { return backprop_strat_->std_deviation(); }
71 
72  virtual double variance() const { return backprop_strat_->variance(); }
73 
74  virtual int nb_samples() const { return backprop_strat_->nb_samples(); }
75 
76  virtual void backpropagate(const double &value) {
78  parent_->backpropagate(value);
79  }
80 
81  virtual ~InnerNode() {
82  for (unsigned i = 0; i < children_.size(); ++i) {
83  delete children_[i];
84  }
85  }
86 };
87 }
88 
89 #endif
virtual double ev() const =0
gets the current expected value
virtual void backpropagate(const double &value)
applies a backpropagation strategy to traverse the simulated result back up in the tree.
Definition: inner_node.hpp:76
virtual double ev() const
gets the current expected value.
Definition: inner_node.hpp:68
virtual node_t * parent() const
gets the parent of the current node
Definition: inner_node.hpp:54
virtual void expand()=0
expand the current context and add all possible derived contexts as children.
virtual double variance() const
gets the variance.
Definition: inner_node.hpp:72
virtual double simulate() const
applies a simulation strategy to a terminal node.
Definition: inner_node.hpp:64
virtual void backpropagate(const double &value)=0
applies a backpropagation strategy to traverse the simulated result back up in the tree.
virtual node_t * select_child()=0
selects a childnode according to a configurable metric
virtual double std_deviation() const =0
gets the current standard deviation
virtual int nb_samples() const
number of times a selection strategy selected the current node.
Definition: inner_node.hpp:74
basic interface for a node in the tree. Every node has to implement this functions.
Definition: inode.hpp:18
virtual void on_backpropagate(const double &value)=0
saves an value according to some metric.
virtual double std_dev() const
gets the standard deviation.
Definition: inner_node.hpp:70
A backpropagation strategy is used in each node. when a terminal node is simulated,...
virtual const Context context() const
gets the context associated with the current node
Definition: inner_node.hpp:52
virtual double variance() const =0
gets the current variance
IBackpropagationStrategy * backprop_strat_
strategy that is used for propagating values back in the tree.
Definition: inner_node.hpp:33
virtual int nb_samples() const =0
gets the number of samples that are saved in the strat.
implements interface for visitor objects.
Definition: ivisitor.hpp:15
virtual void accept(visitor_t *visitor)
accepts a visitor according to the visitor pattern.
Definition: inner_node.hpp:44
virtual const vector< node_t * > children() const
get the children of the current node
Definition: inner_node.hpp:46
virtual void add_child(node_t *child)
insert a new node as a child of the current one.
Definition: inner_node.hpp:48
implements most methods of the inode interface. some methods have to be implemented problem dependend...
Definition: inner_node.hpp:19
virtual node_t * select_recusively()=0
recursively selects nodes with a given simulation strategy until an terminal node is reached.
virtual node_t * select_recusively()
recursively selects nodes with a given simulation strategy until an terminal node is reached.
Definition: inner_node.hpp:56
virtual Config * config() const
gets the config object
Definition: inner_node.hpp:50