-
Notifications
You must be signed in to change notification settings - Fork 0
/
monte_carlo.c
292 lines (263 loc) · 6.71 KB
/
monte_carlo.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
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
#include <stdio.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <omp.h>
#ifndef MAIN
#define MAIN
#include "state.c"
#include "dict.c"
#include "node.c"
#include "solver_common_light.c"
#include "utils.c"
#include "jkiss.c"
#undef MAIN
#endif
#define NUM_TRIES (12)
#define NUM_MAX_KILLS (12)
typedef size_t bin_t;
typedef unsigned char num_t;
typedef struct score_bins {
bin_t *bins;
num_t shift;
} score_bins;
score_bins score_bins_new(const state s)
{
score_bins sb;
int shift = popcount(s.playing_area);
sb.shift = shift;
sb.bins = calloc(2 * shift + 1, sizeof(bin_t));
return sb;
}
void score_bins_destroy(score_bins sb)
{
free(sb.bins);
}
void score_bins_reset(score_bins sb)
{
for (int i = 0; i < 2 * sb.shift + 1; i++) {
sb.bins[i] = 0;
}
}
void score_bins_add(score_bins sb, int score)
{
sb.bins[score + sb.shift]++;
}
void score_bins_merge(score_bins a, score_bins b)
{
for (int i = 0; i < 2 * a.shift + 1; i++) {
a.bins[i] += b.bins[i];
}
}
bin_t score_bins_total(score_bins sb)
{
int num_bins = 2 * sb.shift + 1;
bin_t total = 0;
for (bin_t i = 0; i < num_bins; i++) {
total += sb.bins[i];
}
return total;
}
double score_bins_average(score_bins sb)
{
int num_bins = 2 * sb.shift + 1;
bin_t total = 0;
bin_t expectation = 0;
for (bin_t i = 0; i < num_bins; i++) {
bin_t bin = sb.bins[i];
total += bin;
expectation += i * bin;
}
return expectation / (double) total - sb.shift;
}
void print_score_bins(const score_bins sb, int resolution)
{
int num_bins = 2 * sb.shift + 1;
bin_t total = 0;
bin_t expectation = 0;
bin_t max = 0;
for (bin_t i = 0; i < num_bins; i++) {
bin_t bin = sb.bins[i];
total += bin;
expectation += i * bin;
if (bin > max) {
max = bin;
}
}
for (int i = 0; i < num_bins; i++) {
for (size_t j = 0; j * max < resolution * sb.bins[i]; j++) {
printf("#");
}
printf("\n");
}
printf("Average %g\n", expectation / (double) total - sb.shift);
}
int uniform_move(state *s, const state_info si, jkiss *jk)
{
int index;
stones_t move;
int prisoners;
for (int i = 0; i < NUM_TRIES; i++) {
index = jkiss_step(jk) % si.num_moves;
move = si.moves[index];
if (make_move(s, move, &prisoners)) {
return prisoners;
}
}
return -1;
}
int playout(state *s, const state_info si, jkiss *jk)
{
int num_empty = popcount(s->playing_area ^ s->player ^ s->opponent);
for (int i = 0; i < num_empty; i++) {
if (uniform_move(s, si, jk) < 0) {
return 0;
}
}
return 1;
}
// Ignores target and immortal
int kill_group(state *s)
{
stones_t chain, p;
stones_t opponent = s->opponent;
for (int i = 0; i < WIDTH; i++) {
for (int j = 0; j < HEIGHT; j += 2) {
p = (1ULL | (1ULL << V_SHIFT)) << (i + j * V_SHIFT);
chain = flood(p, opponent);
opponent ^= chain;
stones_t libs = liberties(chain, s->playing_area & ~s->player);
if (popcount(libs) == 1) {
int prisoners;
make_move(s, libs, &prisoners);
return 1;
}
}
}
return 0;
}
void kill_groups(state *s)
{
int prisoners;
int misses = 0;
int i = 0;
while (misses < 2 && i++ < NUM_MAX_KILLS) {
if (kill_group(s)) {
misses = 0;
}
else {
make_move(s, 0, &prisoners);
misses++;
}
}
}
int clean_playout(state s, const state_info si, jkiss *jk)
{
playout(&s, si, jk);
kill_groups(&s);
int score = chinese_liberty_score(&s);
if (s.white_to_play) {
return -score;
}
return score;
}
void monte_carlo(state s, const state_info si, score_bins sb, size_t num_iter)
{
int num_threads = omp_get_max_threads();
jkiss *jks = malloc(num_threads * sizeof(jkiss));
score_bins *sbs = malloc(num_threads * sizeof(score_bins));
for (int i = 0; i < num_threads; i++) {
sbs[i] = score_bins_new(s);
jkiss_init(jks + i);
}
size_t i;
int tid;
int finished = 0;
#pragma omp parallel private(i, tid)
{
tid = omp_get_thread_num();
for (i = 0; i < num_iter; i++) {
score_bins_add(sbs[tid], clean_playout(s, si, jks + tid));
// Bailout once one thread finishes.
if (finished) {
break;
}
}
finished = 1;
}
for (i = 0; i < num_threads; i++) {
score_bins_merge(sb, sbs[i]);
score_bins_destroy(sbs[i]);
}
}
#ifndef MAIN
int main()
{
size_t i;
size_t num_iter_single = 1000000;
size_t num_iter_parallel = 10000000;
state base_state = (state) {rectangle(7, 7), 0, 0, 0, 0, 0, 0};
int asdf;
make_move(&base_state, one(3, 3), &asdf);
state_info si;
init_state(&base_state, &si);
state s = base_state;
jkiss jk;
jkiss_init(&jk);
printf("%d\n", playout(&s, si, &jk));
print_state(&s);
kill_groups(&s);
print_state(&s);
printf("%d\n", chinese_liberty_score(&s));
score_bins sb = score_bins_new(base_state);
for (i = 0; i < num_iter_single; i++) {
score_bins_add(sb, clean_playout(base_state, si, &jk));
}
print_score_bins(sb, 50);
printf("-----------------------------\n");
score_bins_reset(sb);
monte_carlo(base_state, si, sb, num_iter_parallel);
print_score_bins(sb, 50);
/*
int num_threads = omp_get_max_threads();
jkiss *jks = malloc(num_threads * sizeof(jkiss));
bin_t **binss = malloc(num_threads * sizeof(bin_t*));
for (int i = 0; i < num_threads; i++) {
binss[i] = calloc(num_bins, sizeof(bin_t));
jkiss_init(jks + i);
}
int tid;
int finished = 0;
#pragma omp parallel private(i, s, tid)
{
tid = omp_get_thread_num();
for (i = 0; i < num_iter_parallel; i++) {
s = base_state;
playout(&s, si, jks + tid);
kill_groups(&s);
int score = chinese_liberty_score(&s);
if (s.white_to_play) {
score = -score;
}
binss[tid][si.size + score]++;
// Bailout once one thread finishes.
if (finished) {
break;
}
}
finished = 1;
}
for (i = 0; i < num_bins; i++) {
bins[i] = 0;
}
for (i = 0; i < num_threads; i++) {
for (int j = 0; j < num_bins; j++) {
bins[j] += binss[i][j];
}
}
print_bins(bins, num_bins, 128);
*/
return 0;
}
#endif