forked from sysprog21/lab0-c
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmcts.c
157 lines (147 loc) · 4.21 KB
/
mcts.c
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
#include <assert.h>
#include <float.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include "fixpoint.h"
#include "game.h"
#include "mcts.h"
#include "util.h"
struct node {
int move;
char player;
int n_visits;
long score;
struct node *parent;
struct node *children[N_GRIDS];
};
static struct node *new_node(int move, char player, struct node *parent)
{
struct node *node = malloc(sizeof(struct node));
node->move = move;
node->player = player;
node->n_visits = 0;
node->score = 0;
node->parent = parent;
memset(node->children, 0, sizeof(node->children));
return node;
}
static void free_node(struct node *node)
{
for (int i = 0; i < N_GRIDS; i++)
if (node->children[i])
free_node(node->children[i]);
free(node);
}
static inline long uct_score(int n_total, int n_visits, long score)
{
if (n_visits == 0)
return FIXED_MAX;
// return score / n_visits +
// EXPLORATION_FACTOR * sqrt(log(n_total) / n_visits);
return score / n_visits +
(EXPLORATION_FACTOR *
fix_sqrt(fix_log10((long) n_total << SCALE_FACTOR) /
n_visits) >>
SCALE_FACTOR);
}
static struct node *select_move(struct node *node)
{
struct node *best_node = NULL;
long best_score = FIXED_MIN;
for (int i = 0; i < N_GRIDS; i++) {
if (!node->children[i])
continue;
long score = uct_score(node->n_visits, node->children[i]->n_visits,
node->children[i]->score);
if (score > best_score) {
best_score = score;
best_node = node->children[i];
}
}
return best_node;
}
static long simulate(char *table, char player)
{
char current_player = player;
char temp_table[N_GRIDS];
memcpy(temp_table, table, N_GRIDS);
while (1) {
int *moves = available_moves(temp_table);
if (moves[0] == -1) {
free(moves);
break;
}
int n_moves = 0;
while (n_moves < N_GRIDS && moves[n_moves] != -1)
++n_moves;
int move = moves[rand() % n_moves];
free(moves);
temp_table[move] = current_player;
char win;
if ((win = check_win(temp_table)) != ' ')
return calculate_win_value(win, player);
current_player ^= 'O' ^ 'X';
}
return 1UL << (SCALE_FACTOR - 1);
}
static void backpropagate(struct node *node, long score)
{
while (node) {
node->n_visits++;
node->score += score;
node = node->parent;
score = FIXED_1 - score;
}
}
static void expand(struct node *node, char *table)
{
int *moves = available_moves(table);
int n_moves = 0;
while (n_moves < N_GRIDS && moves[n_moves] != -1)
++n_moves;
for (int i = 0; i < n_moves; i++) {
node->children[i] = new_node(moves[i], node->player ^ 'O' ^ 'X', node);
}
free(moves);
}
int mcts(char *table, char player)
{
char win;
struct node *root = new_node(-1, player, NULL);
for (int i = 0; i < ITERATIONS; i++) {
struct node *node = root;
char temp_table[N_GRIDS];
memcpy(temp_table, table, N_GRIDS);
while (1) {
if ((win = check_win(temp_table)) != ' ') {
long score = calculate_win_value(win, node->player ^ 'O' ^ 'X');
backpropagate(node, score);
break;
}
if (node->n_visits == 0) {
long score = simulate(temp_table, node->player);
backpropagate(node, score);
break;
}
if (node->children[0] == NULL)
expand(node, temp_table);
node = select_move(node);
assert(node);
temp_table[node->move] = node->player ^ 'O' ^ 'X';
}
}
struct node *best_node = NULL;
int most_visits = -1;
for (int i = 0; i < N_GRIDS; i++) {
if (root->children[i] && root->children[i]->n_visits > most_visits) {
most_visits = root->children[i]->n_visits;
best_node = root->children[i];
}
}
if (!best_node)
return -1;
int best_move = best_node->move;
free_node(root);
return best_move;
}