libmcts
A Monte Carlo Tree Search Library
max_function_selector.hpp
1 #ifndef MAXFUNCTIONSELECTOR_H
2 #define MAXFUNCTIONSELECTOR_H
3 
4 #include <vector>
5 #include <stdexcept>
6 #include <algorithm>
7 #include "iselection_strategy.hpp"
8 
9 namespace mcts {
10 using std::vector;
11 
12 // ----------------------------------------------------------------------
18 // ----------------------------------------------------------------------
19 template <typename Context, typename Config>
20 class MaxFunctionSelector : public ISelectionStrategy<Context, Config> {
21  typedef typename INode<Context, Config>::node_t node_t;
22 
23 public:
24  node_t *select(node_t *node) {
25  node_t *max_node = NULL;
26  vector<node_t *> children = node->children();
27  vector<double> values(children.size());
28 
29  for (unsigned i = 0; i < children.size(); ++i){
30  values[i] = evaluate(children[i]);
31  }
32 
33  size_t max_index =
34  std::max_element(values.begin(), values.end()) - values.begin();
35  max_node = children[max_index];
36 
37  if (max_node == NULL) {
38  throw std::logic_error("MaxFunctionSelector failed to select a node.");
39  }
40  return max_node;
41  }
42 
43  // ----------------------------------------------------------------------
51  // ----------------------------------------------------------------------
52  virtual double evaluate(node_t *node) const = 0;
53 };
54 }
55 
56 #endif
selector selects the child of a node that maximises a given metric.
interface for all selection strategies. A selection strategy selects one child of a parent node accor...
basic interface for a node in the tree. Every node has to implement this functions.
Definition: inode.hpp:18
virtual double evaluate(node_t *node) const =0
this function has to be implemented in derived classes that specializes the metric to be used....
node_t * select(node_t *node)
select a child of node
virtual const vector< node_t * > children() const =0
get the children of the current node