-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathUCT_Simulator.h
More file actions
142 lines (119 loc) · 3.16 KB
/
Copy pathUCT_Simulator.h
File metadata and controls
142 lines (119 loc) · 3.16 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
#pragma once
#ifndef UCT_SIMULATOR_H
#define UCT_SIMULATOR_H
#include <vector>
#include <cstring>
#include <cstdlib>
#include <ctime>
#include "GoBoard.h"
#include <iostream>
#include <cmath>
const double INFINITY_DOUBLE = 1e20;
const double TIME_LIMIT = 2;
#define POS(i, j) ((i) * GoBoard::BOARD_SIZE + (j))
#define I(pos) ((pos) / GoBoard::BOARD_SIZE)
#define J(pos) ((pos) % GoBoard::BOARD_SIZE)
typedef struct Node* Tree;
struct Node {
static const int BUFFER_SIZE = 100;
static Node bufferNode[BUFFER_SIZE];
static int tail;
bool visited;
double value;
int nb, move_i, move_j;
GoBoard board;
std::vector<Tree> children;
static Tree newNode(const GoBoard& board, int move_i, int move_j){
Tree tree = bufferNode + tail++;
tree->visited = false;
tree->value = 0;
tree->nb = 0;
tree->board = board;
tree->move_i = move_i;
tree->move_j = move_j;
return tree;
}
double getValueByMC(int color) {
static GoBoard test;
test = board;
double res = test.simulateRandomly(color);
if (res > 0) return 1+res/1e3;
else return res/1e3;
}
Tree descendByUCB1() {
double maxV = -INFINITY_DOUBLE;
Tree res = NULL;
for (int i = 0; i < (int)children.size(); ++i) {
double v = INFINITY_DOUBLE;
if (children[i]->nb > 0) {
v = -children[i]->value / children[i]->nb + sqrt(2 * log(nb) / children[i]->nb);
}
if (v > maxV) {
maxV = v;
res = children[i];
}
}
return res;
}
void forkChildren(int color);
void selectMove(int* i, int* j) {
double maxNB = -INFINITY_DOUBLE;
for (int k = 0; k < (int)children.size(); ++k) {
if (children[k]->value / children[k]->nb > maxNB) {
maxNB = children[k]->value / children[k]->nb;
*i = children[k]->move_i;
*j = children[k]->move_j;
}
}
std::cout << maxNB << std::endl;
}
};
class UCT_Simulator
{
public:
UCT_Simulator();
~UCT_Simulator();
void initialize(GoBoard initialBoard, int color) {
Node::tail = 0;
root = Node::newNode(initialBoard, -2, -2);
this->initialColor = color;
}
void getResult(int* i, int* j) {
double st_clock = clock();
int cnt = 0;
while (clock() - st_clock < TIME_LIMIT * CLOCKS_PER_SEC && Node::tail + 169 < Node::BUFFER_SIZE) {
//std::cout << "step 1" << std::endl;
playOneSequence();
}
root->selectMove(i, j);
}
private:
Tree root;
int initialColor;
void playOneSequence() {
static Tree sequence[400];
sequence[0] = root;
double st = clock();
int i = 0;
int color = initialColor;
int cnt = 0;
while (sequence[i] != NULL && sequence[i]->visited && ++cnt <200) {
sequence[i + 1] = sequence[i]->descendByUCB1();
++i;
color = GoBoard::other_color(color);
}
//std::cout << "step 2" << std::endl;
double value = sequence[i]->getValueByMC(color);
//std::cout << "step 3" << std::endl;
sequence[i]->forkChildren(color);
//sequence[i]->board.show();
//system("pause");
//std::cout << "step 4" << std::endl;
sequence[i]->visited = true;
for (int j = i; j >= 0; --j, value = -value) {
sequence[j]->value += value;
++sequence[j]->nb;
}
}
};
#endif