-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgametree.h
103 lines (74 loc) · 2.63 KB
/
gametree.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#ifndef GAMETREE_H_
#define GAMETREE_H_
#include <string>
#include <utility>
#include "const.h"
#include "lib/json.h"
#include "memorypool.h"
#include "virtualboard.h"
class GameTree {
public:
GameTree();
~GameTree();
// Initialize GameTree.
void init(VirtualBoard* board);
// Monty Carlo Tree Search.
// Keep searching until reach 'cycle'.
// Return false if the search ends prematurely.
bool mcts(int cycle);
// Monty Carlo Tree Search.
// Stop if the point that count most times > 'minCount',
// will check it after each batch.
void mcts(int batch, int minCount);
// Monty Carlo Tree Search.
// Keep searching until 'thinking' becomes false,
void mcts(const bool* continueThinking);
// or reach the 'maxCycle'.
void mcts(int maxCycle, const bool* thinking);
// Monty Carlo Tree Search.
// Line 26's multi-thread version.
void mcts(int threadAmount, int batch, int minCount);
// Line 32's multi-thread version.
void mcts(int threadAmount, int maxCycle, const bool* thinking);
// get the child that has highest playout from current node
int mctsResult() const;
// Called when a REAL point is played,
// updates the current node and return game status.
GameStatus play(int index);
// pass
void pass();
// undo
void undo();
// Returns the whole tree in JSON format
std::string getTreeJSON();
// getter
VirtualBoard* getCurrentBoard() { return currentBoard_; }
private:
class Node;
// MCTS function.
// Keep select the child node until reach a leaf or a winning node.
// Return status and selectde node.
std::pair<SearchStatus, Node*> selection(VirtualBoard* board);
// MCTS function.
// Simulate the game and return status.
SearchStatus simulation(VirtualBoard* board) const;
// MCTS function
// Back propagation form leaf to current node.
void backProp(Node* node, SearchStatus result);
// Copy all children in 'srcNode' to 'destNode'.
void copyAllChildren(const Node* srcNode, Node* destNode);
// Copy tree, new tree's root is the current node of 'source'.
void copy(const GameTree* source);
// Merge all tree into this.
void mergeTree(GameTree* tree);
// Merge all children in 'mergedNode' into 'mergingNode'
void mergeAllChildren(Node* mergingNode, const Node* mergedNode);
void minusTree(GameTree* beMinusTree, const GameTree* minusTree);
void minusAllChildren(Node* beMinusNode, const Node* minusNode);
// returns the part of the tree below 'node' in JSON format
nlohmann::json getSubTreeJSON(Node *node, bool whoTurn, int threshold);
Node *root_, *currentNode_;
VirtualBoard* currentBoard_;
MemoryPool pool_;
};
#endif // GAMETREE_H_