-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathncop.cpp
More file actions
457 lines (405 loc) · 15.7 KB
/
Copy pathncop.cpp
File metadata and controls
457 lines (405 loc) · 15.7 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
/* Solver for n-cop Shannon Switching game on complete graphs.
Cop and robber alternate choosing edges to add to their respective graphs.
In the generalized n-cop version, the cop can add up to n edges per turn.
The robber wins if it can create a path between two designated vertices (0 and 1)
using only edges in its graph. The cop can block edges by adding them to its own graph.
If the cop can prevent the robber from connecting vertices 0 and 1, the cop wins.
*/
#include <iostream>
#include <cstdint>
#include <cassert>
#include <vector>
#define TOP_LEFT 0x8000000000000000ULL
#include <immintrin.h>
using namespace std;
typedef __m256i Bitboard;
Bitboard get_cop_starting_bitboard_for_size_k_graph(int k) {
uint16_t data[16] = {0};
for (int i = 0; i < k; i++) {
data[i] = (1 << k) - 1;
}
return _mm256_xor_si256(_mm256_loadu_si256((__m256i const *) data), _mm256_set1_epi32(-1));
}
Bitboard add_edge(Bitboard graph, int u, int v) {
uint16_t data[16] = {0};
data[u] |= 1 << v;
data[v] |= 1 << u;
return _mm256_or_si256(_mm256_loadu_si256((__m256i const *) data), graph);
}
Bitboard remove_edge(Bitboard graph, int u, int v) {
uint16_t data[16] = {0};
data[u] |= 1 << v;
data[v] |= 1 << u;
return _mm256_andnot_si256(_mm256_loadu_si256((__m256i const *) data), graph);
}
void print_graph(Bitboard graph) {
uint16_t data[16];
_mm256_storeu_si256((__m256i *) data, graph);
for (int i = 15; i >= 0; i--) {
for (int j = 0; j < 16; j++) {
if (data[i] & (1ULL << j)) {
cout << "1 ";
} else {
cout << "0 ";
}
}
cout << endl;
}
cout << endl;
}
bool has_edge(Bitboard graph, int u, int v) {
uint16_t data[16];
_mm256_storeu_si256((__m256i *) data, graph);
return data[u] & (1ULL << v);
}
bool is_0_1_connected(Bitboard graph) {
uint16_t frontier = _mm256_extract_epi16(graph, 0);
uint16_t last = 0;
// fill out columns to frontier
__m256i matches = _mm256_set1_epi16(frontier);
while (last != frontier && !(frontier & 0x2)) {
// find rows that intersect with frontier
matches = _mm256_and_si256(matches, graph);
matches = _mm256_cmpeq_epi16(matches, _mm256_setzero_si256());
matches = _mm256_xor_si256(matches, _mm256_set1_epi32(-1));
// get their contents
matches = _mm256_and_si256(matches, graph);
if (_mm256_extract_epi16(matches, 1)) return true; // did we see the one row?
// merge into new columns
matches = _mm256_or_si256(_mm256_alignr_epi8(matches, matches, 2), matches);
matches = _mm256_or_si256(_mm256_alignr_epi8(matches, matches, 4), matches);
matches = _mm256_or_si256(_mm256_alignr_epi8(matches, matches, 8), matches);
matches = _mm256_or_si256(_mm256_alignr_epi8(matches, matches, 16), matches);
// forward propagate...
last = frontier;
frontier = _mm256_extract_epi16(matches, 0);
}
return frontier & 0x2;
}
struct GameState {
int graph_size;
int num_cops;
Bitboard cop;
Bitboard robber;
GameState(const int gs) {
graph_size = gs;
cop = get_cop_starting_bitboard_for_size_k_graph(graph_size);
robber = _mm256_setzero_si256();
}
};
bool did_robber_win(const GameState& graph) {
return is_0_1_connected(graph.robber);
}
bool did_cop_win(const GameState& graph) {
return !is_0_1_connected(~graph.cop);
}
bool is_move_legal(GameState graph, int u, int v) {
return !has_edge(graph.cop, u, v) && !has_edge(graph.robber, u, v) && u != v;
}
// Forward declare
int robbers_turn_evaluate(const GameState& graph, const int num_cops, const int depth);
int cops_turn_evaluate(const GameState& graph, const int num_cops, const int depth = 0) {
if(did_cop_win(graph)) return 1;
if(did_robber_win(graph)) return -1;
bool no_move_found = true;
if(no_move_found && num_cops >= 3) {
for (int u = 0; u < graph.graph_size; u++) {
for (int v = u + 1; v < graph.graph_size; v++) {
if (is_move_legal(graph, u, v)) {
for (int m = u; m < graph.graph_size; m++) {
int n_start = ((m == u) ? v : m) + 1;
for (int n = n_start; n < graph.graph_size; n++) {
if (is_move_legal(graph, m, n)) {
for (int a = m; a < graph.graph_size; a++) {
int b_start = ((a == m) ? n : a) + 1;
for (int b = b_start; b < graph.graph_size; b++) {
if (is_move_legal(graph, a, b)) {
no_move_found = false;
GameState new_graph = graph;
new_graph.cop = add_edge(new_graph.cop, u, v);
new_graph.cop = add_edge(new_graph.cop, m, n);
new_graph.cop = add_edge(new_graph.cop, a, b);
if (depth == 0) {
cout << "Cop removes edges (" << u << ", " << v << "), (" << m << ", " << n << ") and (" << a << ", " << b << ")";
}
int eval = robbers_turn_evaluate(new_graph, num_cops, depth + 1);
if (depth == 0 && eval == 1) cout << ", and wins!" << endl;
if (eval == 1) return 1;
}
}
}
}
}
}
}
}
}
}
if(no_move_found && num_cops >= 2) {
for (int u = 0; u < graph.graph_size; u++) {
for (int v = u + 1; v < graph.graph_size; v++) {
if (is_move_legal(graph, u, v)) {
for (int m = u; m < graph.graph_size; m++) {
int n_start = ((m == u) ? v : m) + 1;
for (int n = n_start; n < graph.graph_size; n++) {
if (is_move_legal(graph, m, n)) {
no_move_found = false;
GameState new_graph = graph;
new_graph.cop = add_edge(new_graph.cop, u, v);
new_graph.cop = add_edge(new_graph.cop, m, n);
if (depth == 0) {
cout << "Cop removes edges (" << u << ", " << v << ") and (" << m << ", " << n << ")";
}
int eval = robbers_turn_evaluate(new_graph, num_cops, depth + 1);
if (depth == 0 && eval == 1) cout << ", and wins!" << endl;
if (eval == 1) return 1;
}
}
}
}
}
}
}
if(no_move_found) {
for (int u = 0; u < graph.graph_size; u++) {
for (int v = u + 1; v < graph.graph_size; v++) {
if (is_move_legal(graph, u, v)) {
GameState new_graph = graph;
new_graph.cop = add_edge(new_graph.cop, u, v);
if (depth == 0) {
cout << "Cop removes edge (" << u << ", " << v << ")";
}
int eval = robbers_turn_evaluate(new_graph, num_cops, depth + 1);
if (depth == 0 && eval == 1) cout << ", and wins!" << endl;
if (eval == 1) return 1;
}
}
}
}
return -1;
}
int robbers_turn_evaluate(const GameState& graph, const int num_cops, const int depth) {
if(did_cop_win(graph)) return 1;
if(did_robber_win(graph)) return -1;
for (int u = 0; u < graph.graph_size; u++) {
for (int v = u + 1; v < graph.graph_size; v++) {
if (is_move_legal(graph, u, v)) {
GameState new_graph = graph;
new_graph.robber = add_edge(new_graph.robber, u, v);
if(cops_turn_evaluate(new_graph, num_cops, depth + 1) == -1) {
if (depth == 1) {
cout << ", but Robber can win by adding edge (" << u << ", " << v << ")" << endl;
}
return -1;
}
}
}
}
return 1;
}
void unit_tests() {
// Silence output during unit tests
streambuf* orig_buf = cout.rdbuf();
cout.rdbuf(nullptr);
{
Bitboard cop_graph = get_cop_starting_bitboard_for_size_k_graph(4);
Bitboard robber_graph = _mm256_setzero_si256();
// Test adding edges
cop_graph = add_edge(cop_graph, 0, 2);
robber_graph = add_edge(robber_graph, 1, 3);
// Test edge existence
assert(has_edge(cop_graph, 0, 2));
assert(!has_edge(cop_graph, 1, 3));
assert(has_edge(robber_graph, 1, 3));
assert(!has_edge(robber_graph, 0, 2));
assert(!has_edge(cop_graph, 0, 1));
assert(!has_edge(robber_graph, 0, 1));
// Test removing edges
cop_graph = remove_edge(cop_graph, 0, 2);
assert(!has_edge(cop_graph, 0, 2));
// Test connectivity
assert(!is_0_1_connected(robber_graph));
robber_graph = add_edge(robber_graph, 2, 3);
assert(!is_0_1_connected(robber_graph));
robber_graph = add_edge(robber_graph, 2, 0);
assert(is_0_1_connected(robber_graph));
}
{
GameState state(6);
state.cop = add_edge(state.cop, 0, 1);
state.cop = add_edge(state.cop, 0, 2);
state.robber = add_edge(state.robber, 4, 5);
state.cop = add_edge(state.cop, 0, 3);
state.cop = add_edge(state.cop, 0, 4);
state.robber = add_edge(state.robber, 0, 5);
state.cop = add_edge(state.cop, 1, 4);
state.cop = add_edge(state.cop, 1, 5);
state.robber = add_edge(state.robber, 1, 2);
assert(cops_turn_evaluate(state, 2, 0) == 1);
}
{
// Test that the cop wins in a 4-vertex clique with 1 cop
GameState state(4);
assert(cops_turn_evaluate(state, 1, 0) == -1);
state.cop = add_edge(state.cop, 0, 1);
state.robber = add_edge(state.robber, 0, 2);
assert(cops_turn_evaluate(state, 1, 0) == -1);
}
{
Bitboard robber = add_edge(_mm256_setzero_si256(), 0, 5);
assert(!is_0_1_connected(robber));
robber = add_edge(robber, 1, 4);
assert(!is_0_1_connected(robber));
robber = add_edge(robber, 2, 3);
assert(!is_0_1_connected(robber));
robber = add_edge(robber, 5, 2);
assert(!is_0_1_connected(robber));
robber = add_edge(robber, 5, 3);
assert(!is_0_1_connected(robber));
robber = add_edge(robber, 4, 2);
assert(is_0_1_connected(robber));
}
{
Bitboard robber = add_edge(_mm256_setzero_si256(), 0, 7);
assert(!is_0_1_connected(robber));
robber = add_edge(robber, 1, 6);
assert(!is_0_1_connected(robber));
robber = add_edge(robber, 2, 5);
assert(!is_0_1_connected(robber));
robber = add_edge(robber, 3, 4);
assert(!is_0_1_connected(robber));
robber = add_edge(robber, 7, 3);
assert(!is_0_1_connected(robber));
robber = add_edge(robber, 3, 5);
assert(!is_0_1_connected(robber));
robber = add_edge(robber, 5, 1);
assert(is_0_1_connected(robber));
}
cout.rdbuf(orig_buf);
cout << "All unit tests passed!" << endl << endl;
}
int run_game_test(const int graph_size, const int num_cops) {
GameState initial_state(graph_size);
int result = cops_turn_evaluate(initial_state, num_cops);
if (result == 1) {
cout << "Cop wins on graph size " << graph_size << endl;
} else {
cout << "Robber wins on graph size " << graph_size << endl;
}
return result;
}
int play_as_cop_against_computer(const int graph_size, const int num_cops) {
GameState state(graph_size);
while (true) {
// Cop's turn
for(int c = 0; c < num_cops; c++) {
int u, v;
cout << "You have " << num_cops - c << " deletions left this turn. Enter an edge to remove (u v): ";
cin >> u >> v;
if (!is_move_legal(state, u, v)) {
cout << "Illegal move. Try again." << endl;
c--;
continue;
}
state.cop = add_edge(state.cop, u, v);
if(did_cop_win(state)) {
cout << "Congratulations, you win!" << endl;
return 1;
}
}
// Robber's turn
bool move_made = false;
vector<pair<int, int>> legal_moves;
for (int u = 0; u < state.graph_size; u++) {
for (int v = u + 1; v < state.graph_size; v++) {
if (is_move_legal(state, u, v)) {
legal_moves.emplace_back(u, v);
GameState new_state = state;
new_state.robber = add_edge(new_state.robber, u, v);
if(cops_turn_evaluate(new_state, num_cops, 1) == -1) {
cout << "Robber adds edge (" << u << ", " << v << ")" << endl;
state.robber = add_edge(state.robber, u, v);
move_made = true;
goto after_robber_move;
}
}
}
}
after_robber_move:
if(!move_made) {
// Pick a random legal move if no winning move found
assert(!legal_moves.empty()); // if so, cop should have already won
pair<int, int> move = legal_moves[rand() % legal_moves.size()];
cout << "Robber adds edge (" << move.first << ", " << move.second << ")" << endl;
state.robber = add_edge(state.robber, move.first, move.second);
}
if(did_robber_win(state)) {
cout << "Sorry, the robber wins!" << endl;
return -1;
}
}
}
int main_play_mode(int argc, char* argv[]) {
int num_cops;
if (argv[1][0] == '1') {
num_cops = 1;
} else if (argv[1][0] == '2') {
num_cops = 2;
} else if (argv[1][0] == '3') {
num_cops = 3;
} else {
cout << "Invalid number of cops. Must be 1, 2, or 3." << endl;
return 1;
}
int graph_size = atoi(argv[2]);
if (graph_size < 2 || graph_size > 16) {
cout << "Invalid graph size. Must be between 2 and 16 inclusive." << endl;
return 1;
}
string role = argv[3];
if (role != "cop" && role != "robber") {
cout << "Invalid role. Must be 'cop' or 'robber'." << endl;
return 1;
}
if (role == "cop") {
play_as_cop_against_computer(graph_size, num_cops);
} else {
cout << "Playing as robber is not implemented yet." << endl;
}
return 0;
}
int main_eval_mode(int argc, char* argv[]) {
int num_cops;
if (argv[1][0] == '1') {
num_cops = 1;
} else if (argv[1][0] == '2') {
num_cops = 2;
} else if (argv[1][0] == '3') {
num_cops = 3;
} else {
cout << "Invalid number of cops. Must be 1, 2, or 3." << endl;
return 1;
}
unit_tests();
int i = 1;
while (true) {
int result = run_game_test(i, num_cops);
if (result == -1) break;
if(i == 8) {
break;
}
i++;
}
return 0;
}
int main(int argc, char* argv[]) {
if (argc != 2 && argc != 4) {
cout << "To evaluate for n cops : " << argv[0] << " <num_cops>" << endl;
cout << "To play as cop or robber : " << argv[0] << " <num_cops> <graph_size> <cop/robber>" << endl;
return 1;
} else if (argc == 4) {
main_play_mode(argc, argv);
} else {
main_eval_mode(argc, argv);
}
}