libmcts
A Monte Carlo Tree Search Library
min_function_selector.hpp
1 #ifndef MIN_FUNCTION_SELECTOR_H
2 #define MIN_FUNCTION_SELECTOR_H
3 
4 #include <vector>
5 #include "iselection_strategy.hpp"
6 #include "inode.hpp"
7 
8 namespace mcts {
9 using std::vector;
10 
11 // ----------------------------------------------------------------------
16 // ----------------------------------------------------------------------
17 template <typename Context, typename Config>
18 class MinFunctionSelector : public ISelectionStrategy<Context, Config> {
19  typedef typename INode<Context, Config>::node_t node_t;
20 
21 public:
22  node_t *select(node_t *node) {
23  vector<node_t *> children = node->children();
24  vector<double> values(children.size());
25  node_t *min_node = NULL;
26 
27  for (unsigned i = 0; i < children.size(); i++) {
28  values[i] = evaluate(children[i]);
29  }
30 
31  size_t min_index =
32  std::min_element(values.begin(), values.end()) - values.begin();
33  min_node = children[min_index];
34 
35  if (min_node == NULL) {
36  throw std::logic_error("MinFunctionSelector failed to select a node.");
37  }
38  return min_node;
39  }
40 
41  // ----------------------------------------------------------------------
49  // ----------------------------------------------------------------------
50  virtual double evaluate(node_t *node) const = 0;
51 };
52 }
53 
54 #endif
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
selects a node that minimises a given metric
virtual double evaluate(node_t *node) const =0
this function has to be implemented in derived classes that specializes the metric to be used....
virtual const vector< node_t * > children() const =0
get the children of the current node
node_t * select(node_t *node)
select a child of node