-
Notifications
You must be signed in to change notification settings - Fork 3
/
Source.cpp
1055 lines (844 loc) · 35.4 KB
/
Source.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
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "Header.hpp"
//#include "emilib_hashmap.hpp"
std::vector<std::string> split(const std::string &s, char delim) {
std::vector<std::string> answer;
std::string item;
for (char ch : s) {
if (ch == delim) {
if (!item.empty())
answer.push_back(item);
item.clear();
}
else {
item += ch;
}
}
if (!item.empty()) answer.push_back(item);
return answer;
}
int32_t ComputeFinalScore(const uint64_t player, const uint64_t opponent) {
//引数の盤面が即詰みだと仮定し、最終スコアを返す。
const int32_t n_discs_p = _mm_popcnt_u64(player);
const int32_t n_discs_o = _mm_popcnt_u64(opponent);
int32_t score = n_discs_p - n_discs_o;
//空白マスが残っている状態で両者とも打つ場所が無い場合は試合終了だが、
//そのとき引き分けでないならば、空白マスは勝者のポイントに加算されるというルールがある。
if (score < 0) score -= 64 - n_discs_p - n_discs_o;
else if (score > 0) score += 64 - n_discs_p - n_discs_o;
return score;
}
enum {
UPPER_MODE = 0,
LOWER_MODE = 1
};
enum {
FIRSTLEVEL = 2,
SECONDLEVEL = 4,
THIRDLEVEL = 8,
};
const int START_EMPTY_NUM = 50;
const int END_EMPTY_NUM = 36;
struct Bound {
int32_t lowerbound;
int32_t upperbound;
};
namespace std {
template <>
struct hash<std::array<uint64_t, 2>> {
size_t operator()(const std::array<uint64_t, 2> &x) const {
return get_hash_code(x[0], x[1]);
}
};
}
struct HashEntry {
std::array<uint64_t, 2>board;
Bound bound;
};
struct HashColumn {
std::array<HashEntry, 3>c;
};
class MyHashTable {
private:
std::vector<HashColumn>data;
uint64_t log2len, bitmask;
void init_data() {
for (size_t i = 0; i < data.size(); ++i) {
for (int j = 0; j < 3; ++j) {
data[i].c[j].board[0] = 0;
data[i].c[j].board[1] = 0;
data[i].c[j].bound.lowerbound = -64;
data[i].c[j].bound.upperbound = 64;
}
}
}
public:
MyHashTable(const int n) {
log2len = n;
bitmask = (1ULL << n) - 1ULL;
data.resize(1ULL << n);
init_data();
}
MyHashTable() : MyHashTable(10) {}
bool find(const std::array<uint64_t, 2>&board, Bound &dest) {
assert(board == board_unique(board));
const uint64_t hash_value = get_hash_code(board[0], board[1]);
const uint64_t hash_index = hash_value & bitmask;
if (data[hash_index].c[0].board == board) {
dest = data[hash_index].c[0].bound;
return true;
}
if (data[hash_index].c[0].board == std::array<uint64_t, 2>{0, 0})return false;
if (data[hash_index].c[1].board == board) {
dest = data[hash_index].c[1].bound;
return true;
}
if (data[hash_index].c[1].board == std::array<uint64_t, 2>{0, 0})return false;
if (data[hash_index].c[2].board == board) {
dest = data[hash_index].c[2].bound;
std::swap(data[hash_index].c[1], data[hash_index].c[2]);
return true;
}
return false;
}
void insert(const std::array<uint64_t, 2>&board, const Bound &bound) {
assert(board == board_unique(board));
const uint64_t hash_value = get_hash_code(board[0], board[1]);
const uint64_t hash_index = hash_value & bitmask;
const auto func_insert = [&](const int n) {
data[hash_index].c[n].board = board;
data[hash_index].c[n].bound = bound;
};
if (data[hash_index].c[0].board == std::array<uint64_t, 2>{0, 0} || data[hash_index].c[0].board == board) {
func_insert(0);
return;
}
const int64_t self_n_occupied = _mm_popcnt_u64(board[0] | board[1]);
const int64_t hash_n_occupied = _mm_popcnt_u64(data[hash_index].c[0].board[0] | data[hash_index].c[0].board[1]);
if (self_n_occupied < hash_n_occupied) {
data[hash_index].c[2] = data[hash_index].c[1];
data[hash_index].c[1] = data[hash_index].c[0];
func_insert(0);
return;
}
else if (self_n_occupied == hash_n_occupied) {
if (data[hash_index].c[1].board == std::array<uint64_t, 2>{0, 0} || data[hash_index].c[1].board == board) {
data[hash_index].c[1] = data[hash_index].c[0];
func_insert(0);
return;
}
else {
data[hash_index].c[2] = data[hash_index].c[1];
data[hash_index].c[1] = data[hash_index].c[0];
func_insert(0);
return;
}
}
else {
if (data[hash_index].c[1].board == std::array<uint64_t, 2>{0, 0} || data[hash_index].c[1].board == board) {
func_insert(1);
return;
}
else {
data[hash_index].c[2] = data[hash_index].c[1];
func_insert(1);
return;
}
}
}
};
typedef std::unordered_map<std::array<uint64_t, 2>, Bound> StaticHashMap;
typedef std::unordered_map<std::array<uint64_t, 2>, Bound> HashMap;
//typedef emilib::HashMap<std::array<uint64_t, 2>, Bound> HashTable;
std::array<MyHashTable, 2>thirdlevel_table;
MyHashTable secondlevel_table;
HashMap firstlevel_table;
StaticHashMap exact_knowledge;
std::unordered_map<std::array<uint64_t, 2>, int32_t> kifu_freq_data;
std::unordered_map<std::array<uint64_t, 2>, int32_t> override_evaluation_table;
int32_t output_max_abs = 64;
std::vector<std::array<uint64_t, 2>>TO_SEARCH_ORDER;
std::unordered_map<std::array<uint64_t, 2>, std::array<int32_t, 3>>TO_SEARCH;
int64_t difference_order_count = 0;
constexpr int BUFSIZE = 2 * 1024 * 1024;
void output_TO_SEARCH_csv(const std::string &filename) {
std::cout << "start: output_TO_SEARCH_csv" << std::endl;
const auto start = std::chrono::system_clock::now(); // 計測開始時間
static char buf[BUFSIZE];//大きいのでスタック領域に置きたくないからstatic。(べつにmallocでもstd::vectorでもいいんだけど)
std::ofstream writing_file;
writing_file.rdbuf()->pubsetbuf(buf, BUFSIZE);
writing_file.open(filename, std::ios::out);
writing_file << "obf,alpha,beta,estimated_score" << std::endl;
assert(TO_SEARCH_ORDER.size() == TO_SEARCH.size());
for (size_t i = 0; i < TO_SEARCH_ORDER.size(); ++i) {
const std::array<uint64_t, 2>BB = board_unique(TO_SEARCH_ORDER[i][0], TO_SEARCH_ORDER[i][1]);
assert(BB == TO_SEARCH_ORDER[i]);
const std::string obf = bitboard2obf(BB[0], BB[1]);
assert(-64 <= TO_SEARCH[BB][0] && TO_SEARCH[BB][0] < TO_SEARCH[BB][1] && TO_SEARCH[BB][1] <= 64);
writing_file << obf << "," << TO_SEARCH[BB][0] << "," << TO_SEARCH[BB][1] << "," << TO_SEARCH[BB][2] << std::endl;
}
writing_file.close();
const auto end = std::chrono::system_clock::now(); // 計測終了時間
const int64_t elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count(); //処理に要した時間をミリ秒に変換
std::cout << "finish: output_TO_SEARCH_csv: elapsed time = " << elapsed << " ms" << std::endl;
std::cout << "info: difference_order_count = " << difference_order_count << std::endl;
}
void output_RESULT_csv(const std::string &obf, bool is_exact, const int32_t alpha, const int32_t beta, const int32_t score) {
std::cout << "start: output_RESULT_csv" << std::endl;
static char buf[BUFSIZE];//大きいのでスタック領域に置きたくないからstatic。(べつにmallocでもstd::vectorでもいいんだけど)
std::ofstream writing_file;
writing_file.rdbuf()->pubsetbuf(buf, BUFSIZE);
writing_file.open(std::string("result_e50")+obf.substr(0, 64)+".csv", std::ios::out);
int32_t score_lowerbound = 100, score_upperbound = 100;
if (alpha < score && score < beta) {
score_lowerbound = score;
score_upperbound = score;
}
else if (score <= alpha) {
score_lowerbound = -64;
score_upperbound = score;
}
else if (beta <= score) {
score_lowerbound = score;
score_upperbound = 64;
}
assert(-64 <= score_lowerbound && score_lowerbound <= score_upperbound && score_upperbound <= 64);
writing_file << "obf,is_exact,score_lowerbound,score_upperbound" << std::endl;
writing_file << obf << "," << (is_exact ? 1 : 0) << "," << score_lowerbound << "," << score_upperbound << std::endl;
writing_file.close();
std::cout << "finish: output_RESULT_csv" << std::endl;
}
//firstlevelの仕事は、END_EMPTY_NUMまで"最適なルート"で読んで、全ての未決定な末端ノードを出力すること。
//ここでいう"最適なルート"とは、
//(1)ある合法手で進んだ先をthirdlevelで読んで、exact_scoreが確定値であるか、fail-high/lowすることが確実ならば、読まずに引き返すこと。
//(2)さもなくば、各合法手で進んだ先をsecondlevelで読んで、スコアが高い順に読むこと。
//とする。
//secondlevelの仕事は、END_EMPTY_NUMまで読んで、"真の値"を返すこと。
//ここでいう"真の値"とは、
//(1)exact_knowledgeが無いなら静的評価関数の値のこと
//(2)exact_knowledgeが正確な値であるならその値のこと
//(3)exact_knowledgeに幅があり静的評価関数の値がその範囲内ならば、静的評価関数の値のこと
//(4)exact_knowledgeに幅があり静的評価関数の値がその範囲外ならば、exact_knowledgeの範囲のうち静的評価関数の値に最も近い値のこと
//とする。
//上記の「静的評価関数の値」は、局面を引数に取り整数を返すedaxの評価関数を基本とするが、override_evaluation_tableに載っている局面についてはそれの値を優先する。
//thirdlevelの仕事は、END_EMPTY_NUMまで読んで、"真の値の区間"の上端か下端を返すこと。
//ここでいう"真の値の区間"とは、
//(1)exact_knowledgeが無いなら[-64,64]のこと
//(2)exact_knowledgeがあるならその区間のこと
//とする。
//modeは0か1で、0が上端、1が下端とする。
int32_t static_evaluation_search(const uint64_t player, const uint64_t opponent, int32_t alpha, const int32_t beta, const int32_t depth) {
if (depth <= 0) {
int32_t score = EvaluatePosition0(player, opponent);
score = (score / 2) * 2;
return score;
}
const int32_t old_alpha = alpha;
const int32_t n_empties = _mm_popcnt_u64(~(player | opponent));
uint64_t bb_moves = get_moves(player, opponent);
if (bb_moves == 0) {
if (get_moves(opponent, player) != 0) { // pass
return -static_evaluation_search(opponent, player, -beta, -alpha, depth);
}
else { // game over
return ComputeFinalScore(player, opponent);
}
}
int32_t bestscore = std::numeric_limits<int32_t>::min();
if (depth == 1) {
for (uint32_t index = 0; bitscan_forward64(bb_moves, &index); bb_moves &= bb_moves - 1) {
uint64_t flipped = flip(index, player, opponent);
if (flipped == opponent)return 64; // wipeout
const uint64_t next_player = opponent ^ flipped;
const uint64_t next_opponent = player ^ (flipped | (1ULL << index));
int32_t s = -EvaluatePosition0(next_player, next_opponent);
s = (s / 2) * 2;
if (bestscore < s)bestscore = s;
if (beta <= s)return bestscore;
}
return bestscore;
}
uint64_t flipped[MAX_MOVE] = {};
int64_t moves[MAX_MOVE] = {}, move_values[MAX_MOVE] = {}, movenum = 0;
for (uint32_t index = 0; bitscan_forward64(bb_moves, &index); bb_moves &= bb_moves - 1) {
moves[movenum] = index;
flipped[movenum] = flip(index, player, opponent);
if (flipped[movenum] == opponent)return 64; // wipeout
const uint64_t next_player = opponent ^ flipped[movenum];
const uint64_t next_opponent = player ^ (flipped[movenum] | (1ULL << index));
move_values[movenum++] = integrated_move_scoring(next_player, next_opponent, index);
}
//sort moves by move_values in descending order.
for (int i = 0; i < movenum - 1; ++i) {
int32_t max_value_index = i;
for (int j = i + 1; j < movenum; ++j)if (move_values[max_value_index] < move_values[j])max_value_index = j;
if (i != max_value_index) {
std::swap(flipped[i], flipped[max_value_index]);
std::swap(moves[i], moves[max_value_index]);
std::swap(move_values[i], move_values[max_value_index]);
}
}
for (int i = 0; i < movenum - 1; ++i) {
assert(move_values[i] >= move_values[i + 1]);
}
int32_t move_index = 0;
//first move in PV node
if (alpha + 1 < beta) {
const uint64_t next_player = opponent ^ flipped[move_index];
const uint64_t next_opponent = player ^ (flipped[move_index] | (1ULL << moves[move_index]));
const int32_t s = -static_evaluation_search(next_player, next_opponent, -beta, -alpha, depth - 1);
if (bestscore < s)bestscore = s;
if (beta <= s)return bestscore;
if (alpha < s)alpha = s;
move_index = 1;
}
//all moves in Null-Window node, or second and subsequent moves in PV node
for (; move_index < movenum; ++move_index) {
const uint64_t next_player = opponent ^ flipped[move_index];
const uint64_t next_opponent = player ^ (flipped[move_index] | (1ULL << moves[move_index]));
const int32_t s1 = -static_evaluation_search(next_player, next_opponent, -(alpha + 1), -alpha, depth - 1);
if (bestscore < s1)bestscore = s1;
if (beta <= bestscore)return bestscore;
if (alpha < s1) {
alpha = s1;
const int32_t s2 = -static_evaluation_search(next_player, next_opponent, -beta, -alpha, depth - 1);
if (bestscore < s2)bestscore = s2;
if (beta <= bestscore)return bestscore;
if (alpha < s2)alpha = s2;
}
}
assert(-64 <= bestscore && bestscore <= 64);
return bestscore;
}
constexpr int32_t STATIC_EVALUATION_FUNCTION_SEARCH_THRESHOLD = 11;
int32_t static_evaluation_function(const std::array<uint64_t, 2> &BB) {
if (override_evaluation_table.find(BB) != override_evaluation_table.end()) {
const int32_t x = override_evaluation_table[BB];
return (x / 2) * 2;
}
int32_t score = EvaluatePosition0(BB[0], BB[1]);
score = (score / 2) * 2;
if (score < -STATIC_EVALUATION_FUNCTION_SEARCH_THRESHOLD || STATIC_EVALUATION_FUNCTION_SEARCH_THRESHOLD < score)return score;
const int32_t alpha = std::min(-3, score - 1);
const int32_t beta = std::max(3, score + 1);
score = static_evaluation_search(BB[0], BB[1], alpha, beta, 2);
score = (score / 2) * 2;
return score;
}
int32_t static_evaluation_function_without_override(const std::array<uint64_t, 2> &BB) {
int32_t score = EvaluatePosition0(BB[0], BB[1]);
score = (score / 2) * 2;
if (score < -STATIC_EVALUATION_FUNCTION_SEARCH_THRESHOLD || STATIC_EVALUATION_FUNCTION_SEARCH_THRESHOLD < score)return score;
const int32_t alpha = std::min(-3, score - 1);
const int32_t beta = std::max(3, score + 1);
score = static_evaluation_search(BB[0], BB[1], alpha, beta, 2);
score = (score / 2) * 2;
return score;
}
template<int32_t SEARCH_LEVEL>int32_t PVS_midgame(const uint64_t player, const uint64_t opponent, int32_t alpha, const int32_t beta) {
assert(SEARCH_LEVEL == FIRSTLEVEL ||
SEARCH_LEVEL == SECONDLEVEL ||
SEARCH_LEVEL == THIRDLEVEL + UPPER_MODE ||
SEARCH_LEVEL == THIRDLEVEL + LOWER_MODE);
const int32_t n_empties = _mm_popcnt_u64(~(player | opponent));
const std::array<uint64_t, 2>BB = board_unique(player, opponent);
Bound old_bound;
old_bound.lowerbound = -64;
old_bound.upperbound = 64;
if (SEARCH_LEVEL == FIRSTLEVEL) {
if (firstlevel_table.find(BB) != firstlevel_table.end()) {
old_bound = firstlevel_table[BB];
}
}
else if (SEARCH_LEVEL == SECONDLEVEL) {
secondlevel_table.find(BB, old_bound);
}
else if (SEARCH_LEVEL & THIRDLEVEL) {
thirdlevel_table[SEARCH_LEVEL % 2].find(BB, old_bound);
}
if (old_bound.lowerbound == old_bound.upperbound)return old_bound.lowerbound;
if (old_bound.upperbound <= alpha)return old_bound.upperbound;
if (beta <= old_bound.lowerbound)return old_bound.lowerbound;
if (SEARCH_LEVEL == FIRSTLEVEL) {
if (output_max_abs < 64) {
const auto s = PVS_midgame<SECONDLEVEL>(player, opponent, -64, 64);
if (s < -output_max_abs || output_max_abs < s) {
return s;
}
}
}
uint64_t bb_moves = get_moves(player, opponent);
const int32_t old_alpha = alpha;
int32_t bestscore = std::numeric_limits<int32_t>::min();
if (n_empties == END_EMPTY_NUM) {
if (bb_moves == 0) {
if (get_moves(opponent, player) != 0) { // pass
return -PVS_midgame<SEARCH_LEVEL ^ (SEARCH_LEVEL / 8)>(opponent, player, -beta, -alpha);
}
else { // game over
return ComputeFinalScore(player, opponent);
}
}
else {
//1手詰め局面なら+64を返す
for (uint32_t index = 0; bitscan_forward64(bb_moves, &index); bb_moves &= bb_moves - 1) {
if (flip(index, player, opponent) == opponent)return 64;
}
}
if (SEARCH_LEVEL == FIRSTLEVEL) {
int32_t score = PVS_midgame<SECONDLEVEL>(player, opponent, -64, 64);
score = (score / 2) * 2;
const std::array<int32_t, 3>ABS = { alpha,beta,score };
if (TO_SEARCH.find(BB) == TO_SEARCH.end()) {
TO_SEARCH[BB] = ABS;
TO_SEARCH_ORDER.push_back(BB);
if ((TO_SEARCH.size() % 1000 == 0) || (TO_SEARCH.size() < 1000 && _mm_popcnt_u64(TO_SEARCH.size()) == 1)) {
std::cout << TO_SEARCH.size() << std::endl;
}
}
else {
TO_SEARCH[BB][0] = std::min(TO_SEARCH[BB][0], alpha);
TO_SEARCH[BB][1] = std::max(TO_SEARCH[BB][1], beta);
assert(TO_SEARCH[BB][2] == score);
++difference_order_count;
}
return score;
}
else if (SEARCH_LEVEL == SECONDLEVEL) {
if (exact_knowledge.find(BB) != exact_knowledge.end()) {
Bound k;
k = exact_knowledge[BB];
assert((k.lowerbound / 2) * 2 == k.lowerbound);
assert((k.upperbound / 2) * 2 == k.upperbound);
if (k.lowerbound == k.upperbound)return k.lowerbound;
if (beta <= k.lowerbound)return k.lowerbound;
if (k.upperbound <= alpha)return k.upperbound;
int32_t score = static_evaluation_function(BB);
score = (score / 2) * 2;
if (k.lowerbound <= score && score <= k.upperbound)return score;
if (score < k.lowerbound)return k.lowerbound;
if (k.upperbound < score)return k.upperbound;
assert(false);
}
else {
int32_t score = static_evaluation_function(BB);
score = (score / 2) * 2;
return score;
}
}
else if (SEARCH_LEVEL & THIRDLEVEL) {
if (exact_knowledge.find(BB) != exact_knowledge.end()) {
Bound k;
k = exact_knowledge[BB];
return (SEARCH_LEVEL == THIRDLEVEL + UPPER_MODE) ? k.upperbound : k.lowerbound;
}
else {
return (SEARCH_LEVEL == THIRDLEVEL + UPPER_MODE) ? 64 : -64;
}
}
assert(false);
return 0;
}
if (bb_moves == 0) { // special cases
if (get_moves(opponent, player) != 0) { // pass
bestscore = -PVS_midgame<SEARCH_LEVEL ^ (SEARCH_LEVEL / 8)>(opponent, player, -beta, -alpha);
}
else { // game over
return ComputeFinalScore(player, opponent);
}
}
else {
uint64_t flipped[MAX_MOVE] = {};
int64_t moves[MAX_MOVE] = {}, move_values[MAX_MOVE] = {}, movenum = 0;
for (uint32_t index = 0; bitscan_forward64(bb_moves, &index); bb_moves &= bb_moves - 1) {
moves[movenum] = index;
flipped[movenum] = flip(index, player, opponent);
if (flipped[movenum] == opponent)return 64; // wipeout
const uint64_t next_player = opponent ^ flipped[movenum];
const uint64_t next_opponent = player ^ (flipped[movenum] | (1ULL << index));
if (SEARCH_LEVEL == FIRSTLEVEL) {
Bound b;
b.lowerbound = -PVS_midgame<THIRDLEVEL + UPPER_MODE>(next_player, next_opponent, -64, 64);
b.upperbound = -PVS_midgame<THIRDLEVEL + LOWER_MODE>(next_player, next_opponent, -64, 64);
assert(-64 <= b.lowerbound && b.lowerbound <= b.upperbound && b.upperbound <= 64);
if (beta <= b.lowerbound)return b.lowerbound;
if (b.upperbound <= alpha) {
bestscore = std::max(bestscore, b.upperbound);
continue;
}
if (b.lowerbound == b.upperbound && alpha < b.lowerbound && b.upperbound < beta) {
bestscore = std::max(bestscore, b.upperbound);
alpha = b.upperbound;
continue;
}
const int64_t score = PVS_midgame<SECONDLEVEL>(next_player, next_opponent, -64, 64);
move_values[movenum] = -score * (1LL << 50);
const std::array<uint64_t, 2>NEXT_BB = board_unique(next_player, next_opponent);
if (kifu_freq_data.find(NEXT_BB) != kifu_freq_data.end())move_values[movenum] += int64_t(kifu_freq_data[BB]) * (1LL << 33);
move_values[movenum] += integrated_move_scoring(next_player, next_opponent, index);
++movenum;
}
else move_values[movenum++] = integrated_move_scoring(next_player, next_opponent, index);
}
if (movenum == 0) {//THIRD_LEVEL-search confirmed that all legal moves cause fail-low.
assert(SEARCH_LEVEL == FIRSTLEVEL);
assert(-64 <= bestscore && bestscore < beta);
return bestscore;
}
//sort moves by move_values in descending order.
for (int i = 0; i < movenum - 1; ++i) {
int32_t max_value_index = i;
for (int j = i + 1; j < movenum; ++j)if (move_values[max_value_index] < move_values[j])max_value_index = j;
if (i != max_value_index) {
std::swap(flipped[i], flipped[max_value_index]);
std::swap(moves[i], moves[max_value_index]);
std::swap(move_values[i], move_values[max_value_index]);
}
}
for (int i = 0; i < movenum - 1; ++i) {
assert(move_values[i] >= move_values[i + 1]);
}
int32_t move_index = 0;
//first move in PV node
if (alpha + 1 < beta) {
const uint64_t next_player = opponent ^ flipped[move_index];
const uint64_t next_opponent = player ^ (flipped[move_index] | (1ULL << moves[move_index]));
const int32_t s = -PVS_midgame<SEARCH_LEVEL ^ (SEARCH_LEVEL / 8)>(next_player, next_opponent, -beta, -alpha);
if (bestscore < s)bestscore = s;
if (beta <= s)goto END_PHASE;
if (alpha < s)alpha = s;
move_index = 1;
}
//all moves in Null-Window node, or second and subsequent moves in PV node
for (; move_index < movenum; ++move_index) {
if (move_values[move_index] == std::numeric_limits<int64_t>::min()) {
assert(SEARCH_LEVEL == FIRSTLEVEL);
assert(move_index);
break;
}
const uint64_t next_player = opponent ^ flipped[move_index];
const uint64_t next_opponent = player ^ (flipped[move_index] | (1ULL << moves[move_index]));
const int32_t s1 = -PVS_midgame<SEARCH_LEVEL ^ (SEARCH_LEVEL / 8)>(next_player, next_opponent, -(alpha + 1), -alpha);
if (bestscore < s1)bestscore = s1;
if (beta <= bestscore) break;
if (alpha < s1) {
alpha = s1;
const int32_t s2 = -PVS_midgame<SEARCH_LEVEL ^ (SEARCH_LEVEL / 8)>(next_player, next_opponent, -beta, -alpha);
if (bestscore < s2)bestscore = s2;
if (beta <= bestscore) break;
if (alpha < s2)alpha = s2;
}
}
}
END_PHASE:;
if (old_alpha < bestscore && bestscore < beta) {
Bound b;
b.lowerbound = b.upperbound = bestscore;
if (SEARCH_LEVEL == FIRSTLEVEL)firstlevel_table[BB] = b;
if (SEARCH_LEVEL == SECONDLEVEL)secondlevel_table.insert(BB, b);
if (SEARCH_LEVEL & THIRDLEVEL)thirdlevel_table[SEARCH_LEVEL % 2].insert(BB, b);
}
else if (bestscore <= old_alpha) {
old_bound.upperbound = std::min(bestscore, old_bound.upperbound);
assert(old_bound.lowerbound <= old_bound.upperbound);
if (SEARCH_LEVEL == FIRSTLEVEL)firstlevel_table[BB] = old_bound;
if (SEARCH_LEVEL == SECONDLEVEL)secondlevel_table.insert(BB, old_bound);
if (SEARCH_LEVEL & THIRDLEVEL)thirdlevel_table[SEARCH_LEVEL % 2].insert(BB, old_bound);
}
else if (beta <= bestscore) {
old_bound.lowerbound = std::max(bestscore, old_bound.lowerbound);
assert(old_bound.lowerbound <= old_bound.upperbound);
if (SEARCH_LEVEL == FIRSTLEVEL)firstlevel_table[BB] = old_bound;
if (SEARCH_LEVEL == SECONDLEVEL)secondlevel_table.insert(BB, old_bound);
if (SEARCH_LEVEL & THIRDLEVEL)thirdlevel_table[SEARCH_LEVEL % 2].insert(BB, old_bound);
}
else {
assert(false);
}
assert(-64 <= bestscore && bestscore <= 64);
return bestscore;
}
void load_kifu_freq() {
constexpr int THRESHOLD = 10;
kifu_freq_data.clear();
std::ifstream fin;
fin.open("opening_book_freq.csv");
if (!fin) {
std::cout << "warning: we could not read opening_book_freq.csv" << std::endl;
return;
}
std::cout << "start: load_kifu_freq" << std::endl;
std::string line;
for (int i = 0; std::getline(fin, line); ++i) {
std::vector<std::string> x = split(line, ',');
if (x.size() != 3)continue;
if (IsObfFormat(x[2]) == false)continue;
try {
const int32_t freq = std::stoi(x[0]);
const int32_t n_empties = std::stoi(x[1]);
const std::array<uint64_t, 2>BB = board_unique(x[2]);
if (n_empties != _mm_popcnt_u64(~(BB[0] | BB[1]))) {
std::cout << "warning: failed to validate a line: " << line << std::endl;
continue;
}
if (END_EMPTY_NUM <= n_empties && n_empties < START_EMPTY_NUM && freq >= THRESHOLD) {
assert(kifu_freq_data.find(BB) == kifu_freq_data.end());
kifu_freq_data[BB] = freq;
}
}
catch (...) {
std::cout << "warning: failed to read a knowledge line: " << line << std::endl;
continue;
}
}
fin.close();
std::cout << "finish: load_kifu_freq" << std::endl;
}
void load_knowledge(const std::string filename) {
//knowledgeファイルはcsv形式で、(見出し行以外の)各行は
//obf,depth,accuracy,score_lowerbound,score_upperbound,nodes
//になっているとする。
//完全読みの結果が書かれているときはdepth==(空きマス数),accuracy==100である。
//さもなくば完全読みではない。(静的評価関数よりは信頼できると考えられるのでoverride_knowledgeに格納する。)
//複数の行に同じ局面の情報が書いてあることがある。ただし、完全読みの場合はそれらは相互矛盾しておらず、真の値は共通部分にあると仮定して良い。
//完全読みでない行において、score_lowerboundとscore_upperboundの値は必ず等しいと仮定して良い。
//1行目およびそれ以外の行が見出し行("obf,lower_exact,upper_exact"など)になっていることもある。それらは単に無視する。
//要件: 例えば、20手73%[+3,+3]と36手99%[+4,+64]があって、静的評価関数は+18のとき、override_knowledgeには+4が登録されてほしい。
// (静的評価関数よりは20手読みの値のほうが信頼できるので+3を使ってほしいが、99%読みのレンジ内にclampしてほしい)
// これは、knowledgeファイル内で登場する順番にかかわらずそうなってほしい。
assert(override_evaluation_table.size() == 0);
typedef struct {
int64_t strength;
int32_t score;
}V_POINT;
typedef struct {
int64_t strength;
Bound b;
}V_RANGE;
std::map<std::array<uint64_t, 2>, V_POINT>tmp_override_data_point;
std::map<std::array<uint64_t, 2>, V_RANGE>tmp_override_data_range;
const auto func_intersection = [&](const Bound b1, const Bound b2) {
Bound b;
b.lowerbound = std::max(b1.lowerbound, b2.lowerbound);
b.upperbound = std::min(b1.upperbound, b2.upperbound);
assert(-64 <= b.lowerbound && b.lowerbound <= b.upperbound && b.upperbound <= 64);
return b;
};
std::ifstream fin;
fin.open(filename);
if (!fin) {
std::cout << "warning: we could not read " << filename << std::endl;
return;
}
std::string line;
for (int i = 0; std::getline(fin, line); ++i) {
std::vector<std::string> x = split(line, ',');
if (x.size() < 5)continue;//[0]から順に,obf,depth,accuracy,score_lowerbound,score_upperbound と仮定する。
if (IsObfFormat(x[0]) == false)continue;
try {
Bound d;
const auto BB = board_unique(x[0]);
const int64_t n_empties = _mm_popcnt_u64(~(BB[0] | BB[1]));
const int64_t depth = std::stoi(x[1]);
const int64_t accuracy = std::stoi(x[2]);
assert(0 <= depth && depth <= n_empties);
assert(0 <= accuracy && accuracy <= 100);
d.lowerbound = std::stoi(x[3]);
d.upperbound = std::stoi(x[4]);//ちなみに、std::stoiは後ろに数値でない文字列がくっついていると単に無視する。
assert(-64 <= d.lowerbound && d.lowerbound <= d.upperbound && d.upperbound <= 64);
if (depth == n_empties && accuracy == 100) {
assert((d.lowerbound / 2) * 2 == d.lowerbound);
assert((d.upperbound / 2) * 2 == d.upperbound);
if (exact_knowledge.find(BB) == exact_knowledge.end()) {
exact_knowledge[BB] = d;
}
else {
exact_knowledge[BB] = func_intersection(exact_knowledge[BB], d);
}
}
else {
if(d.lowerbound == d.upperbound) {
V_POINT v;
v.strength = depth * 1000 + accuracy;
v.score = (d.lowerbound / 2) * 2;
if (tmp_override_data_point.find(BB) == tmp_override_data_point.end()) {
tmp_override_data_point[BB] = v;
}
else if (tmp_override_data_point[BB].strength < v.strength) {
tmp_override_data_point[BB] = v;
}
}
else {
V_RANGE v;
v.strength = depth * 1000 + accuracy;
v.b = d;
if (tmp_override_data_range.find(BB) == tmp_override_data_range.end()) {
tmp_override_data_range[BB] = v;
}
else if (tmp_override_data_range[BB].strength < v.strength) {
tmp_override_data_range[BB] = v;
}
}
}
}
catch (...) {
std::cout << "warning: failed to read a knowledge line: " << line << std::endl;
continue;
}
}
fin.close();
for (const auto x : tmp_override_data_point) {
if (tmp_override_data_range.find(x.first) == tmp_override_data_range.end()) {
override_evaluation_table[x.first] = x.second.score;
}
else if (tmp_override_data_range[x.first].strength <= x.second.strength) {
override_evaluation_table[x.first] = x.second.score;
}
else {
const Bound b = tmp_override_data_range[x.first].b;
override_evaluation_table[x.first] = std::clamp(x.second.score, b.lowerbound, b.upperbound);
}
}
for (const auto x : tmp_override_data_range) {
if (tmp_override_data_point.find(x.first) == tmp_override_data_point.end()) {
const int32_t eval_score = (static_evaluation_function_without_override(x.first) / 2)* 2;
override_evaluation_table[x.first] = std::clamp(eval_score, x.second.b.lowerbound, x.second.b.upperbound);
}
else {
assert(override_evaluation_table.find(x.first) != override_evaluation_table.end());
}
}
std::cout << "info: number of exact knowledge = " << exact_knowledge.size() << std::endl;
std::cout << "info: number of override knowledge = " << override_evaluation_table.size() << std::endl;
}
void p006_solve_root(const uint64_t player, const uint64_t opponent, int32_t alpha, const int32_t beta) {
difference_order_count = 0;
const int32_t n_empties = _mm_popcnt_u64(~(player | opponent));
assert(n_empties == START_EMPTY_NUM);
const std::array<uint64_t, 2>BB = board_unique(player, opponent);
const std::string obf = bitboard2obf(BB[0], BB[1]);
std::cout << "start(unique):" << obf << std::endl;
const int32_t score = PVS_midgame<FIRSTLEVEL>(player, opponent, alpha, beta);
std::cout << "p006_solve_root (alpha = " << alpha << ", beta = " << beta << ") : score = " << score << std::endl;
if (output_max_abs == 64 && TO_SEARCH.size() == 0) {
std::cout << "solved." << std::endl;
std::cout << "score = " << score << std::endl;
}
output_RESULT_csv(obf, output_max_abs == 64 && TO_SEARCH.size() == 0, alpha, beta, score);
std::cout << "TO_SEARCH.size() = " << TO_SEARCH.size() << std::endl;
output_TO_SEARCH_csv(obf.substr(0, 64) + ".csv");
}
std::map<std::string, std::string>parse_args(int argc, char **argv) {
std::vector<std::string>args;
for (int i = 1; i < argc; ++i) {
args.push_back(argv[i]);
}
std::map<std::string, std::string>input;
input["alpha"] = "-64";
input["beta"] = "64";
input["max"] = "64";
input["eval-file"] = "edax_eval_weight.json";
std::string opcode = "";
for (uint64_t i = 0; i < args.size(); ++i) {
if (opcode == "") {
if (args[i] == "-o" || args[i] == "--obf") {
opcode = "obf";
}
else if (args[i] == "-a" || args[i] == "--alpha") {
opcode = "alpha";
}
else if (args[i] == "-b" || args[i] == "--beta") {
opcode = "beta";
}
else if (args[i] == "-e" || args[i] == "--eval-file") {
opcode = "eval-file";
}
else if (args[i] == "-k" || args[i] == "--knowledge-file") {
opcode = "knowledge-file";
}
else if (args[i] == "-t" || args[i] == "--test") {
input["test"] = "true";
}
else if (args[i] == "-m" || args[i] == "--max") {
opcode = "max";
}
else {
std::cout << "error: command line argument is invalid. error_code = 1 (cf. parse_args function in the source code)" << std::endl;
std::exit(EXIT_FAILURE);
}
}
else if (opcode == "obf") {
if (std::regex_match(args[i], std::regex(R"(^[-OX]{64}\s[XO];$)"))) {
input[opcode] = args[i];
const std::array<uint64_t, 2>BB = board_unique(args[i]);
const int n_empties = _mm_popcnt_u64(~(BB[0] | BB[1]));
if (n_empties != START_EMPTY_NUM) {
std::cout << "error: command line argument is invalid. error_code = 2 (cf. parse_args function of the source code)" << std::endl;
std::exit(EXIT_FAILURE);
}
opcode = "";
}
else {
std::cout << "error: command line argument is invalid. error_code = 3 (cf. parse_args function of the source code)" << std::endl;
std::exit(EXIT_FAILURE);
}
}
else if (opcode == "alpha" || opcode == "beta" || opcode == "max") {
if (std::regex_match(args[i], std::regex(R"(-?[0-9]{1,2})"))) {
input[opcode] = args[i];
const int num = std::stoi(input[opcode]);
if (num < -64 || 64 < num || (opcode == "max" && num < 0)) {
std::cout << "error: command line argument is invalid. error_code = 4 (cf. main function of the source code)" << std::endl;
std::exit(EXIT_FAILURE);
}
opcode = "";
}
else {
std::cout << "error: command line argument is invalid. error_code = 5 (cf. main function of the source code)" << std::endl;
std::exit(EXIT_FAILURE);
}
}
else if (opcode == "eval-file" || opcode == "knowledge-file") {
input[opcode] = args[i];
opcode = "";