-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtreeNode.cpp
More file actions
146 lines (134 loc) · 3.73 KB
/
treeNode.cpp
File metadata and controls
146 lines (134 loc) · 3.73 KB
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#include "treeNode.h"
#include <time.h>
#include <iostream>
#include <algorithm>
#include <stdlib.h>
#include <stddef.h>
#include <math.h>
TreeNode::TreeNode(Reversi board, int height, TreeNode *parentNode) {
this->board = board;
this->height = height;
this->childrenNum = 0;
this->parent = parentNode;
this->rewards = 0;
this->visitNum = 0;
children = std::vector<TreeNode*>(28);
childVisitNum = std::vector<int>(28, 0);
nextMoves = std::vector<std::pair<int, int> >(28);
std::vector<std::pair<int, int> > tmp = this->board.getValidMoves();
for (int i = 0; i < tmp.size(); ++i) {
nextMoves[i] = tmp[i];
}
this->moveNum = tmp.size();
}
// Update visit number and reward from bottom to up
void TreeNode::update(double reward) {
this->rewards += reward;
this->visitNum += 1;
TreeNode *cur = this->parent;
while (cur) {
reward = 1 - reward;
cur->rewards += reward;
cur->visitNum += 1;
cur = cur->parent;
}
}
TreeNode* TreeNode::treePolicy() {
std::vector<std::pair<int, int> > moves;
TreeNode *node = this;
while (true) {
moves = node->board.getValidMoves();
if (moves.empty()) break;
if (node->moveNum == node->childrenNum) {
node = node->bestChild();
} else {
return node->expand();
}
}
return node;
}
TreeNode* TreeNode::expand() {
std::vector<int> unvisitedChild;
for (int i = 0; i < moveNum; ++i) {
if (childVisitNum[i] == 0) unvisitedChild.push_back(i);
}
int expandingIndex = unvisitedChild[rand() % unvisitedChild.size()];
childVisitNum[expandingIndex] = 1;
std::pair<int, int> move = this->nextMoves[expandingIndex];
Reversi nextBoard = this->board;
nextBoard.makeMove(move.first, move.second);
nextBoard.turnOver();
TreeNode *newNode =
new TreeNode(nextBoard, this->height + 1, this);
children[expandingIndex] = newNode;
++childrenNum;
return newNode;
}
TreeNode* TreeNode::bestChild() {
std::vector<TreeNode*> children = this->children;
double maxReward = -1e8;
int index = 0;
for (int i = 0; i < this->moveNum; ++i) {
double curReward =
ucb(children[i]->rewards, children[i]->visitNum, this->visitNum);
if (curReward > maxReward) {
maxReward = curReward;
index = i;
}
}
return children[index];
}
std::pair<int, int> TreeNode::bestMove() {
std::pair<int, int> move;
TreeNode *child = this->bestChild();
for (int i = 0; i < this->moveNum; ++i) {
if (child == this->children[i]) {
return this->nextMoves[i];
}
}
return move;
}
double TreeNode::ucb(double a, double b, double c) {
double C = 0.2;
return 1 - a / b + C * sqrt(2 * log(c) / b);
}
double TreeNode::defaultPolicy() {
Reversi game = this->board;
while (true) {
std::vector<std::pair<int, int> > moves = game.getValidMoves();
if (moves.empty()) break;
std::pair<int, int> move = moves[rand() % moves.size()];
game.makeMove(move.first, move.second);
game.turnOver();
}
int score = game.getScore(this->board.getPlayer());
if (score > 0) return 1;
else if (score < 0) return 0;
else return 0.5;
}
void TreeNode::printParameter() {
std::cout << "height: " << this->height <<
", childrenNum: " << this->childrenNum <<
", rewards: " << this->rewards <<
", visitNum: " << this->visitNum <<
", moveNum: " << this->moveNum << "\n";
}
TreeNode::~TreeNode() {
}
void TreeNode::deleteTree() {
if (this != NULL) {
for (int i = 0; i < this->moveNum; ++i) {
this->children[i]->deleteTree();
}
delete this;
}
}
std::vector<double> TreeNode::gatherData(int height) {
std::vector<double> result;
result.push_back(visitNum);
for (int i = 0; i < childrenNum; ++i) {
result.push_back(children[i]->visitNum);
result.push_back(children[i]->rewards);
}
return result;
}