You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

944 lines
24 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include "darknet.h"
  2. namespace DN{
  3. int inverted = 1;
  4. int noi = 1;
  5. static const int nind = 2;
  6. typedef struct {
  7. char **data;
  8. int n;
  9. } moves;
  10. char *fgetgo(FILE *fp)
  11. {
  12. if (feof(fp)) return 0;
  13. size_t size = 94;
  14. char *line = (char*)malloc(size*sizeof(char));
  15. if (size != fread(line, sizeof(char), size, fp)){
  16. free(line);
  17. return 0;
  18. }
  19. return line;
  20. }
  21. moves load_go_moves(char *filename)
  22. {
  23. moves m;
  24. m.n = 128;
  25. m.data = (char**)calloc(128, sizeof(char*));
  26. FILE *fp = fopen(filename, "rb");
  27. int count = 0;
  28. char *line = 0;
  29. while ((line = fgetgo(fp))){
  30. if (count >= m.n){
  31. m.n *= 2;
  32. m.data = (char**)realloc(m.data, m.n*sizeof(char*));
  33. }
  34. m.data[count] = line;
  35. ++count;
  36. }
  37. printf("%d\n", count);
  38. m.n = count;
  39. m.data = (char**)realloc(m.data, count*sizeof(char*));
  40. return m;
  41. }
  42. void string_to_board(char *s, float *board)
  43. {
  44. int i, j;
  45. //memset(board, 0, 1*19*19*sizeof(float));
  46. int count = 0;
  47. for (i = 0; i < 91; ++i){
  48. char c = s[i];
  49. for (j = 0; j < 4; ++j){
  50. int me = (c >> (2 * j)) & 1;
  51. int you = (c >> (2 * j + 1)) & 1;
  52. if (me) board[count] = 1;
  53. else if (you) board[count] = -1;
  54. else board[count] = 0;
  55. ++count;
  56. if (count >= 19 * 19) break;
  57. }
  58. }
  59. }
  60. void board_to_string(char *s, float *board)
  61. {
  62. int i, j;
  63. memset(s, 0, (19 * 19 / 4 + 1)*sizeof(char));
  64. int count = 0;
  65. for (i = 0; i < 91; ++i){
  66. for (j = 0; j < 4; ++j){
  67. int me = (board[count] == 1);
  68. int you = (board[count] == -1);
  69. if (me) s[i] = s[i] | (1 << (2 * j));
  70. if (you) s[i] = s[i] | (1 << (2 * j + 1));
  71. ++count;
  72. if (count >= 19 * 19) break;
  73. }
  74. }
  75. }
  76. data random_go_moves(moves m, int n)
  77. {
  78. data d = { 0 };
  79. d.X = make_matrix(n, 19 * 19);
  80. d.y = make_matrix(n, 19 * 19 + 1);
  81. int i;
  82. for (i = 0; i < n; ++i){
  83. float *board = d.X.vals[i];
  84. float *label = d.y.vals[i];
  85. char *b = m.data[rand() % m.n];
  86. int row = b[0];
  87. int col = b[1];
  88. if (row >= 19 || col >= 19){
  89. label[19 * 19] = 1;
  90. }
  91. else {
  92. label[col + 19 * row] = 1;
  93. string_to_board(b + 2, board);
  94. if (board[col + 19 * row]) printf("hey\n");
  95. }
  96. int flip = rand() % 2;
  97. int rotate = rand() % 4;
  98. image in = float_to_image(19, 19, 1, board);
  99. image out = float_to_image(19, 19, 1, label);
  100. if (flip){
  101. flip_image(in);
  102. flip_image(out);
  103. }
  104. rotate_image_cw(in, rotate);
  105. rotate_image_cw(out, rotate);
  106. }
  107. return d;
  108. }
  109. void train_go(char *cfgfile, char *weightfile, char *filename, int *gpus, int ngpus, int clear)
  110. {
  111. int i;
  112. float avg_loss = -1;
  113. char *base = basecfg(cfgfile);
  114. printf("%s\n", base);
  115. printf("%d\n", ngpus);
  116. network *nets = (network*)calloc(ngpus, sizeof(network));
  117. srand((unsigned int)time(0));
  118. int seed = rand();
  119. for (i = 0; i < ngpus; ++i){
  120. srand(seed);
  121. #ifdef GPU
  122. cuda_set_device(gpus[i]);
  123. #endif
  124. nets[i] = load_network(cfgfile, weightfile, clear);
  125. nets[i].learning_rate *= ngpus;
  126. }
  127. network net = nets[0];
  128. printf("Learning Rate: %g, Momentum: %g, Decay: %g\n", net.learning_rate, net.momentum, net.decay);
  129. char *backup_directory = "/home/pjreddie/backup/";
  130. char buff[256];
  131. moves m = load_go_moves(filename);
  132. //moves m = load_go_moves("games.txt");
  133. int N = m.n;
  134. printf("Moves: %d\n", N);
  135. int epoch = (*net.seen) / N;
  136. while (((int)get_current_batch(net)) < net.max_batches || net.max_batches == 0){
  137. clock_t time = clock();
  138. data train = random_go_moves(m, net.batch*net.subdivisions*ngpus);
  139. printf("Loaded: %lf seconds\n", sec(clock() - time));
  140. time = clock();
  141. float loss = 0;
  142. #ifdef GPU
  143. if(ngpus == 1){
  144. loss = train_network(net, train);
  145. } else {
  146. loss = train_networks(nets, ngpus, train, 4);
  147. }
  148. #else
  149. loss = train_network(net, train);
  150. #endif
  151. free_data(train);
  152. if (avg_loss == -1) avg_loss = loss;
  153. avg_loss = avg_loss*.95f + loss*.05f;
  154. printf("%ld, %.3f: %f, %f avg, %f rate, %lf seconds, %ld images\n", get_current_batch(net), (float)(*net.seen) / N, loss, avg_loss, get_current_rate(net), sec(clock() - time), *net.seen);
  155. if (((int)*net.seen) / N > epoch){
  156. epoch = *net.seen / N;
  157. char buff[256];
  158. sprintf(buff, "%s/%s_%d.weights", backup_directory, base, epoch);
  159. save_weights(net, buff);
  160. }
  161. if (get_current_batch(net) % 1000 == 0){
  162. char buff[256];
  163. sprintf(buff, "%s/%s.backup", backup_directory, base);
  164. save_weights(net, buff);
  165. }
  166. if (get_current_batch(net) % 10000 == 0){
  167. char buff[256];
  168. sprintf(buff, "%s/%s_%ld.backup", backup_directory, base, get_current_batch(net));
  169. save_weights(net, buff);
  170. }
  171. }
  172. sprintf(buff, "%s/%s.weights", backup_directory, base);
  173. save_weights(net, buff);
  174. free_network(net);
  175. free(base);
  176. }
  177. void propagate_liberty(float *board, int *lib, int *visited, int row, int col, int side)
  178. {
  179. if (row < 0 || row > 18 || col < 0 || col > 18) return;
  180. int index = row * 19 + col;
  181. if (board[index] != side) return;
  182. if (visited[index]) return;
  183. visited[index] = 1;
  184. lib[index] += 1;
  185. propagate_liberty(board, lib, visited, row + 1, col, side);
  186. propagate_liberty(board, lib, visited, row - 1, col, side);
  187. propagate_liberty(board, lib, visited, row, col + 1, side);
  188. propagate_liberty(board, lib, visited, row, col - 1, side);
  189. }
  190. int *calculate_liberties(float *board)
  191. {
  192. int *lib = (int*)calloc(19 * 19, sizeof(int));
  193. int visited[361];
  194. int i, j;
  195. for (j = 0; j < 19; ++j){
  196. for (i = 0; i < 19; ++i){
  197. memset(visited, 0, 19 * 19 * sizeof(int));
  198. int index = j * 19 + i;
  199. if (board[index] == 0){
  200. if ((i > 0) && board[index - 1]) propagate_liberty(board, lib, visited, j, i - 1, (int)board[index - 1]);
  201. if ((i < 18) && board[index + 1]) propagate_liberty(board, lib, visited, j, i + 1, (int)board[index + 1]);
  202. if ((j > 0) && board[index - 19]) propagate_liberty(board, lib, visited, j - 1, i, (int)board[index - 19]);
  203. if ((j < 18) && board[index + 19]) propagate_liberty(board, lib, visited, j + 1, i, (int)board[index + 19]);
  204. }
  205. }
  206. }
  207. return lib;
  208. }
  209. void print_board(FILE *stream, float *board, int swap, int *indexes)
  210. {
  211. int i, j, n;
  212. fprintf(stream, " ");
  213. for (i = 0; i < 19; ++i){
  214. fprintf(stream, "%c ", 'A' + i + 1 * (i > 7 && noi));
  215. }
  216. fprintf(stream, "\n");
  217. for (j = 0; j < 19; ++j){
  218. fprintf(stream, "%2d", (inverted) ? 19 - j : j + 1);
  219. for (i = 0; i < 19; ++i){
  220. int index = j * 19 + i;
  221. if (indexes){
  222. int found = 0;
  223. for (n = 0; n < nind; ++n){
  224. if (index == indexes[n]){
  225. found = 1;
  226. /*
  227. if(n == 0) fprintf(stream, "\uff11");
  228. else if(n == 1) fprintf(stream, "\uff12");
  229. else if(n == 2) fprintf(stream, "\uff13");
  230. else if(n == 3) fprintf(stream, "\uff14");
  231. else if(n == 4) fprintf(stream, "\uff15");
  232. */
  233. if (n == 0) fprintf(stream, " 1");
  234. else if (n == 1) fprintf(stream, " 2");
  235. else if (n == 2) fprintf(stream, " 3");
  236. else if (n == 3) fprintf(stream, " 4");
  237. else if (n == 4) fprintf(stream, " 5");
  238. }
  239. }
  240. if (found) continue;
  241. }
  242. //if(board[index]*-swap > 0) fprintf(stream, "\u25C9 ");
  243. //else if(board[index]*-swap < 0) fprintf(stream, "\u25EF ");
  244. if (board[index] * -swap > 0) fprintf(stream, " O");
  245. else if (board[index] * -swap < 0) fprintf(stream, " X");
  246. else fprintf(stream, " ");
  247. }
  248. fprintf(stream, "\n");
  249. }
  250. }
  251. void flip_board(float *board)
  252. {
  253. int i;
  254. for (i = 0; i < 19 * 19; ++i){
  255. board[i] = -board[i];
  256. }
  257. }
  258. void predict_move(network net, float *board, float *move, int multi)
  259. {
  260. float *output = network_predict(net, board);
  261. copy_cpu(19 * 19 + 1, output, 1, move, 1);
  262. int i;
  263. if (multi){
  264. image bim = float_to_image(19, 19, 1, board);
  265. for (i = 1; i < 8; ++i){
  266. rotate_image_cw(bim, i);
  267. if (i >= 4) flip_image(bim);
  268. float *output = network_predict(net, board);
  269. image oim = float_to_image(19, 19, 1, output);
  270. if (i >= 4) flip_image(oim);
  271. rotate_image_cw(oim, -i);
  272. axpy_cpu(19 * 19 + 1, 1, output, 1, move, 1);
  273. if (i >= 4) flip_image(bim);
  274. rotate_image_cw(bim, -i);
  275. }
  276. scal_cpu(19 * 19 + 1, 1. / 8., move, 1);
  277. }
  278. for (i = 0; i < 19 * 19; ++i){
  279. if (board[i]) move[i] = 0;
  280. }
  281. }
  282. void remove_connected(float *b, int *lib, int p, int r, int c)
  283. {
  284. if (r < 0 || r >= 19 || c < 0 || c >= 19) return;
  285. if (b[r * 19 + c] != p) return;
  286. if (lib[r * 19 + c] != 1) return;
  287. b[r * 19 + c] = 0;
  288. remove_connected(b, lib, p, r + 1, c);
  289. remove_connected(b, lib, p, r - 1, c);
  290. remove_connected(b, lib, p, r, c + 1);
  291. remove_connected(b, lib, p, r, c - 1);
  292. }
  293. void move_go(float *b, int p, int r, int c)
  294. {
  295. int *l = calculate_liberties(b);
  296. b[r * 19 + c] = (float)p;
  297. remove_connected(b, l, -p, r + 1, c);
  298. remove_connected(b, l, -p, r - 1, c);
  299. remove_connected(b, l, -p, r, c + 1);
  300. remove_connected(b, l, -p, r, c - 1);
  301. free(l);
  302. }
  303. int makes_safe_go(float *b, int *lib, int p, int r, int c){
  304. if (r < 0 || r >= 19 || c < 0 || c >= 19) return 0;
  305. if (b[r * 19 + c] == -p){
  306. if (lib[r * 19 + c] > 1) return 0;
  307. else return 1;
  308. }
  309. if (b[r * 19 + c] == 0) return 1;
  310. if (lib[r * 19 + c] > 1) return 1;
  311. return 0;
  312. }
  313. int suicide_go(float *b, int p, int r, int c)
  314. {
  315. int *l = calculate_liberties(b);
  316. int safe = 0;
  317. safe = safe || makes_safe_go(b, l, p, r + 1, c);
  318. safe = safe || makes_safe_go(b, l, p, r - 1, c);
  319. safe = safe || makes_safe_go(b, l, p, r, c + 1);
  320. safe = safe || makes_safe_go(b, l, p, r, c - 1);
  321. free(l);
  322. return !safe;
  323. }
  324. int legal_go(float *b, char *ko, int p, int r, int c)
  325. {
  326. if (b[r * 19 + c]) return 0;
  327. char curr[91];
  328. char next[91];
  329. board_to_string(curr, b);
  330. move_go(b, p, r, c);
  331. board_to_string(next, b);
  332. string_to_board(curr, b);
  333. if (memcmp(next, ko, 91) == 0) return 0;
  334. return 1;
  335. }
  336. int generate_move(network net, int player, float *board, int multi, float thresh, float temp, char *ko, int print)
  337. {
  338. int i, j;
  339. int empty = 1;
  340. for (i = 0; i < 19 * 19; ++i){
  341. if (board[i]) {
  342. empty = 0;
  343. break;
  344. }
  345. }
  346. if (empty) {
  347. return 72;
  348. }
  349. for (i = 0; i < net.n; ++i) net.layers[i].temperature = temp;
  350. float move[362];
  351. if (player < 0) flip_board(board);
  352. predict_move(net, board, move, multi);
  353. if (player < 0) flip_board(board);
  354. for (i = 0; i < 19; ++i){
  355. for (j = 0; j < 19; ++j){
  356. if (!legal_go(board, ko, player, i, j)) move[i * 19 + j] = 0;
  357. }
  358. }
  359. int indexes[nind];
  360. top_k(move, 19 * 19 + 1, nind, indexes);
  361. if (thresh > move[indexes[0]]) thresh = move[indexes[nind - 1]];
  362. for (i = 0; i < 19 * 19 + 1; ++i){
  363. if (move[i] < thresh) move[i] = 0;
  364. }
  365. int max = max_index(move, 19 * 19 + 1);
  366. int row = max / 19;
  367. int col = max % 19;
  368. int index = sample_array(move, 19 * 19 + 1);
  369. if (print){
  370. top_k(move, 19 * 19 + 1, nind, indexes);
  371. for (i = 0; i < nind; ++i){
  372. if (!move[indexes[i]]) indexes[i] = -1;
  373. }
  374. print_board(stderr, board, player, indexes);
  375. for (i = 0; i < nind; ++i){
  376. fprintf(stderr, "%d: %f\n", i + 1, move[indexes[i]]);
  377. }
  378. }
  379. if (row == 19) return -1;
  380. if (suicide_go(board, player, row, col)){
  381. return -1;
  382. }
  383. if (suicide_go(board, player, index / 19, index % 19)){
  384. index = max;
  385. }
  386. if (index == 19 * 19) return -1;
  387. return index;
  388. }
  389. void valid_go(char *cfgfile, char *weightfile, int multi, char *filename)
  390. {
  391. srand((unsigned int)time(0));
  392. char *base = basecfg(cfgfile);
  393. printf("%s\n", base);
  394. network net = parse_network_cfg(cfgfile);
  395. if (weightfile){
  396. load_weights(&net, weightfile);
  397. }
  398. set_batch_network(&net, 1);
  399. printf("Learning Rate: %g, Momentum: %g, Decay: %g\n", net.learning_rate, net.momentum, net.decay);
  400. float *board = (float*)calloc(19 * 19, sizeof(float));
  401. float *move = (float*)calloc(19 * 19 + 1, sizeof(float));
  402. // moves m = load_go_moves("/home/pjreddie/backup/go.test");
  403. moves m = load_go_moves(filename);
  404. int N = m.n;
  405. int i;
  406. int correct = 0;
  407. for (i = 0; i < N; ++i){
  408. char *b = m.data[i];
  409. int row = b[0];
  410. int col = b[1];
  411. int truth = col + 19 * row;
  412. string_to_board(b + 2, board);
  413. predict_move(net, board, move, multi);
  414. int index = max_index(move, 19 * 19);
  415. if (index == truth) ++correct;
  416. printf("%d Accuracy %f\n", i, (float)correct / (i + 1));
  417. }
  418. }
  419. int print_game(float *board, FILE *fp)
  420. {
  421. int i, j;
  422. int count = 3;
  423. fprintf(fp, "komi 6.5\n");
  424. fprintf(fp, "boardsize 19\n");
  425. fprintf(fp, "clear_board\n");
  426. for (j = 0; j < 19; ++j){
  427. for (i = 0; i < 19; ++i){
  428. if (board[j * 19 + i] == 1) fprintf(fp, "play black %c%d\n", 'A' + i + (i >= 8), 19 - j);
  429. if (board[j * 19 + i] == -1) fprintf(fp, "play white %c%d\n", 'A' + i + (i >= 8), 19 - j);
  430. if (board[j * 19 + i]) ++count;
  431. }
  432. }
  433. return count;
  434. }
  435. void engine_go(char *filename, char *weightfile, int multi)
  436. {
  437. network net = parse_network_cfg(filename);
  438. if (weightfile){
  439. load_weights(&net, weightfile);
  440. }
  441. srand((unsigned int)time(0));
  442. set_batch_network(&net, 1);
  443. float *board = (float*)calloc(19 * 19, sizeof(float));
  444. char *one = (char*)calloc(91, sizeof(char));
  445. char *two = (char*)calloc(91, sizeof(char));
  446. int passed = 0;
  447. while (1){
  448. char buff[256];
  449. int id = 0;
  450. int has_id = (scanf("%d", &id) == 1);
  451. scanf("%s", buff);
  452. if (feof(stdin)) break;
  453. char ids[256];
  454. sprintf(ids, "%d", id);
  455. //fprintf(stderr, "%s\n", buff);
  456. if (!has_id) ids[0] = 0;
  457. if (!strcmp(buff, "protocol_version")){
  458. printf("=%s 2\n\n", ids);
  459. }
  460. else if (!strcmp(buff, "name")){
  461. printf("=%s DarkGo\n\n", ids);
  462. }
  463. else if (!strcmp(buff, "time_settings") || !strcmp(buff, "time_left")){
  464. char *line = fgetl(stdin);
  465. free(line);
  466. printf("=%s \n\n", ids);
  467. }
  468. else if (!strcmp(buff, "version")){
  469. printf("=%s 1.0. Want more DarkGo? You can find me on OGS, unlimited games, no waiting! https://online-go.com/user/view/434218\n\n", ids);
  470. }
  471. else if (!strcmp(buff, "known_command")){
  472. char comm[256];
  473. scanf("%s", comm);
  474. int known = (!strcmp(comm, "protocol_version") ||
  475. !strcmp(comm, "name") ||
  476. !strcmp(comm, "version") ||
  477. !strcmp(comm, "known_command") ||
  478. !strcmp(comm, "list_commands") ||
  479. !strcmp(comm, "quit") ||
  480. !strcmp(comm, "boardsize") ||
  481. !strcmp(comm, "clear_board") ||
  482. !strcmp(comm, "komi") ||
  483. !strcmp(comm, "final_status_list") ||
  484. !strcmp(comm, "play") ||
  485. !strcmp(comm, "genmove_white") ||
  486. !strcmp(comm, "genmove_black") ||
  487. !strcmp(comm, "fixed_handicap") ||
  488. !strcmp(comm, "genmove"));
  489. if (known) printf("=%s true\n\n", ids);
  490. else printf("=%s false\n\n", ids);
  491. }
  492. else if (!strcmp(buff, "list_commands")){
  493. printf("=%s protocol_version\nshowboard\nname\nversion\nknown_command\nlist_commands\nquit\nboardsize\nclear_board\nkomi\nplay\ngenmove_black\ngenmove_white\ngenmove\nfinal_status_list\nfixed_handicap\n\n", ids);
  494. }
  495. else if (!strcmp(buff, "quit")){
  496. break;
  497. }
  498. else if (!strcmp(buff, "boardsize")){
  499. int boardsize = 0;
  500. scanf("%d", &boardsize);
  501. //fprintf(stderr, "%d\n", boardsize);
  502. if (boardsize != 19){
  503. printf("?%s unacceptable size\n\n", ids);
  504. }
  505. else {
  506. memset(board, 0, 19 * 19 * sizeof(float));
  507. printf("=%s \n\n", ids);
  508. }
  509. }
  510. else if (!strcmp(buff, "fixed_handicap")){
  511. int handicap = 0;
  512. scanf("%d", &handicap);
  513. int indexes[] = { 72, 288, 300, 60, 180, 174, 186, 66, 294 };
  514. int i;
  515. for (i = 0; i < handicap; ++i){
  516. board[indexes[i]] = 1;
  517. }
  518. }
  519. else if (!strcmp(buff, "clear_board")){
  520. passed = 0;
  521. memset(board, 0, 19 * 19 * sizeof(float));
  522. printf("=%s \n\n", ids);
  523. }
  524. else if (!strcmp(buff, "komi")){
  525. float komi = 0;
  526. scanf("%f", &komi);
  527. printf("=%s \n\n", ids);
  528. }
  529. else if (!strcmp(buff, "showboard")){
  530. printf("=%s \n", ids);
  531. print_board(stdout, board, 1, 0);
  532. printf("\n");
  533. }
  534. else if (!strcmp(buff, "play") || !strcmp(buff, "black") || !strcmp(buff, "white")){
  535. char color[256];
  536. if (!strcmp(buff, "play"))
  537. {
  538. scanf("%s ", color);
  539. }
  540. else {
  541. scanf(" ");
  542. color[0] = buff[0];
  543. }
  544. char c;
  545. int r;
  546. int count = scanf("%c%d", &c, &r);
  547. int player = (color[0] == 'b' || color[0] == 'B') ? 1 : -1;
  548. if ((c == 'p' || c == 'P') && count < 2) {
  549. passed = 1;
  550. printf("=%s \n\n", ids);
  551. char *line = fgetl(stdin);
  552. free(line);
  553. fflush(stdout);
  554. fflush(stderr);
  555. continue;
  556. }
  557. else {
  558. passed = 0;
  559. }
  560. if (c >= 'A' && c <= 'Z') c = c - 'A';
  561. if (c >= 'a' && c <= 'z') c = c - 'a';
  562. if (c >= 8) --c;
  563. r = 19 - r;
  564. fprintf(stderr, "move: %d %d\n", r, c);
  565. char *swap = two;
  566. two = one;
  567. one = swap;
  568. move_go(board, player, r, c);
  569. board_to_string(one, board);
  570. printf("=%s \n\n", ids);
  571. //print_board(stderr, board, 1, 0);
  572. }
  573. else if (!strcmp(buff, "genmove") || !strcmp(buff, "genmove_black") || !strcmp(buff, "genmove_white")){
  574. int player = 0;
  575. if (!strcmp(buff, "genmove")){
  576. char color[256];
  577. scanf("%s", color);
  578. player = (color[0] == 'b' || color[0] == 'B') ? 1 : -1;
  579. }
  580. else if (!strcmp(buff, "genmove_black")){
  581. player = 1;
  582. }
  583. else {
  584. player = -1;
  585. }
  586. int index = generate_move(net, player, board, multi, .4f, 1.f, two, 0);
  587. if (passed || index < 0){
  588. printf("=%s pass\n\n", ids);
  589. passed = 0;
  590. }
  591. else {
  592. int row = index / 19;
  593. int col = index % 19;
  594. char *swap = two;
  595. two = one;
  596. one = swap;
  597. move_go(board, player, row, col);
  598. board_to_string(one, board);
  599. row = 19 - row;
  600. if (col >= 8) ++col;
  601. printf("=%s %c%d\n\n", ids, 'A' + col, row);
  602. //print_board(board, 1, 0);
  603. }
  604. }
  605. else if (!strcmp(buff, "p")){
  606. //print_board(board, 1, 0);
  607. }
  608. else if (!strcmp(buff, "final_status_list")){
  609. char type[256];
  610. scanf("%s", type);
  611. fprintf(stderr, "final_status\n");
  612. char *line = fgetl(stdin);
  613. free(line);
  614. if (type[0] == 'd' || type[0] == 'D'){
  615. int i;
  616. FILE *f = fopen("game.txt", "w");
  617. int count = print_game(board, f);
  618. fprintf(f, "%s final_status_list dead\n", ids);
  619. fclose(f);
  620. FILE *p = _popen("./gnugo --mode gtp < game.txt", "r");
  621. for (i = 0; i < count; ++i){
  622. free(fgetl(p));
  623. free(fgetl(p));
  624. }
  625. char *l = 0;
  626. while ((l = fgetl(p))){
  627. printf("%s\n", l);
  628. free(l);
  629. }
  630. }
  631. else {
  632. printf("?%s unknown command\n\n", ids);
  633. }
  634. }
  635. else {
  636. char *line = fgetl(stdin);
  637. free(line);
  638. printf("?%s unknown command\n\n", ids);
  639. }
  640. fflush(stdout);
  641. fflush(stderr);
  642. }
  643. }
  644. void test_go(char *cfg, char *weights, int multi)
  645. {
  646. network net = parse_network_cfg(cfg);
  647. if (weights){
  648. load_weights(&net, weights);
  649. }
  650. srand((unsigned int)time(0));
  651. set_batch_network(&net, 1);
  652. float *board = (float*)calloc(19 * 19, sizeof(float));
  653. float *move = (float*)calloc(19 * 19 + 1, sizeof(float));
  654. int color = 1;
  655. while (1){
  656. int i;
  657. predict_move(net, board, move, multi);
  658. int indexes[nind];
  659. int row, col;
  660. top_k(move, 19 * 19 + 1, nind, indexes);
  661. print_board(stderr, board, color, indexes);
  662. for (i = 0; i < nind; ++i){
  663. int index = indexes[i];
  664. row = index / 19;
  665. col = index % 19;
  666. if (row == 19){
  667. printf("%d: Pass, %.2f%%\n", i + 1, move[index] * 100);
  668. }
  669. else {
  670. printf("%d: %c %d, %.2f%%\n", i + 1, col + 'A' + 1 * (col > 7 && noi), (inverted) ? 19 - row : row + 1, move[index] * 100);
  671. }
  672. }
  673. //if(color == 1) printf("\u25EF Enter move: ");
  674. //else printf("\u25C9 Enter move: ");
  675. if (color == 1) printf("X Enter move: ");
  676. else printf("O Enter move: ");
  677. char c;
  678. char *line = fgetl(stdin);
  679. int picked = 1;
  680. int dnum = sscanf(line, "%d", &picked);
  681. int cnum = sscanf(line, "%c", &c);
  682. if (strlen(line) == 0 || dnum) {
  683. --picked;
  684. if (picked < nind){
  685. int index = indexes[picked];
  686. row = index / 19;
  687. col = index % 19;
  688. if (row < 19){
  689. move_go(board, 1, row, col);
  690. }
  691. }
  692. }
  693. else if (cnum){
  694. if (c <= 'T' && c >= 'A'){
  695. int num = sscanf(line, "%c %d", &c, &row);
  696. row = (inverted) ? 19 - row : row - 1;
  697. col = c - 'A';
  698. if (col > 7 && noi) col -= 1;
  699. if (num == 2) move_go(board, 1, row, col);
  700. }
  701. else if (c == 'p') {
  702. // Pass
  703. }
  704. else if (c == 'b' || c == 'w'){
  705. char g;
  706. int num = sscanf(line, "%c %c %d", &g, &c, &row);
  707. row = (inverted) ? 19 - row : row - 1;
  708. col = c - 'A';
  709. if (col > 7 && noi) col -= 1;
  710. if (num == 3) board[row * 19 + col] = (float)((g == 'b') ? color : -color);
  711. }
  712. else if (c == 'c'){
  713. char g;
  714. int num = sscanf(line, "%c %c %d", &g, &c, &row);
  715. row = (inverted) ? 19 - row : row - 1;
  716. col = c - 'A';
  717. if (col > 7 && noi) col -= 1;
  718. if (num == 3) board[row * 19 + col] = 0;
  719. }
  720. }
  721. free(line);
  722. flip_board(board);
  723. color = -color;
  724. }
  725. }
  726. float score_game(float *board)
  727. {
  728. int i;
  729. FILE *f = fopen("game.txt", "w");
  730. int count = print_game(board, f);
  731. fprintf(f, "final_score\n");
  732. fclose(f);
  733. FILE *p = _popen("./gnugo --mode gtp < game.txt", "r");
  734. for (i = 0; i < count; ++i){
  735. free(fgetl(p));
  736. free(fgetl(p));
  737. }
  738. char *l = 0;
  739. float score = 0;
  740. char player = 0;
  741. while ((l = fgetl(p))){
  742. fprintf(stderr, "%s \t", l);
  743. int n = sscanf(l, "= %c+%f", &player, &score);
  744. free(l);
  745. if (n == 2) break;
  746. }
  747. if (player == 'W') score = -score;
  748. _pclose(p);
  749. return score;
  750. }
  751. void self_go(char *filename, char *weightfile, char *f2, char *w2, int multi)
  752. {
  753. network net = parse_network_cfg(filename);
  754. if (weightfile){
  755. load_weights(&net, weightfile);
  756. }
  757. network net2 = net;
  758. if (f2){
  759. net2 = parse_network_cfg(f2);
  760. if (w2){
  761. load_weights(&net2, w2);
  762. }
  763. }
  764. srand((unsigned int)time(0));
  765. char boards[600][93];
  766. int count = 0;
  767. set_batch_network(&net, 1);
  768. set_batch_network(&net2, 1);
  769. float *board = (float*)calloc(19 * 19, sizeof(float));
  770. char *one = (char*)calloc(91, sizeof(char));
  771. char *two = (char*)calloc(91, sizeof(char));
  772. int done = 0;
  773. int player = 1;
  774. int p1 = 0;
  775. int p2 = 0;
  776. int total = 0;
  777. while (1){
  778. if (done){
  779. float score = score_game(board);
  780. if ((score > 0) == (total % 2 == 0)) ++p1;
  781. else ++p2;
  782. ++total;
  783. fprintf(stderr, "Total: %d, Player 1: %f, Player 2: %f\n", total, (float)p1 / total, (float)p2 / total);
  784. #ifdef __linux__
  785. sleep(1);
  786. #else
  787. Sleep(1000);
  788. #endif
  789. /*
  790. int i = (score > 0)? 0 : 1;
  791. int j;
  792. for(; i < count; i += 2){
  793. for(j = 0; j < 93; ++j){
  794. printf("%c", boards[i][j]);
  795. }
  796. printf("\n");
  797. }
  798. */
  799. memset(board, 0, 19 * 19 * sizeof(float));
  800. player = 1;
  801. done = 0;
  802. count = 0;
  803. fflush(stdout);
  804. fflush(stderr);
  805. }
  806. print_board(stderr, board, 1, 0);
  807. //sleep(1);
  808. network use = ((total % 2 == 0) == (player == 1)) ? net : net2;
  809. int index = generate_move(use, player, board, multi, .4f, 1.f, two, 0);
  810. if (index < 0){
  811. done = 1;
  812. continue;
  813. }
  814. int row = index / 19;
  815. int col = index % 19;
  816. char *swap = two;
  817. two = one;
  818. one = swap;
  819. if (player < 0) flip_board(board);
  820. boards[count][0] = row;
  821. boards[count][1] = col;
  822. board_to_string(boards[count] + 2, board);
  823. if (player < 0) flip_board(board);
  824. ++count;
  825. move_go(board, player, row, col);
  826. board_to_string(one, board);
  827. player = -player;
  828. }
  829. }
  830. void run_go(int argc, char **argv)
  831. {
  832. //boards_go();
  833. if (argc < 4){
  834. fprintf(stderr, "usage: %s %s [train/test/valid] [cfg] [weights (optional)]\n", argv[0], argv[1]);
  835. return;
  836. }
  837. char *gpu_list = find_char_arg(argc, argv, "-gpus", 0);
  838. int *gpus = 0;
  839. int gpu = 0;
  840. int ngpus = 0;
  841. if (gpu_list){
  842. printf("%s\n", gpu_list);
  843. int len = strlen(gpu_list);
  844. ngpus = 1;
  845. int i;
  846. for (i = 0; i < len; ++i){
  847. if (gpu_list[i] == ',') ++ngpus;
  848. }
  849. gpus = (int*)calloc(ngpus, sizeof(int));
  850. for (i = 0; i < ngpus; ++i){
  851. gpus[i] = atoi(gpu_list);
  852. gpu_list = strchr(gpu_list, ',') + 1;
  853. }
  854. }
  855. else {
  856. gpu = gpu_index;
  857. gpus = &gpu;
  858. ngpus = 1;
  859. }
  860. int clear = find_arg(argc, argv, "-clear");
  861. char *cfg = argv[3];
  862. char *weights = (argc > 4) ? argv[4] : 0;
  863. char *c2 = (argc > 5) ? argv[5] : 0;
  864. char *w2 = (argc > 6) ? argv[6] : 0;
  865. int multi = find_arg(argc, argv, "-multi");
  866. if (0 == strcmp(argv[2], "train")) train_go(cfg, weights, c2, gpus, ngpus, clear);
  867. else if (0 == strcmp(argv[2], "valid")) valid_go(cfg, weights, multi, c2);
  868. else if (0 == strcmp(argv[2], "self")) self_go(cfg, weights, c2, w2, multi);
  869. else if (0 == strcmp(argv[2], "test")) test_go(cfg, weights, multi);
  870. else if (0 == strcmp(argv[2], "engine")) engine_go(cfg, weights, multi);
  871. }
  872. };