-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzuniq.hpp
1061 lines (869 loc) · 34 KB
/
zuniq.hpp
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
#ifndef ZUNIQ_ZUNIQ_HPP
#define ZUNIQ_ZUNIQ_HPP
#include "config.hpp"
#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cstdint>
#include <functional>
#include <iomanip>
#include <iostream>
#include <memory>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <unordered_set>
#include <utility>
#include <variant>
#include <vector>
using namespace std;
#define MP make_pair
#define ALL(c) (c).begin(), (c).end()
#define SZ(c) (int) (c).size()
using Move = pair<uint32_t, uint32_t>;
enum class CTX_VAR {
ROUND,
ELAPSED_TIME_MILLIS,
};
using Context = unordered_map<CTX_VAR, int32_t>;
// constants.
static constexpr uint8_t N = 6;
static constexpr size_t TOTAL_SQUARES = (N - 1) * (N - 1);
static constexpr size_t TOTAL_MOVES = N * (N - 1) * 2;
static constexpr pair<int8_t, int8_t> NEIGH_SQ[4] = {{-1, 0},
{1, 0},
{0, -1},
{0, 1}};
static constexpr int TOP_N_FIRST_LEVEL = 5;
static constexpr int TOP_N_SECOND_LEVEL = 5;
static constexpr unsigned int TOTAL_TIME_MILLIS = 30'000U;
struct MoveHash {
size_t operator()(const Move &m) const {
return TOTAL_MOVES * (m.first) + m.second;
}
};
ostream &operator<<(ostream &os, const Move &m) {
os << "Move(" << m.first << "," << m.second << ")";
return os;
}
enum class Player {
WHITE,
BLACK
};
enum class GameCommand {
START,
QUIT,
};
template<typename T>
struct UnionFind {
UnionFind() = default;
UnionFind(const UnionFind &rhs) = default;
explicit UnionFind(size_t max) {
resize(max);
}
void resize(size_t max) {
parent.assign(max, -1);
}
[[nodiscard]] T find_set_const(T e) const {
if (parent[e] < 0) {
return e;
} else {
T root = e;
/* Find root */
while (parent[root] >= 0) {
root = parent[root];
}
return root;
}
}
T find_set(T e) {
if (parent[e] < 0) {
return e;
} else {
T root = e;
/* Find root */
while (parent[root] >= 0) {
root = parent[root];
}
/* No need of path compression */
if (parent[e] == static_cast<int32_t>(root)) return root;
/* Compress path */
while (parent[e] >= 0) {
T tmp = parent[e];
parent[e] = root;
e = tmp;
}
return root;
}
}
void union_set(T e1, T e2) {
e1 = find_set(e1);
e2 = find_set(e2);
if (e1 != e2) {
if (parent[e1] < parent[e2]) {
parent[e1] += parent[e2];
parent[e2] = e1;
} else {
parent[e2] += parent[e1];
parent[e1] = e2;
}
}
}
private:
vector<T> parent;
};
/**
* From @mcleary: https://gist.github.com/mcleary/b0bf4fa88830ff7c882d
*/
class Timer {
std::chrono::time_point<std::chrono::steady_clock> start_time_;
std::chrono::time_point<std::chrono::steady_clock> end_time_;
bool is_running = false;
std::chrono::time_point<std::chrono::steady_clock> elapsed() {
if (is_running) {
return std::chrono::steady_clock::now();
} else {
return end_time_;
}
}
public:
Timer &start() {
start_time_ = std::chrono::steady_clock::now();
is_running = true;
return *this;
}
void stop() {
end_time_ = std::chrono::steady_clock::now();
is_running = false;
}
int64_t elapsed_milli() {
auto end_time = elapsed();
return std::chrono::duration_cast<std::chrono::milliseconds>(end_time - start_time_).count();
}
int64_t elapsed_micro() {
auto end_time = elapsed();
return std::chrono::duration_cast<std::chrono::microseconds>(end_time - start_time_).count();
}
};
struct Board {
Board() : turn_(Player::WHITE), uf_(N * N) {
init_available_moves();
}
Board(const Board &rhs) = default;
Board(Board &&rhs) = default;
Board &operator=(const Board &rhs) = default;
Board &operator=(Board &&rhs) = default;
bool is_over() const {
return available_moves_.empty();
}
Player winner() const {
assert(is_over());
Player winner = Player::WHITE;
if (get_turn() == Player::WHITE) winner = Player::BLACK;
return winner;
}
[[nodiscard]] const Player &get_turn() const {
return turn_;
}
[[nodiscard]] const unordered_set<Move, MoveHash> &get_available_moves() const {
return available_moves_;
}
[[nodiscard]] bool is_valid(Move m) const {
return available_moves_.count(m) > 0;
}
inline bool is_closing_region(const Move &m) const {
return (uf_.find_set_const(m.first) == uf_.find_set_const(m.second));
}
void apply_move(const Move &m) {
assert(is_valid(m));
applied_moves_.insert(m);
if (uf_.find_set(m.first) == uf_.find_set(m.second)) {
const auto &rc = count_regions(m);
assert(rc.first > 0);
assert(closed_sizes_.count(rc.first) == 0);
closed_sizes_.insert(rc.first);
const auto &squares_inside_close_region = rc.second;
for (const auto sq : squares_inside_close_region) {
available_moves_.erase(get_line_above(sq));
on_hold_moves_.erase(get_line_above(sq));
available_moves_.erase(get_line_left(sq));
on_hold_moves_.erase(get_line_left(sq));
available_moves_.erase(get_line_right(sq));
on_hold_moves_.erase(get_line_right(sq));
available_moves_.erase(get_line_bellow(sq));
on_hold_moves_.erase(get_line_bellow(sq));
}
}
uf_.union_set(m.first, m.second);
available_moves_.erase(m);
exclude_invalid_next_moves();
change_turn();
}
// Get square number above a horizontal move.
static inline uint32_t get_square_above(uint32_t move_first) {
uint32_t row = get_line_row(move_first);
uint32_t col = get_line_col(move_first);
assert(row > 0);
return (N - 1) * (row - 1) + col;
}
// Get square number bellow a horizontal move.
static inline uint32_t get_square_bellow(uint32_t move_first) {
uint32_t row = get_line_row(move_first);
uint32_t col = get_line_col(move_first);
assert(row < N - 1);
return (N - 1) * row + col;
}
// Get square number to the left of a vertical move.
static inline uint32_t get_square_left(uint32_t move_first) {
uint32_t row = get_line_row(move_first);
uint32_t col = get_line_col(move_first);
assert(col > 0);
return (N - 1) * row + col - 1;
}
// Get square number to the right of a vertical move.
static inline uint32_t get_square_right(uint32_t move_first) {
uint32_t row = get_line_row(move_first);
uint32_t col = get_line_col(move_first);
assert(col < N - 1);
return (N - 1) * row + col;
}
static inline Move get_line_left(uint32_t square) {
assert(square < TOTAL_SQUARES);
uint32_t row = get_square_row(square);
uint32_t col = get_square_col(square);
uint32_t start = get_move_start(row, col);
return MP(start, start + 6);
}
static inline Move get_line_right(uint32_t square) {
assert(square < TOTAL_SQUARES);
uint32_t row = get_square_row(square);
uint32_t col = get_square_col(square);
uint32_t start = get_move_start(row, col + 1);
return MP(start, start + 6);
}
static inline Move get_line_above(uint32_t square) {
assert(square < TOTAL_SQUARES);
uint32_t row = get_square_row(square);
uint32_t col = get_square_col(square);
uint32_t start = get_move_start(row, col);
return MP(start, start + 1);
}
static inline Move get_line_bellow(uint32_t square) {
assert(square < TOTAL_SQUARES);
uint32_t row = get_square_row(square);
uint32_t col = get_square_col(square);
uint32_t start = get_move_start(row + 1, col);
return MP(start, start + 1);
}
private:
[[nodiscard]] pair<size_t, set<uint32_t>> count_regions(Move candidate_move) const {
queue<uint32_t> Q1;
queue<uint32_t> Q2;
if (candidate_move.second - candidate_move.first == 1) {
// horizontal move.
if (get_line_row(candidate_move.first) > 0) {
// include square above line
Q1.push(get_square_above(candidate_move.first));
}
if (get_line_row(candidate_move.first) < N - 1) {
// includes square bellow line
Q2.push(get_square_bellow(candidate_move.first));
}
} else {
// vertical move.
if (get_line_col(candidate_move.first) > 0) {
// include square left to line
Q1.push(get_square_left(candidate_move.first));
}
if (get_line_col(candidate_move.first) < N - 1) {
// includes square right to line
Q2.push(get_square_right(candidate_move.first));
}
}
auto ans = BFS(Q1);
if (ans.first <= 0) {
ans = BFS(Q2);
}
return ans;
}
/**
* Exclude moves that would make closed regions with sizes in closed_region.
*/
void exclude_invalid_next_moves() {
vector<Move> to_exclude;
// Finds out which ones must be excluded
available_moves_.insert(ALL(on_hold_moves_));
if (!on_hold_moves_.empty()) {
to_exclude.reserve(available_moves_.size());
}
on_hold_moves_.clear();
for (const auto &move : available_moves_) {
if (uf_.find_set(move.first) == uf_.find_set(move.second)) {
applied_moves_.insert(move);
auto rc = count_regions(move);
applied_moves_.erase(move);
if (rc.first > 0 && closed_sizes_.count(rc.first) > 0) {
bool exclusion_flag = true;
for (size_t reg_cnt = 1; reg_cnt <= rc.first; reg_cnt++) {
exclusion_flag &= (closed_sizes_.count(reg_cnt) > 0);
}
if (!exclusion_flag) on_hold_moves_.insert(move);
to_exclude.push_back(move);
}
}
}
// Excludes then
for (auto &move : to_exclude) {
available_moves_.erase(move);
}
}
static inline uint32_t get_line_row(uint32_t pos) {
return pos / N;
}
static inline uint32_t get_line_col(uint32_t pos) {
return pos % N;
}
static inline uint32_t get_square_row(uint32_t pos) {
return pos / (N - 1);
}
static inline uint32_t get_square_col(uint32_t pos) {
return pos % (N - 1);
}
static inline uint32_t get_move_start(uint32_t row, uint32_t col) {
return (row * N) + col;
}
inline void change_turn() {
if (turn_ == Player::WHITE) turn_ = Player::BLACK;
else
turn_ = Player::WHITE;
}
void init_available_moves() {
for (size_t row = 0; row < N; row++) {
for (size_t col = 0; col < N - 1; col++) {
uint32_t start = N * row + col;
available_moves_.emplace(start, start + 1);
}
}
for (size_t row = 0; row < N - 1; row++) {
for (size_t col = 0; col < N; col++) {
uint32_t start = N * row + col;
available_moves_.emplace(start, start + 6);
}
}
assert(available_moves_.size() == TOTAL_MOVES);
}
[[nodiscard]] pair<size_t, set<uint32_t>> BFS(queue<uint32_t> &Q) const {
set<uint32_t> visited;
if (Q.empty()) return MP(0, visited);
visited.insert(Q.front());
function<bool(int32_t, int32_t)> valid = [&visited](int32_t row, int32_t col) {
if (row < 0 || col < 0 || row >= static_cast<int32_t>(N) || col >= static_cast<int32_t>(N) ||
visited.count((N - 1) * row + col) > 0)
return false;
return true;
};
function<bool(uint32_t, uint32_t)> is_open = [this](uint32_t row, uint32_t col) {
assert(row < (uint32_t) N);
assert(col < (uint32_t) N);
uint32_t sq = (N - 1) * row + col;
if ((row == 0 && applied_moves_.count(get_line_above(sq)) == 0) ||
(col == 0 && applied_moves_.count(get_line_left(sq)) == 0) ||
(row == N - 2 && applied_moves_.count(get_line_bellow(sq)) == 0) ||
(col == N - 2 && applied_moves_.count(get_line_right(sq)) == 0)) {
return true;
}
return false;
};
size_t cnt = 0;
while (!Q.empty()) {
size_t sz = Q.size();
while (sz--) {
uint32_t sq = Q.front();
Q.pop();
cnt++;
int32_t row = get_square_row(sq);
int32_t col = get_square_col(sq);
if (is_open(row, col)) {
cnt = 0;
visited.clear();
goto fim;
}
for (auto &[dr, dc] : NEIGH_SQ) {
if (dr < 0 && applied_moves_.count(get_line_above(sq)) > 0) continue;
if (dr > 0 && applied_moves_.count(get_line_bellow(sq)) > 0) continue;
if (dc < 0 && applied_moves_.count(get_line_left(sq)) > 0) continue;
if (dc > 0 && applied_moves_.count(get_line_right(sq)) > 0) continue;
uint32_t new_row = row + dr;
uint32_t new_col = col + dc;
if (valid(new_row, new_col)) {
uint32_t new_sq = (N - 1) * new_row + new_col;
Q.push(new_sq);
visited.insert(new_sq);
}
}
}
}
fim:
return MP(cnt, visited);
}
// Who is the turn_ to play;
Player turn_;
// Available moves
unordered_set<Move, MoveHash> available_moves_;
//Keep track of connected components
UnionFind<int8_t> uf_;
// keep record of closed area sizes
unordered_set<size_t> closed_sizes_;
// Executed moves.
unordered_set<Move, MoveHash> applied_moves_;
unordered_set<Move, MoveHash> on_hold_moves_;
};
namespace IO {
string readln() {
string input;
getline(cin, input);
return input;
}
void writeln(string_view s) {
cout << s << endl;
}
Move parse_move(string_view s) {
assert(s.size() == 3 || (s.size() == 4 && s[3] == '!'));
assert((tolower(s[2]) == 'v') || (tolower(s[2]) == 'h'));
uint32_t row = toupper(s[0]) - 'A';
assert(row < N);
uint32_t col = s[1] - '1';
assert(col < N);
uint32_t start = (N * row) + col;
uint32_t end;
if (s[2] == 'h') {
assert(col < N - 1);
end = start + 1;
} else {
assert(row < N - 1);
end = start + N;
}
assert(end < N * N);
return MP(start, end);
}
string format_move(Move m) {
assert(m.first < m.second);
assert(m.second < 36);
string ans;
ans.resize(3);
ans[0] = (m.first / N) + 'A';
ans[1] = (m.first % N) + '1';
if (m.second - m.first == 1) {
ans[2] = 'h';
} else {
assert(m.second - m.first == N);
ans[2] = 'v';
}
return ans;
}
variant<Move, GameCommand> parse_input(string_view s) {
variant<Move, GameCommand> resp;
string input;
input.resize(s.size());
std::transform(s.begin(), s.end(), input.begin(),
[](unsigned char c) { return tolower(c); });
if (input == "start") {
resp = GameCommand::START;
} else if (input == "quit") {
resp = GameCommand::QUIT;
} else {
resp = parse_move(input);
}
return resp;
}
}// namespace IO
// --------- TimeStrategy ------------//
class TimeStrategy {
uint32_t total_time_millis_;
protected:
[[nodiscard]] uint32_t get_total_time_millis() const noexcept { return total_time_millis_; }
public:
explicit TimeStrategy(uint32_t total_time_millis) : total_time_millis_(total_time_millis) {}
[[nodiscard]] virtual double max_move_time(const Context &) const noexcept = 0;
};
class ConstantTimeStrategy : public TimeStrategy {
public:
explicit ConstantTimeStrategy(uint32_t total_time_millis) : TimeStrategy(total_time_millis) {}
[[nodiscard]] double max_move_time(const Context &) const noexcept override {
return (0.995 * get_total_time_millis()) / 20.0;
}
};
class RemainingTimeStrategy : public TimeStrategy {
public:
explicit RemainingTimeStrategy(uint32_t total_time_millis) : TimeStrategy(total_time_millis) {}
[[nodiscard]] double max_move_time(const Context &ctx) const noexcept override {
assert(ctx.count(CTX_VAR::ROUND) && ctx.count(CTX_VAR::ELAPSED_TIME_MILLIS));
uint32_t remaining_moves = max(1, 20 - ctx.at(CTX_VAR::ROUND) / 2);
uint32_t remaining_time = get_total_time_millis() - ctx.at(CTX_VAR::ELAPSED_TIME_MILLIS);
double move_time = static_cast<double>(0.999 * remaining_time) / remaining_moves;
return move_time;
}
};
class HardCodedTimeStrategy : public TimeStrategy {
public:
explicit HardCodedTimeStrategy(uint32_t total_time_millis) : TimeStrategy(total_time_millis) {}
[[nodiscard]] double max_move_time(const Context &ctx) const noexcept override {
assert(ctx.count(CTX_VAR::ROUND) && ctx.count(CTX_VAR::ELAPSED_TIME_MILLIS));
uint32_t round = ctx.at(CTX_VAR::ROUND);
if (round < 10) return 48.00 * 2;
else if (round < 20)
return 148.00 * 2;
else if (round < 25)
return 750.00 * 2;
else if (round < 35)
return 2'350.00 * 2;
else if (round < 37)
return 200.00 * 2;
else
return 25.00 * 2;
}
};
// --------- Agent -------------------//
struct Agent {
explicit Agent(Player color) : color_(color) {}
void set_color(Player color) { color_ = color; }
virtual pair<Move, bool> select_move(const Board &, const Context &) = 0;
virtual ~Agent() = default;
protected:
Player color_;
};
// --------- RandomAgent -------------------//
struct RandomAgent : public Agent {
RandomAgent(Player color, mt19937 rng, bool with_priority = false, bool verbose = false) : Agent(color),
rng_(rng),
with_priority_(with_priority),
verbose_(verbose) {}
pair<Move, bool> select_move(const Board &b, const Context &) override {
assert(b.get_turn() == color_);
Timer timer = Timer().start();
pair<Move, bool> pmove;
if (with_priority_) {
pmove = select_move_with_priority(b);
} else {
pmove = select_move_no_priority(b);
}
if (verbose_) {
timer.stop();
#ifndef QUIET_MODE
cerr << "[I]: RNDEngine selected: " << IO::format_move((pmove).first) << " in "
<< timer.elapsed_micro() << " µs." << endl;
#endif
}
return pmove;
}
private:
mt19937 rng_;
bool with_priority_;
bool verbose_;
pair<Move, bool> select_move_with_priority(const Board &board) {
assert(board.get_turn() == color_);
vector<Move> moves(ALL(board.get_available_moves()));
vector<double> prob(moves.size());
size_t i = 0;
for (Move move : moves) {
if (board.is_closing_region(move)) {
prob[i] = WEIGHT_CLOSE_REGION_MOVE;
} else {
prob[i] = WEIGHT_REGULAR_MOVE;
}
++i;
}
discrete_distribution dist(prob.begin(), prob.end());
int offset = dist(rng_);
return make_pair(moves[offset], false);
}
pair<Move, bool> select_move_no_priority(const Board &board) {
assert(board.get_turn() == color_);
const auto &moves = board.get_available_moves();
size_t num_moves = moves.size();
size_t offset = uniform_int_distribution<size_t>(0, num_moves - 1)(rng_);
return make_pair(*std::next(moves.begin(), offset), false);
}
};
// --------- MCTS AGENT ---------------//
class MCTSAgent : public Agent {
uint32_t num_rounds_;
double temperature_;
unique_ptr<TimeStrategy> ts_;
mt19937 &rng_;
bool sent_is_winning_;
struct ScoredMove;
// --------- MCTSNode -------------------//
struct MCTSNode {
MCTSNode(const shared_ptr<Board> &game_state, MCTSNode *parent, std::optional<Move> &&move, mt19937 &rng) : game_state_(game_state), parent_(parent), move_(move), rng_(rng), num_rollouts_(0),
white_win_counts_(0), black_win_count_(0),
unvisited_moves_(ALL(game_state->get_available_moves())) {
std::shuffle(ALL(unvisited_moves_), rng_);
children_.reserve(unvisited_moves_.size());
}
MCTSNode(const MCTSNode &rhs) = default;
MCTSNode(MCTSNode &&rhs) = default;
MCTSNode &operator=(const MCTSNode &rhs) = delete;
MCTSNode &operator=(MCTSNode &&rhs) = delete;
[[nodiscard]] std::optional<Move> get_move() const noexcept { return move_; }
MCTSNode &add_random_child() {
Move &new_move = unvisited_moves_.back();
unvisited_moves_.pop_back();
shared_ptr<Board> new_game_state = std::make_shared<Board>(*game_state_);
assert(new_game_state != game_state_);
new_game_state->apply_move(new_move);
children_.emplace_back(new_game_state, this, make_optional(new_move), rng_);
return children_.back();
}
void record_win(Player winner) noexcept {
if (winner == Player::WHITE) ++white_win_counts_;
else
++black_win_count_;
++num_rollouts_;
}
[[nodiscard]] inline bool can_add_child() const noexcept {
return SZ(unvisited_moves_) > 0;
}
[[nodiscard]] inline bool is_terminal() const noexcept {
return game_state_->is_over();
}
[[nodiscard]] double winning_frac(Player player) const noexcept {
double win_counts = black_win_count_;
if (player == Player::WHITE) win_counts = white_win_counts_;
return win_counts / double(num_rollouts_);
}
[[nodiscard]] vector<ScoredMove> top_n(size_t n) const noexcept {
vector<ScoredMove> scored_moves;
const size_t CHILDREN_SIZE = this->children_.size();
scored_moves.reserve(CHILDREN_SIZE);
for (size_t i = 0; i < CHILDREN_SIZE; i++) {
const auto &child = children_[i];
assert(child.move_.has_value());
scored_moves.emplace_back(child.winning_frac(game_state_->get_turn()), &child,
child.num_rollouts_);
}
sort(ALL(scored_moves));
vector<ScoredMove> top_n(scored_moves.begin(), scored_moves.begin() + min(n, CHILDREN_SIZE));
return top_n;
}
private:
shared_ptr<Board> game_state_;
MCTSNode *const parent_;
optional<Move> move_;
mt19937 &rng_;
uint32_t num_rollouts_;
uint32_t white_win_counts_;
uint32_t black_win_count_;
vector<Move> unvisited_moves_;
vector<MCTSNode> children_;
friend class MCTSAgent;
};// end of struct MCTSNode.
// --------- ScoredMove -------------------//
struct ScoredMove {
double winning_fraction_;
const MCTSNode *node_;
uint32_t num_rollouts_;
ScoredMove(double wf, const MCTSNode *const node, int nr) : winning_fraction_(wf), node_(node), num_rollouts_(nr) {}
bool operator<(const ScoredMove &other) const { return winning_fraction_ > other.winning_fraction_; }
friend ostream &operator<<(ostream &out, const ScoredMove &sd) noexcept {
out << IO::format_move(sd.node_->get_move().value()) << ' ' << std::setprecision(2) << sd.winning_fraction_ << '('
<< sd.num_rollouts_ << ')';
return out;
}
};// end of struct ScoredMove
public:
MCTSAgent(uint32_t num_rounds, double temperature, unique_ptr<TimeStrategy> &ts, Player color, mt19937 &rng) : Agent(color),
num_rounds_(num_rounds),
temperature_(temperature),
ts_(std::move(ts)),
rng_(rng),
sent_is_winning_(false) {}
MCTSAgent(const MCTSAgent &rhs) = delete;
MCTSAgent(MCTSAgent &&rhs) = delete;
MCTSAgent &operator=(const MCTSAgent &rhs) = delete;
MCTSAgent &operator=(MCTSAgent &&rhs) = delete;
pair<Move, bool> select_move(const Board &game_state, const Context &ctx) override {
assert(color_ == game_state.get_turn());
Timer timer = Timer().start();
auto sgs = make_shared<Board>(game_state);
MCTSNode root = MCTSNode(sgs, nullptr, make_optional<Move>(), rng_);
for (uint32_t i = 0; i < num_rounds_; i++) {
if ((i % 10 == 0) && (timer.elapsed_milli() >= ts_->max_move_time(ctx))) {
#ifndef QUIET_MODE
cerr << "[I]: num_rounds: " << i << "/" << num_rounds_ << endl;
#endif
break;
}
MCTSNode *node = &root;
while (!node->can_add_child() && !node->is_terminal()) {
node = this->select_child(node);
}
// Add a new child node into the tree.
if (node->can_add_child()) {
node = &node->add_random_child();
}
// Simulate a random game from this node.
Player winner = this->simulate_random_game(*node->game_state_, ctx);
// Propagate scores back up the tree.
while (node != nullptr) {
node->record_win(winner);
node = node->parent_;
}
}
#ifndef QUIET_MODE
auto scored_moves = root.top_n(TOP_N_FIRST_LEVEL);
cerr << "[I]: Top " << SZ(scored_moves) << " moves:\n";
for (auto &score : scored_moves) {
cerr << "[I]: " << score << "\n";
auto sl_scored_moves = score.node_->top_n(TOP_N_SECOND_LEVEL);
cerr << "[I]:\t\t";
for (auto &score2 : sl_scored_moves) {
cerr << score2 << ",";
}
cerr << "\n";
}
cerr.flush();
#endif
const Move *best_move = nullptr;
double best_pct = -1.0;
for (auto &child : root.children_) {
double child_pct = child.winning_frac(game_state.get_turn());
if (child_pct > best_pct) {
best_pct = child_pct;
assert(child.move_.has_value());
best_move = &child.move_.value();
}
}
assert(best_move != nullptr);
bool is_winning = (best_pct > IS_WINNING_THRESHOLD) && (!sent_is_winning_) && (ctx.at(CTX_VAR::ROUND) >= MIN_ROUND_TO_CLAIM_IS_WINNING);
sent_is_winning_ = (sent_is_winning_ || is_winning);
#ifndef QUIET_MODE
timer.stop();
cerr << "[I]: Selected: " << IO::format_move(*best_move) << (is_winning ? "!" : "") << " in "
<< timer.elapsed_milli() << " ms." << endl;
#endif
return make_pair(*best_move, is_winning);
}
private:
/**
* Select a child according to the UCT metric.
* @param node : MCTSNode pointing to parent
* @return pointer to best MCTSNode child of this parent.
*/
MCTSNode *select_child(MCTSNode *const node) const noexcept {
uint32_t total_rollouts = node->num_rollouts_;
double log_rollouts = log(total_rollouts);
double best_score = -1.0;
MCTSNode *best_child = nullptr;
for (int i = 0; i < SZ(node->children_); i++) {
MCTSNode &child = node->children_[i];
double win_percentage = child.winning_frac(node->game_state_->get_turn());
double exploration_factor = sqrt(log_rollouts / child.num_rollouts_);
double uct_score = win_percentage + temperature_ * exploration_factor;
if (uct_score > best_score) {
best_score = uct_score;
best_child = &node->children_[i];
}
}
assert(best_child != nullptr);
return best_child;
}
[[nodiscard]] Player simulate_random_game(Board game, const Context &ctx) const noexcept {
RandomAgent white_bot(Player::WHITE, rng_, WHITE_USE_WEIGHT_ROLLOUT, false);
RandomAgent black_bot(Player::BLACK, rng_, BLACK_USE_WEIGHT_ROLLOUT, false);
RandomAgent *bot;
while (!game.is_over()) {
if (game.get_turn() == Player::WHITE) bot = &white_bot;
else
bot = &black_bot;
auto [bot_move, _] = bot->select_move(game, ctx);
std::ignore = _;// pleases compiler warning.
game.apply_move(bot_move);
}
return game.winner();
}
};// End of class MCTSAgent
void game_loop() {
random_device dev;
mt19937 rng(dev());
unique_ptr<TimeStrategy> ts = make_unique<RemainingTimeStrategy>(TOTAL_TIME_MILLIS);
unique_ptr<Agent> rnd_engine = make_unique<RandomAgent>(Player::BLACK, rng, false, true);
unique_ptr<Agent> mcts_engine = make_unique<MCTSAgent>(NUM_ROUNDS, TEMPERATURE, ts, Player::BLACK, rng);
Board board;
Context ctx;
int32_t &round_number = ctx[CTX_VAR::ROUND];
int32_t &elapsed_time = ctx[CTX_VAR::ELAPSED_TIME_MILLIS];
Timer timer;
Timer opponent_timer;
bool finished = false;
round_number = 0;
elapsed_time = 0;
while (not finished) {
string input = IO::readln();
timer.start();
auto parsed = IO::parse_input(input);
if (holds_alternative<GameCommand>(parsed)) {
GameCommand cmd = std::get<GameCommand>(parsed);
switch (cmd) {
case GameCommand::QUIT: {