-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmcts.c
More file actions
1265 lines (1081 loc) · 38.4 KB
/
mcts.c
File metadata and controls
1265 lines (1081 loc) · 38.4 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
/*
Copyright (C) 2020- TheTrustedComputer
*/
#include "mcts.h"
// Toggle the run flag to stop after receiving SIGINT
static inline void MCTS_stop(int UNUSED)
{
(void)(UNUSED); // To suppress the unused parameter warning
atomic_store(&runMCTS, false);
}
/*!
* @param _init The MCTS node to initialize.
* @param _ancest The ancestor or parent node.
* @param _MOVE The move that led to this node.
*/
void MCTSNode_initialize(MCTSNode* restrict _init, MCTSNode* restrict _ancest, const uint8_t _MOVE)
{
_init->ancestor = _ancest;
_init->descendant = nullptr;
_init->points = _init->visits = _init->count = 0;
_init->move = _MOVE;
_init->state = MCTS_UNSOLVED;
// _init->depth = _ancest ? _ancest->depth + 1 : 0;
}
/*!
* @param _dest The MCTS node to destroy or free.
*/
void MCTSNode_destroy(MCTSNode* restrict _dest)
{
uint8_t node;
if (_dest->descendant)
{
for (node = 0; node < _dest->count; node++)
{
MCTSNode_destroy(&_dest->descendant[node]);
}
free(_dest->descendant);
}
}
/*!
* @param _PRINT The MCTS node to print.
*/
void MCTSNode_print(const MCTSNode* restrict _PRINT)
{
printf("\e[1m%d%c\e[0m: %lld %lld %d %.8f\n", _PRINT->move >> 4, 'A' + (_PRINT->move & 0xf), _PRINT->points, _PRINT->visits, _PRINT->count, MCTS_uct(_PRINT));
}
/*!
* @param _PRINTALL Which MCTS node to print all of.
* @param _DEPTH The depth of the node.
*/
void MCTSNode_printAll(const MCTSNode* restrict _PRINTALL, const int _DEPTH)
{
uint8_t node;
// Print all tree nodes
if (_PRINTALL->count)
{
for (node = 0; node < _PRINTALL->count; node++)
{
MCTSNode_printAll(_PRINTALL->descendant, _DEPTH + 1);
}
}
else
{
printf("Depth %d: ", _DEPTH);
MCTSNode_print(_PRINTALL);
}
}
/*!
* @param _PTS The MCTS node to print the average points.
*/
void MCTSNode_avgPoints(const MCTSNode* restrict _PTS)
{
double avg;
uint8_t tile, col, node;
for (node = 0; _PTS && (node < _PTS->count); node++)
{
avg = (double)(_PTS->descendant[node].points) / _PTS->descendant[node].visits;
tile = _PTS->descendant[node].move >> 4;
col = _PTS->descendant[node].move & 0xf;
printf("%d%c %.2f %lld %lld ", tile, col + 'A', avg, _PTS->descendant[node].points, _PTS->descendant[node].visits);
NodeStatus_print(_PTS->descendant[node].state, true);
puts("");
}
}
/*!
* @param _UCT The MCTS node to calculate the upper confidence bound for trees (UCT).
* @return The UCT value of that node.
* @note It has been adapted to work with the points-based metric.
*/
double MCTS_uct(const MCTSNode* restrict _UCT)
{
return ((double)(_UCT->points) / _UCT->visits) + sqrt(2.0 * log((double)(_UCT->ancestor->visits) / _UCT->visits));
}
/*!
* @param _status The MCTS node to update the game-theoretic state.
*/
void MCTS_updateState(MCTSNode* restrict _status)
{
uint8_t draws, wins, unsolved, i;
// Sum the game-theoretic state of all nodes, propagating it up the tree when we have a conclusive result
for (draws = wins = unsolved = i = 0; i < _status->count; i++)
{
switch (_status->descendant[i].state)
{
case MCTS_LOSS: // One loss is sufficient to prove a win; stop the search
_status->state = MCTS_WIN;
return;
case MCTS_DRAW:
draws++;
break;
case MCTS_WIN:
wins++;
break;
default:
unsolved++;
break;
}
}
if (!unsolved) // Draws and wins require solutions from all descendants
{
if (draws)
{
_status->state = MCTS_DRAW; // We draw if the opponent can draw but not win
}
else if (i && i == wins)
{
_status->state = MCTS_LOSS; // Opponent can win regardless of what we move
}
}
}
/*!
* @param _selector The MCTS node to select.
* @param _m7 The Make 7 game state to update.
* @return The selected MCTS node.
*/
MCTSNode *MCTS_select(MCTSNode* restrict _selector, Make7* restrict _m7)
{
double bestUCT, currUCT;
MCTSNode *selected = nullptr;
uint8_t node;
// As long as this node has descendants
while (_selector->descendant)
{
for (bestUCT = -DBL_MAX, node = 0; node < _selector->count; node++)
{
// Check unvisited descendant nodes; select it for expansion (same as having a UCT value of INFINITY)
if (!_selector->descendant[node].visits)
{
return _selector;
}
// Select the descendant with the highest UCT value, not exploring moves that lead to solved states
if ((_selector->descendant[node].state == MCTS_UNSOLVED) && (currUCT = MCTS_uct(&_selector->descendant[node])) > bestUCT)
{
bestUCT = currUCT;
selected = &_selector->descendant[node];
}
}
// Update the game state to select the next node
Make7_drop(_m7, selected->move >> 4, selected->move & 0xf);
_selector = selected;
}
// This node has no descendants; expand it
return _selector;
}
/*!
* @param _expander The MCTS node to expand.
* @param _M7 The Make 7 game state to generate moves from.
* @return True if the expansion was successful, false otherwise.
*/
bool MCTS_expand(MCTSNode* restrict _expander, const Make7* restrict _M7)
{
MCTSNode expandee;
uint8_t drops[MAKE7_SIZE_X3], node;
// Check if the game is not over; do not expand if it is or already expanded
if (!(_expander->count || Make7_gameOver(_M7)))
{
// Generate the list of possible moves
Make7_generate(_M7, drops, &_expander->count);
// Reserve memory for descendant nodes
if (!(_expander->descendant = malloc(sizeof(*_expander->descendant) * _expander->count)))
{
return false;
}
// Expand the nodes and add them to the list of descendants
for (node = 0; node < _expander->count; node++)
{
MCTSNode_initialize(&expandee, _expander, drops[node]);
_expander->descendant[node] = expandee;
}
}
return true;
}
/*!
* @param _simulMS The Make 7 game state to simulate.
* @param _TURN Who's turn it is to play.
* @return The reward for the simulation.
*/
signed long long MCTS_simulate(Make7* restrict _simulMS, const uint8_t _TURN)
{
unsigned randMove;
uint8_t simulMoves[MAKE7_SIZE_X3], simulCount;
// Play until the game is over
for (;;)
{
// Get the list of possible drops to simulate
Make7_generate(_simulMS, simulMoves, &simulCount);
// If there are moves, select one at random and play that move; otherwise, return no reward to indicate a draw
if (simulCount)
{
randMove = genrand_int32() % simulCount;
Make7_drop(_simulMS, simulMoves[randMove] >> 4, simulMoves[randMove] & 0xf);
// If there is a win or a loss, return the score depending on the player who started the simulation
if (Make7_tilesSumTo7(_simulMS))
{
// ^ is the same as != on a single bit
return (_TURN ^ _simulMS->turn) ? -1 : 1;
}
}
else
{
return 0;
}
}
}
/*!
* @param _backpropagator The MCTS node to backpropagate.
* @param _simulResult The result of the simulation.
*/
void MCTS_backpropagate(MCTSNode* restrict _backpropagator, signed long long _simulResult)
{
while (_backpropagator)
{
MCTS_updateState(_backpropagator);
_backpropagator->visits++;
_backpropagator->points += _simulResult;
_simulResult = -_simulResult;
_backpropagator = _backpropagator->ancestor;
}
}
/*!
* @param _root The root MCTS node to search from.
* @return The move with the highest visits and odds of winning.
*/
MCTSResult MCTS_best(MCTSNode* restrict _root)
{
MCTSNode *bestNode = nullptr;
// long double currPoints, bestPoints = -INFINITY;
unsigned long long bestVisits;
uint8_t node, bestState, descLosses, descDraws, descUnsolved;
// Count the number of unsolved, lost, and drawn descendants
for (node = descLosses = descDraws = descUnsolved = 0; node < _root->count && !descLosses; node++)
{
switch (_root->descendant[node].state)
{
case MCTS_DRAW:
descDraws++;
break;
case MCTS_LOSS:
descLosses++;
break;
case MCTS_UNSOLVED:
descUnsolved++;
break;
}
}
// Pick the descendant that's likely to lose, as this means the ancestor is favored to win
// In other cases, delay the decision until all descendants have been decided
if (descLosses)
{
bestState = MCTS_LOSS;
}
else if (descUnsolved)
{
bestState = MCTS_UNSOLVED;
}
else
{
if (descDraws)
{
bestState = MCTS_DRAW;
}
else
{
bestState = MCTS_WIN;
}
}
// bestPoints = -INFINITY;
// bestVisits = 0;
// Choose the node with the highest mean points per visit
/*for (node = 0; node < _root->count; node++)
{
if (_root->descendant[node].state == bestState && (currPoints = ((long double)(_root->descendant[node].points) / _root->descendant[node].visits)) > bestPoints)
{
bestPoints = currPoints;
bestNode = &_root->descendant[node];
}
}*/
// Choose the node with the highest visit count
for (node = bestVisits = 0; node < _root->count; node++)
{
if ((_root->descendant[node].state == bestState) && (_root->descendant[node].visits > bestVisits))
{
bestVisits = _root->descendant[node].visits;
bestNode = &_root->descendant[node];
}
}
// If there is only one node, choose that
if (!bestNode)
{
bestNode = &_root->descendant[0];
}
return (MCTSResult) {(double)(bestNode->points) / bestNode->visits, bestNode->move, bestNode->state};
}
/*
int SimulThread_simulate(void *_args)
{
SimulThread *data = _args;
// Check if it's time to stop
while (atomic_load(&runMCTS))
{
// Wait for the start signal
Barrier_wait(data->start);
// Simulate
*(data->localRes) = MCTS_simulate(&data->localM7, data->localM7.plyNum & 1);
// Signal that the simulation is done
Barrier_wait(data->finish);
}
return 0;
}
*/
int ProgressThread_print(void *_args)
{
ProgressThread *progress = _args;
struct timespec oneSec = {.tv_sec = 1};
for (;;)
{
thrd_sleep(&oneSec, nullptr);
if (atomic_load(&runMCTS) && progress->output && progress->root->state == MCTS_UNSOLVED)
{
*progress->result = MCTS_best(progress->root);
MCTS_progress(progress->result, progress->winConHandle, *(progress->iters), ++(*progress->seconds), progress->root->state);
}
else
{
break;
}
}
return 0;
}
/*!
* @param _M7 The Make 7 game state to search for a move.
* @param _WIN_HND The handle to the console window (compilation for Windows only).
* @param _OUTPUT Whether to output the search progress to the console.
* @return The best move found by Monte Carlo tree search.
*/
uint8_t MCTS_search(const Make7* restrict _M7, void* restrict _WIN_HND, const bool _OUTPUT)
{
#if defined(_WIN64) || defined(_WIN32)
HANDLE *winTerm = _WIN_HND;
#else
(void)(_WIN_HND);
#endif
static MCTSNode root, *leaf;
static MCTSResult result;
double oneTile[MAKE7_SIZE], twoTile[MAKE7_SIZE], threeTile[MAKE7_SIZE];
long long i, secs, sims;
uint8_t tile, col, state1[MAKE7_SIZE], state2[MAKE7_SIZE], state3[MAKE7_SIZE];
ProgressThread progThread;
thrd_t ioThread;
Make7 mctsM7 = *_M7;
/*thrd_t *simulators;
Barrier simulStart, simulEnd;
SimulThread *simulArgs;
long numThreads;
int tid;
#if defined(_WIN64) || defined(_WIN32)
numThreads = GetMaximumProcessorCount(ALL_PROCESSOR_GROUPS) - 1;
#elifdef __unix__
numThreads = sysconf(_SC_NPROCESSORS_ONLN) - 1; // -1 to exclude the search thread
#else
numThreads = 1;
#endif
//numThreads = 1;
signed long long threadSims[numThreads];
// Create simulation threads based on system hardware
if (!(simulators = malloc(sizeof(*simulators) * numThreads)))
{
printf("Could not allocate memory for simulation threads.\n");
exit(EXIT_FAILURE);
}
if (!(simulArgs = malloc(sizeof(*simulArgs) * numThreads)))
{
printf("Could not allocate memory to store simulation thread arguments.\n");
exit(EXIT_FAILURE);
}
// To take into account the search thread
Barrier_initialize(&simulStart, numThreads + 1);
Barrier_initialize(&simulEnd, numThreads + 1);
for (i = 0; i < numThreads; i++)
{
simulArgs[i] = (SimulThread) {.localRes = &threadSims[i],
.start = &simulStart,
.finish = &simulEnd,
.seed = time(nullptr) + clock() + i,
.id = i};
printf("%x\n", &simulArgs[i].localM7);
switch (thrd_create(&simulators[i], SimulThread_simulate, &simulArgs[i]))
{
case thrd_nomem:
printf("Ran out of memory creating simulation thread #%lld.\n", i);
exit(EXIT_FAILURE);
case thrd_error:
printf("Could not create simulation thread #%lld.\n", i);
exit(EXIT_FAILURE);
default:
break;
}
}*/
progThread = (ProgressThread) {.root = &root,
.result = &result,
.iters = &i,
.seconds = &secs,
.output = _OUTPUT};
#if defined(_WIN64) || defined(_WIN32)
progThread.winConHandle = winTerm;
#else
progThread.winConHandle = nullptr;
#endif
switch (thrd_create(&ioThread, ProgressThread_print, &progThread))
{
case thrd_nomem:
printf("Ran out of memory creating simulation thread #%lld.\n", i);
exit(EXIT_FAILURE);
case thrd_error:
printf("Could not create simulation thread #%lld.\n", i);
exit(EXIT_FAILURE);
default:
break;
}
// Catch SIGINT to stop the search
signal(SIGINT, MCTS_stop);
MCTSNode_initialize(&root, nullptr, 0);
for (i = secs = 0; atomic_load(&runMCTS) && root.state == MCTS_UNSOLVED; i++)
{
// Selection
leaf = MCTS_select(&root, &mctsM7);
// Expansion
if (!MCTS_expand(leaf, &mctsM7))
{
// On no more memory, restore the game state and stop; won't happen if virtual memory is unlimited
mctsM7 = *_M7;
break;
}
// Pick a random move to simulate and update the game state
if (leaf->count)
{
leaf = &leaf->descendant[genrand_int32() % leaf->count];
Make7_drop(&mctsM7, leaf->move >> 4, leaf->move & 0xf);
sims = MCTS_simulate(&mctsM7, mctsM7.turn);
/*// Copy the game state to the threads
for (tid = 0; tid < numThreads; tid++)
{
simulArgs[tid].localM7 = mctsM7;
}
// Start them
Barrier_wait(&simulStart);
// Wait for all to finish
Barrier_wait(&simulEnd);
// Add simulation results
for (tid = 0; tid < numThreads; tid++)
{
sims += threadSims[tid];
}*/
}
else
{
sims = MAKE7_AREA_P1 - Make7_plyNum(&mctsM7);
// Check immediate wins and losses, rewarding them with higher scores
if (Make7_tilesSumTo7(&mctsM7))
{
leaf->state = MCTS_LOSS;
}
else if (Make7_noMoreMoves(&mctsM7))
{
sims = 0;
leaf->state = MCTS_DRAW;
}
}
//sims = Make7_tilesSumTo7(&mctsM7) ? MAKE7_AREA_P1 - leaf->depth : MCTS_simulate(&mctsM7, leaf->depth & 1);
// Simulation and backpropagation
MCTS_backpropagate(leaf, sims);
// Reset the game state for the next loop
mctsM7 = *_M7;
// Compute best move found and print the progress
/*if ((elapsed = difftime(time(nullptr), progress)) >= 1)
{
result = MCTS_best(&root);
#if defined(_WIN64) || defined(_WIN32)
MCTS_progress(&result, winTerm, i, ++secs, root.state);
#else
MCTS_progress(&result, nullptr, i, ++secs, root.state);
#endif
progress = time(nullptr);
}*/
}
// Get the best move by average points per visit
result = MCTS_best(&root);
printf("\a");
if (_OUTPUT)
{
if (root.state != MCTS_UNSOLVED)
{
#ifdef _WIN64
MCTS_progress(&result, winTerm, i, !secs ? 1 : secs, root.state);
#else
MCTS_progress(&result, nullptr, i, !secs ? 1 : secs, root.state);
#endif
}
//root.state != MCTS_UNSOLVED ? NodeStatus_print(root.state, false) : puts("");
puts("");
// Initialize the array of points for invalid moves
for (i = 0; i < MAKE7_SIZE; i++)
{
oneTile[i] = twoTile[i] = threeTile[i] = MCTS_INVALID;
state1[i] = state2[i] = state3[i] = MCTS_UNSOLVED;
}
// Copy the points to the array
for (i = 0; i < root.count; i++)
{
tile = root.descendant[i].move >> 4;
col = root.descendant[i].move & 0xf;
switch (tile)
{
case 1:
oneTile[col] = (double)(root.descendant[i].points) / root.descendant[i].visits;
state1[col] = root.descendant[i].state;
break;
case 2:
twoTile[col] = (double)(root.descendant[i].points) / root.descendant[i].visits;
state2[col] = root.descendant[i].state;
break;
case 3:
threeTile[col] = (double)(root.descendant[i].points) / root.descendant[i].visits;
state3[col] = root.descendant[i].state;
break;
}
}
// Print the point statistics by tile and column
#if defined(_WIN64) || defined(_WIN32)
MCTS_pointStats(winTerm, oneTile, twoTile, threeTile, state1, state2, state3);
#else
MCTS_pointStats(nullptr, oneTile, twoTile, threeTile, state1, state2, state3);
#endif
// MCTSNode_avgPoints(&root);
}
/*for (i = 0; i < numThreads; i++)
{
if (thrd_join(simulators[i], nullptr) == thrd_error)
{
printf("Could not join simulation thread #%lld\n", i);
exit(EXIT_FAILURE);
}
}*/
if (thrd_join(ioThread, nullptr) == thrd_error)
{
puts("Could not join the I/O thread");
exit(EXIT_FAILURE);
}
signal(SIGINT, SIG_DFL);
MCTSNode_destroy(&root);
/*Barrier_destroy(&simulStart);
Barrier_destroy(&simulEnd);
free(simulators);
free(simulArgs);*/
atomic_store(&runMCTS, true);
return result.bestMove;
}
void MCTS_progress(const MCTSResult* restrict _RESULT, void* restrict _WIN_HND, const unsigned long long _ITERS, const unsigned long long _SECS, const uint8_t _STATE)
{
#if defined(_WIN64) || defined(_WIN32)
HANDLE *winTerm = _WIN_HND;
#else
(void)(_WIN_HND);
#endif
printf("\r");
if (_RESULT->meanPts <= -1.0f)
{
#if defined(_WIN64) || defined(_WIN32)
SetConsoleTextAttribute(*winTerm, FOREGROUND_RED | FOREGROUND_INTENSITY);
#else
printf("\e[1;31m");
#endif
}
else if ((_RESULT->meanPts > -1.0f) && (_RESULT->meanPts < 1.0f))
{
#if defined(_WIN64) || defined(_WIN32)
SetConsoleTextAttribute(*winTerm, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY);
#else
printf("\e[1;33m");
#endif
}
else
{
#if defined(_WIN64) || defined(_WIN32)
SetConsoleTextAttribute(*winTerm, FOREGROUND_GREEN | FOREGROUND_INTENSITY);
#else
printf("\e[1;32m");
#endif
}
switch (_STATE)
{
case MCTS_LOSS:
#if defined(_WIN64) || defined(_WIN32)
SetConsoleTextAttribute(*winTerm, FOREGROUND_RED | FOREGROUND_INTENSITY);
#else
printf("\e[0m\e[1;31m");
#endif
break;
case MCTS_DRAW:
#if defined(_WIN64) || defined(_WIN32)
SetConsoleTextAttribute(*winTerm, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY);
#else
printf("\e[0m\e[1;33m");
#endif
break;
case MCTS_WIN:
#if defined(_WIN64) || defined(_WIN32)
SetConsoleTextAttribute(*winTerm, FOREGROUND_GREEN | FOREGROUND_INTENSITY);
#else
printf("\e[0m\e[1;32m");
#endif
default:
break;
}
printf("%d%c ", (_RESULT->bestMove >> 4), 'A' + (_RESULT->bestMove & 0xf));
_STATE != MCTS_UNSOLVED ? NodeStatus_print(_STATE, false) : printf("%.3f", _RESULT->meanPts);
#if defined(_WIN64) || defined(_WIN32)
SetConsoleTextAttribute(*winTerm, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
printf(" %llu %llu %llu", _ITERS, (_ITERS / _SECS), _SECS);
#else
// printf("%d%c %.3f\e[0m %llu %llu %llu", (_RESULT->bestMove >> 4), 'A' + (_RESULT->bestMove & 0xf), _RESULT->meanPts, _ITERS, (_ITERS / _SECS), _SECS);
printf("\e[0m %llu %llu %llu", _ITERS, (_ITERS / _SECS), _SECS);
#endif
#ifdef __unix__
fflush(stdout);
#endif
}
void MCTS_pointStats(void* restrict _WIN_HND, const double* restrict _1T_PTS, const double* restrict _2T_PTS, const double* restrict _3T_PTS, const uint8_t *_1T_STATES, const uint8_t *_2T_STATES, const uint8_t *_3T_STATES)
{
int tile, col;
#if defined(_WIN64) || defined(_WIN32)
HANDLE *winTerm = _WIN_HND;
#else
(void)(_WIN_HND);
#endif
for (tile = 1; tile <= 3; tile++)
{
printf("%d ", tile);
for (col = 0; col < MAKE7_SIZE; col++)
{
switch (tile)
{
case 1:
if (_1T_STATES[col] != MCTS_UNSOLVED)
{
switch (_1T_STATES[col])
{
case MCTS_LOSS:
#if defined(_WIN64) || defined(_WIN32)
SetConsoleTextAttribute(*winTerm, FOREGROUND_GREEN | FOREGROUND_INTENSITY);
#else
printf("\e[1;32m");
#endif
break;
case MCTS_DRAW:
#if defined(_WIN64) || defined(_WIN32)
SetConsoleTextAttribute(*winTerm, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY);
#else
printf("\e[1;33m");
#endif
break;
case MCTS_WIN:
#if defined(_WIN64) || defined(_WIN32)
SetConsoleTextAttribute(*winTerm, FOREGROUND_RED | FOREGROUND_INTENSITY);
#else
printf("\e[1;31m");
#endif
default:
break;
}
NodeStatus_print(_1T_STATES[col], true);
#if defined(_WIN64) || defined(_WIN32)
SetConsoleTextAttribute(*winTerm, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
printf(" ");
#else
printf("\e[0m ");
#endif
}
else
{
if ((_1T_PTS[col] != MCTS_INVALID) && (_1T_PTS[col] <= -1.0))
{
#if defined(_WIN64) || defined(_WIN32)
SetConsoleTextAttribute(*winTerm, FOREGROUND_RED | FOREGROUND_INTENSITY);
#else
printf("\e[1;31m");
#endif
}
else if ((_1T_PTS[col] > -1.0) && (_1T_PTS[col] < 1.0))
{
#if defined(_WIN64) || defined(_WIN32)
SetConsoleTextAttribute(*winTerm, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY);
#else
printf("\e[1;33m");
#endif
}
else if (_1T_PTS[col] >= 1.0)
{
#if defined(_WIN64) || defined(_WIN32)
SetConsoleTextAttribute(*winTerm, FOREGROUND_GREEN | FOREGROUND_INTENSITY);
#else
printf("\e[1;32m");
#endif
}
#if defined(_WIN64) || defined(_WIN32)
(_1T_PTS[col] == MCTS_INVALID) ? printf("-- ") : printf("%.2f ", _1T_PTS[col]);
SetConsoleTextAttribute(*winTerm, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
#else
(_1T_PTS[col] == MCTS_INVALID) ? printf("-- ") : printf("%.2f\e[0m ", _1T_PTS[col]);
#endif
}
break;
case 2:
if (_2T_STATES[col] != MCTS_UNSOLVED)
{
switch (_2T_STATES[col])
{
case MCTS_LOSS:
#if defined(_WIN64) || defined(_WIN32)
SetConsoleTextAttribute(*winTerm, FOREGROUND_GREEN | FOREGROUND_INTENSITY);
#else
printf("\e[1;32m");
#endif
break;
case MCTS_DRAW:
#if defined(_WIN64) || defined(_WIN32)
SetConsoleTextAttribute(*winTerm, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY);
#else
printf("\e[1;33m");
#endif
break;
case MCTS_WIN:
#if defined(_WIN64) || defined(_WIN32)
SetConsoleTextAttribute(*winTerm, FOREGROUND_RED | FOREGROUND_INTENSITY);
#else
printf("\e[1;31m");
#endif
default:
break;
}
NodeStatus_print(_2T_STATES[col], true);
#if defined(_WIN64) || defined(_WIN32)
SetConsoleTextAttribute(*winTerm, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
printf(" ");
#else
printf("\e[0m ");
#endif
}
else
{
if ((_2T_PTS[col] != MCTS_INVALID) && (_2T_PTS[col] <= -1.0))
{
#if defined(_WIN64) || defined(_WIN32)
SetConsoleTextAttribute(*winTerm, FOREGROUND_RED | FOREGROUND_INTENSITY);
#else
printf("\e[1;31m");
#endif
}
else if ((_2T_PTS[col] > -1.0) && (_2T_PTS[col] < 1.0))
{
#if defined(_WIN64) || defined(_WIN32)
SetConsoleTextAttribute(*winTerm, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY);
#else
printf("\e[1;33m");
#endif
}
else if (_2T_PTS[col] >= 1.0)
{
#if defined(_WIN64) || defined(_WIN32)
SetConsoleTextAttribute(*winTerm, FOREGROUND_GREEN | FOREGROUND_INTENSITY);
#else
printf("\e[1;32m");
#endif
}
#if defined(_WIN64) || defined(_WIN32)
(_2T_PTS[col] == MCTS_INVALID) ? printf("-- ") : printf("%.2f ", _2T_PTS[col]);
SetConsoleTextAttribute(*winTerm, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
#else
(_2T_PTS[col] == MCTS_INVALID) ? printf("-- ") : printf("%.2f\e[0m ", _2T_PTS[col]);
#endif
}
break;
case 3:
if (_3T_STATES[col] != MCTS_UNSOLVED)
{
switch (_3T_STATES[col])
{
case MCTS_LOSS:
#if defined(_WIN64) || defined(_WIN32)
SetConsoleTextAttribute(*winTerm, FOREGROUND_GREEN | FOREGROUND_INTENSITY);
#else
printf("\e[1;32m");
#endif
break;
case MCTS_DRAW:
#if defined(_WIN64) || defined(_WIN32)
SetConsoleTextAttribute(*winTerm, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY);
#else
printf("\e[1;33m");
#endif
break;
case MCTS_WIN:
#if defined(_WIN64) || defined(_WIN32)
SetConsoleTextAttribute(*winTerm, FOREGROUND_RED | FOREGROUND_INTENSITY);
#else
printf("\e[1;31m");
#endif
default:
break;
}
NodeStatus_print(_3T_STATES[col], true);
#if defined(_WIN64) || defined(_WIN32)
SetConsoleTextAttribute(*winTerm, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
printf(" ");
#else
printf("\e[0m ");
#endif
}
else
{
if ((_3T_PTS[col] != MCTS_INVALID) && (_3T_PTS[col] <= -1.0))
{
#if defined(_WIN64) || defined(_WIN32)
SetConsoleTextAttribute(*winTerm, FOREGROUND_RED | FOREGROUND_INTENSITY);
#else
printf("\e[1;31m");
#endif
}
else if ((_3T_PTS[col] > -1.0) && (_3T_PTS[col] < 1.0))
{
#if defined(_WIN64) || defined(_WIN32)
SetConsoleTextAttribute(*winTerm, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY);
#else
printf("\e[1;33m");
#endif
}
else if (_3T_PTS[col] >= 1.0)