-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMCTSNode.cpp
196 lines (168 loc) · 5.79 KB
/
MCTSNode.cpp
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
//
// Created by DFZ on 2018/4/18.
//
#include <cassert>
#include <iostream>
#include "MCTSNode.h"
MCTSNode::MCTSNode(MCTSNode *father, const Reversi &boardState, int color) : father(father), boardState(boardState),
color(color) {}
bool MCTSNode::isAllExpanded() {
// return children.size()==boardState.possibleMovement(color).size();
if (children.size() == 0) {
return false;
}
for (auto child : children) {
if (child->visitTimes == 0) { return false; }
}
return true;
}
std::shared_ptr<MCTSNode> MCTSNode::bestChild() {
double max = -1;
std::shared_ptr<MCTSNode> bestChild;
for (auto child : children) {
if (child->visitTimes == 0) {
continue;
}
double childUCB =
child->wonTimes / child->visitTimes + MCTSNode::C * sqrt(log(this->visitTimes / child->visitTimes));
if (childUCB > max) {
max = childUCB;
bestChild = child;
}
}
assert(bestChild != nullptr);
return bestChild;
}
std::shared_ptr<MCTSNode> MCTSNode::randomUnexpandedChild() {
std::random_device r;
size_t mean;
do {
std::default_random_engine e1(r());
std::uniform_int_distribution<size_t> uniform_dist(0, children.size() - 1);
mean = uniform_dist(e1);
} while (children[mean]->visitTimes != 0);
return children[mean];
}
void MCTSNode::genChildren() {
for (auto step : this->boardState.possibleMovement(color)) {
auto child = std::make_shared<MCTSNode>(this, this->boardState.next(step.first, step.second, color), -color);
// children.push_back(child);
children.emplace_back(child);
}
}
std::shared_ptr<MCTSNode> selection(std::shared_ptr<MCTSNode> node) {
while (node->children.size() != 0) {
if (node->isAllExpanded()) {
return selection(node->bestChild());
} else {
return selection(node->randomUnexpandedChild());
}
}
return node;
}
std::shared_ptr<MCTSNode> expansion(std::shared_ptr<MCTSNode> node) {
if (node->boardState.isTerminal()) {
return node;
} else {
node->genChildren();
return node->randomUnexpandedChild();
}
}
std::shared_ptr<MCTSNode> simulation(std::shared_ptr<MCTSNode> node) {
if (node->boardState.isTerminal()) { return node; }
else {
return node->completeGameRandomly();
}
}
void backpropagation(MCTSNode *node, int color) {
// node->win = node->boardState.isWin(color);
node->win = node->boardState.isWin(color);
bool win = node->win;
node->visitTimes++;
if (win) {
node->wonTimes++;
}
while (node->father != nullptr) {
node->father->visitTimes++;
if (win) {
node->father->wonTimes++;
}
node = node->father;
}
// std::cout << "back complete" << std::endl;
}
std::shared_ptr<MCTSNode> MCTSNode::completeGameRandomly() {
// return nullptr;
auto node = this;
std::shared_ptr<MCTSNode> retChild;
while (!node->boardState.isTerminal()) {
auto possibleMove = node->boardState.possibleMovement(node->color);
// std::cout << "possibleMove size: " << possibleMove.size() << std::endl;
if (possibleMove.size() == 0) {
// std::cout << "no possible movement, next player go" << std::endl;
auto child = std::make_shared<MCTSNode>(node, node->boardState, -node->color);
node->children.push_back(child);
node = child.get();
retChild = child;
continue;
}
std::random_device r;
std::default_random_engine e1(r());
std::uniform_int_distribution<size_t> uniform_dist(0, possibleMove.size() - 1);
size_t mean = uniform_dist(e1);
// std::cout << "movement: " << possibleMove[mean].first << " " << possibleMove[mean].second << " " << node->color
// << std::endl;
auto board = node->boardState.next(possibleMove[mean].first, possibleMove[mean].second, node->color);
// board.printBoard();
auto child = std::make_shared<MCTSNode>(node, board, -node->color);
node->children.push_back(child);
node = child.get();
retChild = child;
}
return retChild;
}
std::shared_ptr<MCTSNode> MCTSNode::decideNext() {
double max = -1;
std::shared_ptr<MCTSNode> bestChild = nullptr;
for (auto child : children) {
if (child->visitTimes == 0) {
continue;
}
double childUCB =
child->wonTimes / child->visitTimes + MCTSNode::C * sqrt(log(this->visitTimes / child->visitTimes));
if (childUCB > max) {
max = childUCB;
bestChild = child;
}
}
std::cout << "UCB: " << max << std::endl;
std::cout << "children size: " << children.size() << std::endl;
for (auto child : children) {
std::cout << "won times:" << child->wonTimes << " visit times: " << child->visitTimes << std::endl;
}
assert(bestChild);
bestChild->father = nullptr;
return bestChild;
}
std::shared_ptr<MCTSNode> MCTSNode::selectNext(int row, int column) {
if (this->children.size() == 0) {
genChildren();
}
for (auto child : children) {
if (child->boardState.getBoard()[row][column] == color) {
child->father = nullptr;
return child;
}
}
auto child = genChild(row, column);
std::cout << "I gen a child" << std::endl;
child->father = nullptr;
return child;
// assert(false);
// return nullptr;
}
std::shared_ptr<MCTSNode> MCTSNode::genChild(int row, int column) {
auto child = std::make_shared<MCTSNode>(this, this->boardState.next(row, column, color), -color);
children.push_back(child);
return child;
}