-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbonsai.cpp
More file actions
1113 lines (930 loc) · 38 KB
/
Copy pathbonsai.cpp
File metadata and controls
1113 lines (930 loc) · 38 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
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 <stdio.h>
#include <iostream>
#include <math.h>
#include <iomanip>
#include <bit>
#include <fstream>
#include <chrono>
#include <algorithm>
#include <queue>
#include <immintrin.h>
#include <random>
#include "include/chess.hpp"
using namespace chess;
struct Node
{
Move action;
int firstchild; int num_children;
int visits = 0;
float value = 0;
float policy;
};
struct SearchResult {
Move bestMove;
float confidence;
int nps;
// policy target
std::vector<std::pair<Move, int>> policy;
};
Board board;
std::vector<Node> tree;
bool debug = false;
bool datagenActive = false;
const int DEFAULT_HASH_MIB = 128;
int hashMib = DEFAULT_HASH_MIB;
size_t hashLimitNodes = static_cast<size_t>(DEFAULT_HASH_MIB) * 1024 * 1024 / sizeof(Node);
inline size_t computeHashLimitNodes(int mib) {
return static_cast<size_t>(mib) * 1024 * 1024 / sizeof(Node);
}
// Fraction of total nodes pruneTree() retains per call (~half).
const float PRUNE_KEEP_FRACTION = 0.5f;
#include "net/iteration1.hpp"
float sigmoid(float x) {
return 1.0f / (1.0f + std::exp(-x));
}
inline float evaluate_network_16hl(const chess::Board& b) {
int32_t acc[16];
for (int i = 0; i < 16; ++i) {
acc[i] = HIDDEN_BIASES[i];
}
chess::Color stm = b.sideToMove();
bool isBlack = (stm == chess::Color::BLACK);
// Precompute rank/file lookup to avoid constructing chess::Square twice
for (int sq = 0; sq < 64; ++sq) {
chess::Piece piece = b.at(chess::Square(sq));
if (piece == chess::Piece::NONE) continue;
int type_idx;
switch (piece.type()) {
case PieceType(PieceType::PAWN): type_idx = 0; break;
case PieceType(PieceType::KNIGHT): type_idx = 1; break;
case PieceType(PieceType::BISHOP): type_idx = 2; break;
case PieceType(PieceType::ROOK): type_idx = 3; break;
case PieceType(PieceType::QUEEN): type_idx = 4; break;
case PieceType(PieceType::KING): type_idx = 5; break;
default: continue;
}
int piece_idx = type_idx;
if (piece.color() != stm) {
piece_idx += 6;
}
// sq is 0-63: rank = sq / 8, file = sq % 8 (algebraic: a1=0, h1=7, a8=56, h8=63)
int rank = sq >> 3; // sq / 8
int file = sq & 7; // sq % 8
int python_square = (7 - rank) * 8 + file;
if (isBlack) {
python_square ^= 56;
}
int feature_idx = piece_idx * 64 + python_square;
// Accumulate contribution of this single active feature into all 16 hidden neurons
for (int h = 0; h < 16; ++h) {
acc[h] += HIDDEN_WEIGHTS[h][feature_idx];
}
}
// SCReLU
constexpr int32_t CLAMP_LIMIT = 255;
int32_t output_accumulation = OUTPUT_BIAS;
for (int i = 0; i < 16; ++i) {
int32_t h = acc[i];
// Branchless clamp to [0, CLAMP_LIMIT]
h = (h < 0) ? 0 : ((h > CLAMP_LIMIT) ? CLAMP_LIMIT : h);
// Square the clipped value and accumulate into output
output_accumulation += h * h * OUTPUT_WEIGHTS[i];
}
float raw_output_float = static_cast<float>(output_accumulation) / SCALE_FACTOR_SQ;
return sigmoid(raw_output_float);
}
// obsolete?
inline bool isNodeTerminal(GameResult result) {
return result != GameResult::NONE;
}
bool isNodeFullyExpanded(int node) {
return tree[node].firstchild != 0;
}
void expandNode(int parent) {
Movelist moves;
movegen::legalmoves(moves, board);
if (moves.empty()) return;
constexpr int pieceValues[] = {100, 300, 300, 500, 900, 10000};
auto getMvvLvaScore = [&](const Move& m) -> int {
bool isEnPassant = (m.typeOf() == Move::ENPASSANT);
Piece victimPiece = board.at(m.to());
if (victimPiece == Piece::NONE && !isEnPassant) {
return 0;
}
PieceType attacker = board.at(m.from()).type();
PieceType victim = isEnPassant ? PieceType::PAWN : victimPiece.type();
return (pieceValues[static_cast<int>(victim)] * 10)
- pieceValues[static_cast<int>(attacker)]
+ 100000;
};
std::sort(moves.begin(), moves.end(), [&](const Move& a, const Move& b) {
return getMvvLvaScore(a) > getMvvLvaScore(b);
});
tree[parent].firstchild = tree.size();
tree[parent].num_children = moves.size();
std::vector<double> policyWeights(moves.size());
double totalWeight = 0.0;
for (size_t i = 0; i < moves.size(); ++i) {
policyWeights[i] = 1.0 / (static_cast<double>(i) + 1.0);
totalWeight += policyWeights[i];
}
for (size_t i = 0; i < moves.size(); ++i) {
Node child = Node();
child.action = moves[i];
child.policy = policyWeights[i] / totalWeight;
tree.push_back(child);
}
}
const float C_PUCT = 1.41f;
inline float fast_sqrt_hint(float x) {
if (x <= 0.0f) return 0.0f;
#if defined(__SSE2__)
__m128 v = _mm_set_ss(x);
__m128 r = _mm_rsqrt_ss(v); // r ~ 1/sqrt(x)
// Newton-Raphson refinement for sqrt(x):
// result = 0.5 * (x * r) * (3.0 - x * r * r)
__m128 x_r = _mm_mul_ss(v, r);
__m128 x_r_r = _mm_mul_ss(x_r, r);
__m128 three = _mm_set_ss(3.0f);
__m128 half = _mm_set_ss(0.5f);
__m128 term = _mm_sub_ss(three, x_r_r);
__m128 refined = _mm_mul_ss(half, _mm_mul_ss(x_r, term));
float result;
_mm_store_ss(&result, refined);
return result;
#else
return sqrtf(x);
#endif
}
// https://github.com/TomaszJaworski777/cpu-mcts-tutorial
inline float PUCT(int node, int parent) {
const Node& child = tree[node];
const Node& parent_node = tree[parent];
float q = (child.visits == 0) ? 0.5f : (child.value / child.visits);
float N_parent = static_cast<float>(parent_node.visits);
if (N_parent < 1.0f) N_parent = 1.0f;
// Inject child.policy here to scale the exploration factor
float p = child.policy * fast_sqrt_hint(N_parent) / (static_cast<float>(child.visits) + 1.0f);
return q + C_PUCT * p;
}
int selectBestChild(int parent) {
int bestChild = tree[parent].firstchild;
float bestChildPUCT = -MAXFLOAT; // no MINFLOAT?
for(int i = 0; i < tree[parent].num_children; i++) {
float currentChildPUCT = PUCT(tree[parent].firstchild + i, parent);
if(currentChildPUCT > bestChildPUCT) {
bestChild = tree[parent].firstchild + i;
bestChildPUCT = currentChildPUCT;
}
}
return bestChild;
}
void backpropagateResult(const std::vector<int>& line, float value) {
// Backpropagate through the path from leaf to child of root
for (int i = static_cast<int>(line.size()) - 1; i >= 0; i--) {
tree[line[i]].visits++;
tree[line[i]].value += value;
value = 1 - value;
}
// Update root node
tree[0].visits++;
tree[0].value += value;
}
// theoretically unused but leaving it here for static evaluation
inline int material(const chess::Board& b) {
auto count = [](auto bb) { return std::popcount(bb.getBits()); };
return
100 * (count(b.us(chess::Color::WHITE) & b.pieces(chess::PieceType::PAWN))
- count(b.us(chess::Color::BLACK) & b.pieces(chess::PieceType::PAWN))) +
320 * (count(b.us(chess::Color::WHITE) & b.pieces(chess::PieceType::KNIGHT))
- count(b.us(chess::Color::BLACK) & b.pieces(chess::PieceType::KNIGHT))) +
330 * (count(b.us(chess::Color::WHITE) & b.pieces(chess::PieceType::BISHOP))
- count(b.us(chess::Color::BLACK) & b.pieces(chess::PieceType::BISHOP))) +
500 * (count(b.us(chess::Color::WHITE) & b.pieces(chess::PieceType::ROOK))
- count(b.us(chess::Color::BLACK) & b.pieces(chess::PieceType::ROOK))) +
900 * (count(b.us(chess::Color::WHITE) & b.pieces(chess::PieceType::QUEEN))
- count(b.us(chess::Color::BLACK) & b.pieces(chess::PieceType::QUEEN)));
}
// Helper: build PV line (most-visited child path) from root into `out`
void buildPV(std::vector<int>& out) {
out.clear();
int cur = 0;
while (isNodeFullyExpanded(cur)) {
int bestChild = 0;
int bestVisits = -1;
for (int i = 0; i < tree[cur].num_children; i++) {
int idx = tree[cur].firstchild + i;
if (tree[idx].visits > bestVisits) {
bestChild = idx;
bestVisits = tree[idx].visits;
}
}
out.push_back(bestChild);
cur = bestChild;
}
}
// Helper: print UCI info line
void printInfo(long long plyTotal, int plyMax, int iterationsCompleted,
double safeElapsed, const std::vector<int>& pvLine) {
int simPerSec = static_cast<int>(iterationsCompleted / safeElapsed);
int hashfull = 0;
if (hashLimitNodes > 0) {
size_t used = tree.size();
if (used > hashLimitNodes) used = hashLimitNodes;
hashfull = static_cast<int>((used * 1000) / hashLimitNodes);
}
float y = floor(((1.0f - (static_cast<float>(tree[0].value) / tree[0].visits)) - 0.5f) * 100.0f * 20.0f);
std::cout << "info depth " << (plyTotal / iterationsCompleted)
<< " seldepth " << plyMax
<< " nodes " << iterationsCompleted
<< " nps " << simPerSec
<< " hashfull " << hashfull
<< " time " << static_cast<int>(safeElapsed * 1000)
<< " score cp " << y
<< " pv ";
for (int node : pvLine) {
std::cout << uci::moveToUci(tree[node].action) << " ";
}
std::cout << std::endl;
}
void pruneTree() {
const size_t n = tree.size();
if (n < 1024) return; // not worth compacting tiny trees
size_t keepTarget = static_cast<size_t>(n * PRUNE_KEEP_FRACTION);
if (keepTarget < 1) keepTarget = 1;
if (keepTarget > n) keepTarget = n;
// --- Pass A: mark the top-`keepTarget` nodes by (visits desc, index asc) ---
// That set is ancestor-closed (see above), so it is a connected subtree
// rooted at node 0 and compacts cleanly.
std::vector<bool> kept(n, false);
{
std::vector<std::pair<int,int>> rank; // (visits, index)
rank.reserve(n);
for (size_t i = 0; i < n; i++)
rank.push_back({tree[i].visits, static_cast<int>(i)});
std::partial_sort(rank.begin(), rank.begin() + keepTarget, rank.end(),
[](const std::pair<int,int>& a, const std::pair<int,int>& b) {
if (a.first != b.first) return a.first > b.first;
return a.second < b.second;
});
for (size_t i = 0; i < keepTarget; i++) kept[rank[i].second] = true;
kept[0] = true; // root always retained (defensive)
} // `rank` freed before compaction to keep peak memory down
// --- Pass B: emit the kept subtree in BFS order ---
// BFS lets us write each kept node's kept children as one contiguous block
// (same layout expandNode() produces), so firstchild + num_children stay
// valid. Subtrees may land anywhere in the array -- that's fine, they are
// reached through firstchild, exactly like the organically grown tree.
size_t keptCount = 0;
for (size_t i = 0; i < n; i++) if (kept[i]) keptCount++;
std::vector<Node> newTree;
newTree.reserve(keptCount);
std::vector<int> oldToNew(n, -1);
std::vector<int> keptKids; // reused
std::queue<int> bfs;
oldToNew[0] = 0;
newTree.push_back(tree[0]);
bfs.push(0);
while (!bfs.empty()) {
int p = bfs.front(); bfs.pop();
int pNew = oldToNew[p];
const Node& pnode = tree[p];
keptKids.clear();
if (pnode.firstchild != 0) {
for (int i = 0; i < pnode.num_children; i++) {
int ci = pnode.firstchild + i;
if (kept[ci]) keptKids.push_back(ci);
}
}
if (keptKids.empty()) {
// No retained children: this node becomes a re-expandable leaf.
newTree[pNew].firstchild = 0;
newTree[pNew].num_children = 0;
continue;
}
int childBlockStart = static_cast<int>(newTree.size());
newTree[pNew].firstchild = childBlockStart;
newTree[pNew].num_children = static_cast<int>(keptKids.size());
for (int ci : keptKids) {
oldToNew[ci] = static_cast<int>(newTree.size());
newTree.push_back(tree[ci]); // firstchild fixed when ci is processed
bfs.push(ci);
}
}
if (debug && !datagenActive) {
std::cout << "info string prune " << n << " -> " << newTree.size()
<< " nodes (hash limit " << hashLimitNodes << ")" << std::endl;
}
tree = std::move(newTree);
}
SearchResult monteCarloSearch(int iterationsMax, int timeMax) {
if (iterationsMax == 0) iterationsMax = INT32_MAX;
tree.clear();
Node root = Node();
tree.push_back(root);
expandNode(0);
auto startTime = std::chrono::steady_clock::now();
long long plyCurrent = 0;
long long plyTotal = 0;
int plyMax = 0;
int iterationsCompleted = 0;
// Reusable buffers to avoid per-iteration allocations
std::vector<int> line;
line.reserve(64);
std::vector<int> pvLine;
pvLine.reserve(32);
int infoInterval = 100000;
int nextInfo = infoInterval;
int nextTimeCheck = 1000;
for (; iterationsCompleted < iterationsMax; iterationsCompleted++) {
line.clear();
int current = 0;
// Selection: descend to a leaf
while (isNodeFullyExpanded(current)) {
current = selectBestChild(current);
board.makeMove(tree[current].action);
plyCurrent++;
line.push_back(current);
}
// Expansion + one more selection step
auto [over, r] = board.isGameOver();
if (r == GameResult::NONE && !isNodeFullyExpanded(current)) {
expandNode(current);
current = selectBestChild(current);
board.makeMove(tree[current].action);
plyCurrent++;
line.push_back(current);
// Re-check game state after the extra move
auto result2 = board.isGameOver();
r = result2.second;
}
// Track depth stats
if (plyCurrent > plyMax) plyMax = plyCurrent;
plyTotal += plyCurrent;
plyCurrent = 0;
// Evaluation
float value = 0;
switch (r) {
case GameResult::WIN: value = 1.0f; break;
case GameResult::DRAW: value = 0.49f; break; // contempt
case GameResult::LOSE: value = 0.0f; break;
// removed * 0.9f, either tune or throw out, is probably bad for eval
//case GameResult::NONE: value = evaluate_network_16hl(board); break;
// fallback
case GameResult::NONE: {
float white_persp = 1.0 / (1.0 + std::exp(-static_cast<double>(material(board)) / 400.0));
value = (board.sideToMove() == Color::WHITE) ? white_persp : (1.0f - white_persp);
break;
}
}
backpropagateResult(line, 1.0f - value);
// Unmake moves to restore root position
for (int i = static_cast<int>(line.size()) - 1; i >= 0; i--) {
board.unmakeMove(tree[line[i]].action);
}
// Periodic time check (not every iteration)
if (timeMax != 0 && iterationsCompleted >= nextTimeCheck) {
auto currentTime = std::chrono::steady_clock::now();
double elapsed = std::max(1e-9, std::chrono::duration<double>(currentTime - startTime).count());
if (timeMax < elapsed * 1000.0) break;
nextTimeCheck += 1000;
}
// compact
if (tree.size() >= hashLimitNodes) {
pruneTree();
}
// Periodic info print
if (!datagenActive && iterationsCompleted >= nextInfo) {
auto currentTime = std::chrono::steady_clock::now();
double safeElapsed = std::max(1e-9, std::chrono::duration<double>(currentTime - startTime).count());
buildPV(pvLine);
printInfo(plyTotal, plyMax, iterationsCompleted, safeElapsed, pvLine);
nextInfo += infoInterval;
}
}
// Final PV and info
buildPV(pvLine);
auto currentTime = std::chrono::steady_clock::now();
double safeElapsed = std::max(1e-9, std::chrono::duration<double>(currentTime - startTime).count());
if (!datagenActive) {
printInfo(plyTotal, plyMax, iterationsCompleted, safeElapsed, pvLine);
}
// Debug output
if (debug && !datagenActive) {
const int BAR_WIDTH = 40;
std::cout << "\nMove Statistics:\n";
std::cout << "-------------------------------\n";
for (int i = 0; i < tree[0].num_children; i++) {
const Node& child = tree[tree[0].firstchild + i];
float ratio = static_cast<float>(child.visits) / iterationsCompleted;
int filled = static_cast<int>(ratio * BAR_WIDTH);
std::cout << " [";
for (int j = 0; j < BAR_WIDTH; j++) {
std::cout << (j < filled ? '#' : ' ');
}
std::cout << "] " << uci::moveToUci(child.action) << " "
<< child.visits << " ("
<< std::fixed << ratio * 100.0f << "%" << ") q="
<< (child.visits > 0 ? child.value / child.visits : 0.0f)
<< " puct=" << PUCT(tree[0].firstchild + i, 0) << "\n";
}
}
int bestChild = pvLine.empty() ? tree[0].firstchild : pvLine[0];
SearchResult result;
result.bestMove = tree[bestChild].action;
result.nps = static_cast<int>(iterationsCompleted / safeElapsed);
result.confidence = 1.0f - (static_cast<float>(tree[0].value) / tree[0].visits);
result.policy.reserve(tree[0].num_children);
for (int i = 0; i < tree[0].num_children; i++) {
const Node& child = tree[tree[0].firstchild + i];
result.policy.push_back({child.action, child.visits});
}
return result;
}
struct DatagenPosition {
std::string fen;
std::vector<std::pair<Move, int>> policy;
Color sideToMove;
float confidence;
};
#pragma pack(push, 1)
struct PositionRecord {
uint64_t bitboards[12]; // 96 bytes: 768-bit one-hot encoding
float confidence; // 4 bytes: MCTS search evaluation
};
#pragma pack(pop) // Total size: Exactly 100 bytes per position
// custom binary trickery
void writeGameToBinary(const std::string& filename,
const std::vector<DatagenPosition>& game,
const Color& winner)
{
std::ofstream file(filename, std::ios::binary | std::ios::app);
if (!file.is_open()) return;
std::vector<PositionRecord> records;
records.reserve(game.size());
for (const auto& position : game)
{
PositionRecord record;
float gameResult = 0.5f; // Default to a draw
if (winner == Color::WHITE) {
gameResult = (position.sideToMove == Color::WHITE) ? 1.0f : 0.0f;
} else if (winner == Color::BLACK) {
gameResult = (position.sideToMove == Color::BLACK) ? 1.0f : 0.0f;
}
// Apply lambda blend formula
record.confidence = (0.7f * position.confidence) + (0.3f * gameResult);
std::memset(record.bitboards, 0, sizeof(record.bitboards));
bool isWhite = (position.sideToMove == Color::WHITE);
int rank = 7;
int fileIdx = 0;
for (char c : position.fen)
{
if (c == ' ') break;
if (c == '/') {
rank--;
fileIdx = 0;
}
else if (std::isdigit(c)) {
fileIdx += c - '0';
}
else {
int pIdx = -1;
// Bake PIECE_MAP_W vs PIECE_MAP_B logic directly into the parser
if (isWhite) {
switch (c) {
case 'P': pIdx = 0; break; case 'N': pIdx = 1; break;
case 'B': pIdx = 2; break; case 'R': pIdx = 3; break;
case 'Q': pIdx = 4; break; case 'K': pIdx = 5; break;
case 'p': pIdx = 6; break; case 'n': pIdx = 7; break;
case 'b': pIdx = 8; break; case 'r': pIdx = 9; break;
case 'q': pIdx = 10; break; case 'k': pIdx = 11; break;
}
} else {
switch (c) {
case 'p': pIdx = 0; break; case 'n': pIdx = 1; break;
case 'b': pIdx = 2; break; case 'r': pIdx = 3; break;
case 'q': pIdx = 4; break; case 'k': pIdx = 5; break;
case 'P': pIdx = 6; break; case 'N': pIdx = 7; break;
case 'B': pIdx = 8; break; case 'R': pIdx = 9; break;
case 'Q': pIdx = 10; break; case 'K': pIdx = 11; break;
}
}
if (pIdx != -1) {
int square = rank * 8 + fileIdx;
// Flip the board vertically for Black's perspective (square ^ 56)
if (!isWhite) {
square ^= 56;
}
record.bitboards[pIdx] |= (1ULL << square);
fileIdx++;
}
}
}
records.push_back(record);
}
file.write(reinterpret_cast<const char*>(records.data()), records.size() * sizeof(PositionRecord));
}
// viriformat
// spec: https://github.com/cosmobobak/viriformat
#pragma pack(push, 1)
struct ViriformatBoard {
uint64_t occupied = 0; // 8 bytes: Bitmask of occupied squares
uint8_t pieces[16] = {0}; // 16 bytes: 32 packed 4-bit piece entries
uint8_t stm_ep = 64; // 1 byte: MSB = side to move, Lower 7 bits = EP square
uint8_t halfmove = 0; // 1 byte: Halfmove clock
uint16_t fullmove = 1; // 2 bytes: Fullmove counter
int16_t score = 0; // 2 bytes: Root position evaluation
uint8_t result = 1; // 1 byte: 0 = Black Win, 1 = Draw, 2 = White Win
uint8_t padding = 0; // 1 byte: Memory alignment padding
};
#pragma pack(pop) // Total size: Exactly 32 bytes
void writeGameToViriformat(const std::string& filename,
const std::vector<DatagenPosition>& game,
const Color& winner)
{
if (game.empty()) return; // no moves happened
// stream
std::ofstream file(filename, std::ios::binary | std::ios::app);
if (!file.is_open()) return;
ViriformatBoard packed_board;
// parse fen
std::stringstream ss(game[0].fen);
std::string pieces_str, stm_str, castling_str, ep_str, halfmove_str, fullmove_str;
ss >> pieces_str >> stm_str >> castling_str >> ep_str >> halfmove_str >> fullmove_str;
// Reconstruct initial position
std::vector<char> board_state(64, ' ');
int rank = 7;
int fileIdx = 0;
for (char c : pieces_str) {
if (c == '/') {
rank--;
fileIdx = 0;
} else if (std::isdigit(c)) {
fileIdx += c - '0';
} else {
board_state[rank * 8 + fileIdx] = c;
fileIdx++;
}
}
// Set occupied bitboard
for (int s = 0; s < 64; ++s) {
if (board_state[s] != ' ') {
packed_board.occupied |= (1ULL << s);
}
}
// Pack piece entries into array
int piece_count = 0;
for (int s = 0; s < 64; ++s) {
if (board_state[s] != ' ') {
char c = board_state[s];
bool is_black = std::islower(c);
char type_char = std::tolower(c);
uint8_t type = 0;
if (type_char == 'p') type = 0;
else if (type_char == 'n') type = 1;
else if (type_char == 'b') type = 2;
else if (type_char == 'r') {
type = 3; // Default Rook
// Check castling strings to upgrade active rooks to Unmoved Rooks (Type 6)
if (!is_black) {
if (s == 7 && castling_str.find('K') != std::string::npos) type = 6;
else if (s == 0 && castling_str.find('Q') != std::string::npos) type = 6;
} else {
if (s == 63 && castling_str.find('k') != std::string::npos) type = 6;
else if (s == 56 && castling_str.find('q') != std::string::npos) type = 6;
}
}
else if (type_char == 'q') type = 4;
else if (type_char == 'k') type = 5;
uint8_t piece_val = type;
if (is_black) piece_val |= 0x08; // Set MSB bit for Black
// Store two 4-bit entries per byte
int byte_idx = piece_count / 2;
if (piece_count % 2 == 0) {
packed_board.pieces[byte_idx] |= (piece_val & 0x0F);
} else {
packed_board.pieces[byte_idx] |= ((piece_val & 0x0F) << 4);
}
piece_count++;
}
}
// Pack Side-to-move and En-passant field
uint8_t stm_ep = 64;
if (!ep_str.empty() && ep_str != "-") {
stm_ep = (ep_str[1] - '1') * 8 + (ep_str[0] - 'a');
}
if (stm_str == "b") {
stm_ep |= 0x80; // Set MSB if Black to move
}
packed_board.stm_ep = stm_ep;
// Parsing clocks with safe defaults
packed_board.halfmove = halfmove_str.empty() ? 0 : std::stoi(halfmove_str);
packed_board.fullmove = fullmove_str.empty() ? 1 : std::stoi(fullmove_str);
packed_board.score = 0;
// Game final result mapping
if (winner == Color::WHITE) packed_board.result = 2;
else if (winner == Color::BLACK) packed_board.result = 0;
else packed_board.result = 1;
file.write(reinterpret_cast<const char*>(&packed_board), sizeof(ViriformatBoard));
auto fill_flat_board = [](const std::string& fen, std::vector<char>& b) {
std::stringstream fss(fen);
std::string p_str; fss >> p_str;
int r = 7, f = 0;
for (char c : p_str) {
if (c == '/') { r--; f = 0; }
else if (std::isdigit(c)) { f += c - '0'; }
else { b[r * 8 + f] = c; f++; }
}
};
for (size_t i = 0; i < game.size() - 1; ++i) {
std::vector<char> b1(64, ' '), b2(64, ' ');
fill_flat_board(game[i].fen, b1);
fill_flat_board(game[i + 1].fen, b2);
bool is_white_turn = (game[i].sideToMove == Color::WHITE);
int from_sq = -1, to_sq = -1;
int move_type = 0; // 0: Normal, 1: EP, 2: Castling, 3: Promotion
int promo_piece = 0; // 0: N, 1: B, 2: R, 3: Q
// king takes rook castling
if (is_white_turn) {
if (b1[4] == 'K' && b2[6] == 'K') { from_sq = 4; to_sq = 7; move_type = 2; }
else if (b1[4] == 'K' && b2[2] == 'K') { from_sq = 4; to_sq = 0; move_type = 2; }
} else {
if (b1[60] == 'k' && b2[62] == 'k') { from_sq = 60; to_sq = 63; move_type = 2; }
else if (b1[60] == 'k' && b2[58] == 'k') { from_sq = 60; to_sq = 56; move_type = 2; }
}
// If not a castling move, parse square deltas
if (move_type == 0) {
std::vector<int> changed;
for (int s = 0; s < 64; ++s) {
if (b1[s] != b2[s]) changed.push_back(s);
}
// Identify the active moving piece's starting square
for (int s : changed) {
if (b1[s] != ' ' && (std::isupper(b1[s]) == is_white_turn)) {
from_sq = s;
}
}
if (changed.size() == 3) {
// En-Passant Capture
move_type = 1;
for (int s : changed) {
if (s != from_sq && b1[s] == ' ' && (b2[s] == 'P' || b2[s] == 'p')) {
to_sq = s;
}
}
} else {
// Normal move, simple capture, or promotion
for (int s : changed) {
if (s != from_sq) to_sq = s;
}
// Check for Pawn Promotion
if (std::tolower(b1[from_sq]) == 'p' && (to_sq / 8 == 7 || to_sq / 8 == 0)) {
move_type = 3;
char p_char = std::tolower(b2[to_sq]);
if (p_char == 'n') promo_piece = 0;
else if (p_char == 'b') promo_piece = 1;
else if (p_char == 'r') promo_piece = 2;
else if (p_char == 'q') promo_piece = 3;
}
}
}
// Pack values into the Move structure
uint16_t packed_move = 0;
packed_move |= (from_sq & 0x3F);
packed_move |= ((to_sq & 0x3F) << 6);
packed_move |= ((promo_piece & 0x03) << 12);
packed_move |= ((move_type & 0x03) << 14);
// Convert confidence float to viriformat White-Relative Centipawn Score (i16)
float gameResVal = 0.5f;
if (winner == Color::WHITE) gameResVal = (game[i].sideToMove == Color::WHITE) ? 1.0f : 0.0f;
else if (winner == Color::BLACK) gameResVal = (game[i].sideToMove == Color::BLACK) ? 1.0f : 0.0f;
float blended = (0.7f * game[i].confidence) + (0.3f * gameResVal);
// Inverse sigmoid with scale 400 (reverse of 1/(1+exp(-x/400)))
float clamped = std::max(1e-6f, std::min(1.0f - 1e-6f, blended));
int16_t move_score = static_cast<int16_t>(400.0f * std::log(clamped / (1.0f - clamped)));
file.write(reinterpret_cast<const char*>(&packed_move), sizeof(uint16_t));
file.write(reinterpret_cast<const char*>(&move_score), sizeof(int16_t));
}
uint32_t terminator = 0;
file.write(reinterpret_cast<const char*>(&terminator), sizeof(uint32_t));
}
void datagen() {
datagenActive = true;
int i = 0;
long allPlies = 0;
// Set up a proper random engine for weighted sampling
std::random_device rd;
std::mt19937 gen(rd());
while (true) {
// Make a random opening position
board.setFen("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1");
int numMoves = 5 + rand() % 5;
for (int j = 0; j < numMoves; j++) {
Movelist moves;
movegen::legalmoves(moves, board);
if (moves.empty()) {
break;
}
int index = rand() % moves.size();
board.makeMove(moves[index]);
}
std::string startingFen = board.getFen();
std::vector<DatagenPosition> game;
int ply = 0;
// --- START TIMER ---
auto startTime = std::chrono::high_resolution_clock::now();
while (true) {
Movelist moves;
movegen::legalmoves(moves, board);
if (board.isGameOver().second != GameResult::NONE)
break;
// Run the deep MCTS search for every recorded position
SearchResult search = monteCarloSearch(1501, 0);
DatagenPosition pos;
pos.fen = board.getFen();
pos.policy = search.policy;
pos.sideToMove = board.sideToMove();
pos.confidence = search.confidence;
game.push_back(pos);
Move moveToPlay;
// --- TEMPERATURE MECHANISM ---
if (ply < 20) {
// T = 1: Weighted random choice based strictly on visit counts
int totalVisits = 0;
for (const auto& p : search.policy) {
totalVisits += p.second;
}
if (totalVisits > 0) {
std::uniform_int_distribution<> dis(0, totalVisits - 1);
int target = dis(gen);
int currentSum = 0;
for (const auto& p : search.policy) {
currentSum += p.second;
if (currentSum > target) {
moveToPlay = p.first;
break;
}
}
} else {
moveToPlay = search.bestMove; // Fallback safety
}
}
else {
// T = 0: Exploit mode. Play the absolute best move found by the search
moveToPlay = search.bestMove;
}
board.makeMove(moveToPlay);
ply++;
allPlies++;
}
// --- END TIMER & CALCULATE SPEED ---
auto endTime = std::chrono::high_resolution_clock::now();
std::chrono::duration<double> elapsed = endTime - startTime;
auto [over, result] = board.isGameOver();
Color winner = Color::NONE; // stays none if draw
if (result == GameResult::LOSE)
{
// sideToMove is checkmated, so opposite side won
winner = (board.sideToMove() == Color::WHITE)
? Color::BLACK
: Color::WHITE;
}
double movesPerSec = (elapsed.count() > 0) ? (ply / elapsed.count()) : 0.0;
std::cout << "Game " << i << ": " << startingFen
<< " | plies: " << ply
<< " | speed: " << std::fixed << std::setprecision(2) << movesPerSec << " plies/s"
<< " | total plies: " << allPlies
<< std::endl;
writeGameToViriformat("data/test.vf", game, winner);
game.clear();
i++;
}
}
void handlePosition(std::istringstream& ss) {
std::string token;
ss >> token; // Should be "startpos" or "fen"
if (token == "startpos") {
board.setFen("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1");
} else if (token == "fen") {
std::string fen_string;
// Read the FEN string- can have spaces
while (ss >> token && token != "moves") {
fen_string += token + " ";
}
if (!fen_string.empty()) {fen_string.pop_back();}
board.setFen(fen_string);
if (token == "moves") {
while (ss >> token) {
//std::cout << token << std::endl;
chess::Move move = uci::uciToMove(board, token);
board.makeMove(move);
}
}
return;
}
ss >> token; // Should be "moves"
if (token == "moves") {