-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompletion_hypergraph.cpp
More file actions
657 lines (615 loc) · 21.3 KB
/
Copy pathcompletion_hypergraph.cpp
File metadata and controls
657 lines (615 loc) · 21.3 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
#include <algorithm>
#include <array>
#include <bit>
#include <chrono>
#include <cstdint>
#include <deque>
#include <iostream>
#include <limits>
#include <memory>
#include <stdexcept>
#include <span>
#include <string>
#include <vector>
namespace {
int Size;
int Cells;
std::uint64_t Board;
std::uint64_t InitialBlack;
std::uint64_t InitialWhite;
std::uint64_t Empty;
int EmptyCells;
std::uint32_t CompactMask;
std::uint64_t Left;
std::uint64_t Right;
constexpr std::uint64_t bit(int point) { return std::uint64_t{1} << point; }
std::vector<std::uint64_t> make_neighbors()
{
std::vector<std::uint64_t> result(Cells);
constexpr std::array<std::pair<int, int>, 6> directions{{
{0, 1}, {0, -1}, {1, 0}, {-1, 0}, {-1, 1}, {1, -1}}};
for (int point = 0; point < Cells; ++point)
{
const int row = point / Size;
const int column = point % Size;
for (auto [dr, dc] : directions)
if (const int rr = row + dr, cc = column + dc;
rr >= 0 && rr < Size && cc >= 0 && cc < Size)
result[point] |= bit(rr * Size + cc);
}
return result;
}
std::vector<std::uint64_t> Neighbors;
bool connected(std::uint64_t stones, int color)
{
const std::uint64_t first = color == 0 ? bit(Size) - 1 : Left;
const std::uint64_t second = color == 0
? first << (Cells - Size) : Right;
std::uint64_t frontier = stones & first;
std::uint64_t seen = frontier;
while (frontier)
{
if (frontier & second)
return true;
std::uint64_t adjacent = 0;
for (auto work = frontier; work; work &= work - 1)
adjacent |= Neighbors[std::countr_zero(work)];
frontier = adjacent & stones & ~seen;
seen |= frontier;
}
return false;
}
std::vector<std::uint64_t> make_empty_cells()
{
std::vector<std::uint64_t> result;
result.reserve(EmptyCells);
for (auto work = Empty; work; work &= work - 1)
result.push_back(work & -work);
return result;
}
std::vector<std::uint64_t> EmptyCell;
std::uint64_t expand(std::uint32_t compact)
{
std::uint64_t result = 0;
for (auto work = compact; work; work &= work - 1)
result |= EmptyCell[std::countr_zero(work)];
return result;
}
std::string board_names(std::uint64_t stones)
{
std::string result;
for (auto work = stones; work; work &= work - 1)
{
const int point = std::countr_zero(work);
if (!result.empty())
result += ',';
result += char('a' + point % Size);
result += std::to_string(point / Size + 1);
}
return result.empty() ? "-" : result;
}
std::string names(std::uint32_t compact)
{
return board_names(expand(compact));
}
std::vector<std::uint32_t> minimal_completions()
{
std::array<std::uint32_t, 64> weight{};
for (int i = 0; i < EmptyCells; ++i)
weight[std::countr_zero(EmptyCell[i])] = std::uint32_t{1} << i;
std::array<std::vector<std::uint32_t>, 64> carriers;
std::deque<std::pair<int, std::uint32_t>> queue;
std::vector<std::uint32_t> result;
auto insert = [](auto& family, std::uint32_t carrier) {
for (std::uint32_t edge : family)
if (!(edge & ~carrier))
return false;
std::erase_if(family, [carrier](std::uint32_t edge) {
return !(carrier & ~edge);
});
family.push_back(carrier);
return true;
};
for (auto work = Left & ~InitialBlack; work; work &= work - 1)
{
const int point = std::countr_zero(work);
insert(carriers[point], weight[point]);
queue.emplace_back(point, weight[point]);
}
while (!queue.empty())
{
auto [point, carrier] = queue.front();
queue.pop_front();
if (std::find(carriers[point].begin(), carriers[point].end(), carrier)
== carriers[point].end())
continue;
bool finished = false;
for (std::uint32_t edge : result)
if (!(edge & ~carrier))
{
finished = true;
break;
}
if (finished)
continue;
if (bit(point) & Right)
{
insert(result, carrier);
continue;
}
for (auto work = Neighbors[point] & ~InitialBlack;
work; work &= work - 1)
{
const int next = std::countr_zero(work);
const auto extension = carrier | weight[next];
if (insert(carriers[next], extension))
queue.emplace_back(next, extension);
}
}
std::sort(result.begin(), result.end());
return result;
}
struct Hypergraph
{
std::uint32_t edge_block;
std::uint32_t edge_offset;
std::uint32_t edge_count;
std::uint32_t active;
std::array<std::uint8_t, 2> known{};
std::array<std::uint8_t, 2> wins{};
};
static_assert(sizeof(Hypergraph) == 20);
class HypergraphPool
{
public:
std::uint32_t intern(std::vector<std::uint32_t> edges)
{
// Packing preserves numeric edge order.
std::uint32_t support = 0;
for (std::uint32_t edge : edges)
support |= edge;
if (support & (support + 1))
{
std::array<std::uint32_t, 32> mapping{};
std::uint32_t target = 1;
for (auto work = support; work; work &= work - 1, target <<= 1)
mapping[std::countr_zero(work)] = target;
for (std::uint32_t& edge : edges)
{
std::uint32_t packed = 0;
for (auto work = edge; work; work &= work - 1)
packed |= mapping[std::countr_zero(work)];
edge = packed;
}
}
const std::uint32_t hash = edge_hash(edges);
if (index_hashes.empty())
resize_index(1024);
if (const std::uint32_t found = find(hash, edges);
found != Missing)
return found;
if constexpr (sizeof(std::size_t) > sizeof(std::uint32_t))
if (graphs.size() >= std::uint64_t{1} << 32)
throw std::overflow_error("too many distinct hypergraphs");
std::uint32_t active = 0;
for (std::uint32_t edge : edges)
active |= edge;
const auto id = static_cast<std::uint32_t>(graphs.size());
const auto [block, offset] = append_edges(edges);
graphs.push_back(Hypergraph{
block, offset, static_cast<std::uint32_t>(edges.size()), active});
if ((graphs.size() * 4) > (index_hashes.size() * 3))
resize_index(index_hashes.size() * 2);
insert_index(hash, id);
return id;
}
Hypergraph& operator[](std::uint32_t id)
{
return graphs[id];
}
std::span<const std::uint32_t> edges(std::uint32_t id) const
{
const Hypergraph& graph = graphs[id];
return {edge_blocks[graph.edge_block].data.get() + graph.edge_offset,
graph.edge_count};
}
std::uint64_t size() const { return graphs.size(); }
std::uint64_t bytes() const
{
std::uint64_t result = graphs.size() * sizeof(Hypergraph);
for (const EdgeBlock& block : edge_blocks)
result += std::uint64_t{block.capacity} * sizeof(std::uint32_t);
result += std::uint64_t{index_hashes.capacity()
+ index_ids.capacity()}
* sizeof(std::uint32_t);
return result;
}
private:
static constexpr std::uint32_t Missing =
std::numeric_limits<std::uint32_t>::max();
static constexpr std::uint32_t EdgeBlockWords = 1 << 20;
struct EdgeBlock
{
std::unique_ptr<std::uint32_t[]> data;
std::uint32_t capacity;
std::uint32_t used;
};
static std::uint64_t hash_edges(
std::span<const std::uint32_t> edges)
{
std::uint64_t value = edges.size();
for (std::uint32_t edge : edges)
{
value ^= edge + 0x9e3779b97f4a7c15ULL
+ (value << 6) + (value >> 2);
value ^= value >> 30;
value *= 0xbf58476d1ce4e5b9ULL;
value ^= value >> 27;
value *= 0x94d049bb133111ebULL;
value ^= value >> 31;
}
return value;
}
static std::uint32_t edge_hash(
std::span<const std::uint32_t> edges)
{
const std::uint32_t hash = hash_edges(edges);
return hash ? hash : 1;
}
std::uint32_t find(std::uint32_t hash,
std::span<const std::uint32_t> sought) const
{
const std::size_t mask = index_hashes.size() - 1;
std::size_t slot = hash & mask;
while (index_hashes[slot])
{
const std::uint32_t id = index_ids[slot];
if (index_hashes[slot] == hash
&& std::ranges::equal(edges(id), sought))
return id;
slot = (slot + 1) & mask;
}
return Missing;
}
void insert_index(std::uint32_t hash, std::uint32_t id)
{
const std::size_t mask = index_hashes.size() - 1;
std::size_t slot = hash & mask;
while (index_hashes[slot])
slot = (slot + 1) & mask;
index_hashes[slot] = hash;
index_ids[slot] = id;
}
void resize_index(std::size_t capacity)
{
std::vector<std::uint32_t> old_hashes = std::move(index_hashes);
std::vector<std::uint32_t> old_ids = std::move(index_ids);
index_hashes.assign(capacity, 0);
index_ids.resize(capacity);
for (std::size_t slot = 0; slot < old_hashes.size(); ++slot)
if (old_hashes[slot])
insert_index(old_hashes[slot], old_ids[slot]);
}
std::pair<std::uint32_t, std::uint32_t> append_edges(
std::span<const std::uint32_t> edges)
{
if (edges.size() > std::numeric_limits<std::uint32_t>::max())
throw std::overflow_error("hypergraph has too many edges");
const std::uint32_t count = static_cast<std::uint32_t>(edges.size());
if (edge_blocks.empty()
|| edge_blocks.back().capacity - edge_blocks.back().used < count)
{
if constexpr (sizeof(std::size_t) > sizeof(std::uint32_t))
if (edge_blocks.size() >= std::uint64_t{1} << 32)
throw std::overflow_error(
"too many hypergraph edge blocks");
const std::uint32_t capacity = std::max(EdgeBlockWords, count);
edge_blocks.push_back({
std::make_unique_for_overwrite<std::uint32_t[]>(capacity),
capacity, 0});
}
EdgeBlock& block = edge_blocks.back();
const std::uint32_t offset = block.used;
std::copy(edges.begin(), edges.end(), block.data.get() + offset);
block.used += count;
return {static_cast<std::uint32_t>(edge_blocks.size() - 1), offset};
}
std::deque<Hypergraph> graphs;
std::vector<EdgeBlock> edge_blocks;
std::vector<std::uint32_t> index_hashes;
std::vector<std::uint32_t> index_ids;
};
class Solver
{
public:
bool solve(std::uint32_t remaining,
const std::vector<std::uint32_t>& edges,
bool black_turn)
{
std::uint32_t active = 0;
for (std::uint32_t edge : edges)
active |= edge;
const std::uint32_t id = pool.intern(edges);
return win(id, (remaining & ~active) != 0, black_turn);
}
std::uint64_t states() const { return solved_states; }
std::uint64_t hypergraphs() const { return pool.size(); }
std::uint64_t bytes() const { return pool.bytes(); }
std::uint32_t winning_batch(
std::uint32_t remaining, std::vector<std::uint32_t> edges,
bool black_turn)
{
if (!solve(remaining, edges, black_turn))
return 0;
std::uint32_t batch = 0;
for (;;)
{
std::uint32_t active = 0;
for (std::uint32_t edge : edges)
active |= edge;
const std::uint32_t dead = remaining & ~active;
bool continued = false;
// Witnesses use physical labels; value queries pack them.
std::vector<std::uint32_t> choices;
if (dead)
choices.push_back(dead & -dead);
for (auto work = active; work; work &= work - 1)
choices.push_back(work & -work);
for (std::uint32_t cell : choices)
{
const std::uint32_t taken = cell & dead ? dead : cell;
std::vector<std::uint32_t> child;
if (cell & dead)
child = edges;
else if (black_turn)
{
child = black_child(edges, cell);
if (child.empty())
continue;
}
else
{
bool legal;
child = white_child(edges, cell, legal);
if (!legal)
continue;
}
const std::uint32_t rest = remaining & ~taken;
if (!solve(rest, child, !black_turn))
return batch | taken;
if (solve(rest, child, black_turn))
{
batch |= taken;
remaining = rest;
edges = std::move(child);
continued = true;
break;
}
}
if (!continued)
throw std::logic_error("winning state has no winning batch");
}
}
private:
static std::vector<std::uint32_t> white_child(
std::span<const std::uint32_t> edges, std::uint32_t cell,
bool& legal)
{
std::vector<std::uint32_t> reduced;
reduced.reserve(edges.size());
legal = true;
for (std::uint32_t edge : edges)
{
if (!(edge & cell))
continue;
if (edge == cell)
{
legal = false;
return {};
}
reduced.push_back(edge & ~cell);
}
// Only shortened edges can dominate unchanged edges.
std::vector<std::uint32_t> result = reduced;
for (std::uint32_t edge : edges)
{
if (edge & cell)
continue;
bool dominated = false;
for (std::uint32_t kept : reduced)
if (!(kept & ~edge))
{
dominated = true;
break;
}
if (!dominated)
result.push_back(edge);
}
std::inplace_merge(result.begin(), result.begin() + reduced.size(),
result.end());
return result;
}
static std::vector<std::uint32_t> black_child(
std::span<const std::uint32_t> edges, std::uint32_t cell)
{
std::vector<std::uint32_t> result;
result.reserve(std::count_if(
edges.begin(), edges.end(), [cell](std::uint32_t edge) {
return !(edge & cell);
}));
for (std::uint32_t edge : edges)
if (!(edge & cell))
result.push_back(edge);
return result;
}
bool win(std::uint32_t id, bool isolates, bool black_turn)
{
const int turn = black_turn ? 1 : 0;
const std::uint8_t state = std::uint8_t{1} << isolates;
if (pool[id].known[turn] & state)
return pool[id].wins[turn] & state;
// Dead cells preserve the continue/end predicate.
if (isolates)
{
const bool result = !win(id, false, !black_turn)
|| win(id, false, black_turn);
remember(id, turn, state, result);
return result;
}
const std::uint32_t active = pool[id].active;
for (auto work = active; work;)
{
const std::uint32_t cell = work & -work;
// Vertices with identical edge incidence are interchangeable.
std::uint32_t twins = active;
for (std::uint32_t edge : pool.edges(id))
{
twins &= edge & cell ? edge : ~edge;
if (twins == cell)
break;
}
work &= ~twins;
std::vector<std::uint32_t> child;
if (black_turn)
{
child = black_child(pool.edges(id), cell);
if (child.empty())
continue;
}
else
{
bool legal;
child = white_child(pool.edges(id), cell, legal);
if (!legal)
continue;
}
const std::uint32_t child_id = pool.intern(std::move(child));
if (!win(child_id, false, !black_turn)
|| win(child_id, false, black_turn))
{
remember(id, turn, state, true);
return true;
}
}
remember(id, turn, state, false);
return false;
}
void remember(std::uint32_t id, int turn, std::uint8_t state, bool win)
{
Hypergraph& graph = pool[id];
graph.known[turn] |= state;
if (win)
graph.wins[turn] |= state;
++solved_states;
}
HypergraphPool pool;
std::uint64_t solved_states = 0;
};
void initialize_board(int size)
{
if (size < 1 || size > 8)
throw std::invalid_argument("board size must be in 1..8");
Size = size;
Cells = Size * Size;
Board = Cells == 64 ? ~std::uint64_t{0} : bit(Cells) - 1;
Left = 0;
for (int row = 0; row < Size; ++row)
Left |= bit(row * Size);
Right = Left << (Size - 1);
Neighbors = make_neighbors();
}
std::uint64_t parse_cells(const std::string& text)
{
if (text == "-" || text.empty())
return 0;
std::uint64_t result = 0;
std::size_t start = 0;
for (;;)
{
const std::size_t end = text.find(',', start);
const std::string name = text.substr(
start, end == std::string::npos ? end : end - start);
if (name.size() < 2 || name[0] < 'a' || name[0] >= 'a' + Size)
throw std::invalid_argument("invalid cell: " + name);
int row = 0;
for (std::size_t index = 1; index < name.size(); ++index)
{
if (name[index] < '0' || name[index] > '9')
throw std::invalid_argument("invalid cell: " + name);
row = 10 * row + name[index] - '0';
}
if (row < 1 || row > Size)
throw std::invalid_argument("invalid cell: " + name);
const std::uint64_t cell = bit((row - 1) * Size + name[0] - 'a');
if (result & cell)
throw std::invalid_argument("duplicate cell: " + name);
result |= cell;
if (end == std::string::npos)
return result;
start = end + 1;
if (start == text.size())
throw std::invalid_argument("empty cell after comma");
}
}
void initialize_position(const std::string& black,
const std::string& white)
{
InitialBlack = parse_cells(black);
InitialWhite = parse_cells(white);
if ((InitialBlack & InitialWhite)
|| connected(InitialBlack, 0) || connected(InitialWhite, 1))
throw std::invalid_argument("initial position is terminal or overlaps");
Empty = Board & ~(InitialBlack | InitialWhite);
if (!connected(InitialWhite | Empty, 1))
throw std::invalid_argument("White has no completion");
EmptyCells = std::popcount(Empty);
if (EmptyCells > 32)
throw std::invalid_argument(
"completion construction supports at most 32 empty cells");
CompactMask = EmptyCells == 32 ? ~std::uint32_t{0}
: (std::uint32_t{1} << EmptyCells) - 1;
EmptyCell = make_empty_cells();
}
} // namespace
int main(int argc, char** argv)
{
try
{
if (argc != 5 || (std::string(argv[4]) != "black"
&& std::string(argv[4]) != "white"))
throw std::invalid_argument(
"usage: rexplus_hypergraph N BLACK_CELLS WHITE_CELLS "
"black|white (use - for an empty set)");
initialize_board(std::stoi(argv[1]));
initialize_position(argv[2], argv[3]);
const bool black_turn = std::string(argv[4]) == "black";
const auto started = std::chrono::steady_clock::now();
const auto edges = minimal_completions();
Solver solver;
const bool mover_wins = solver.solve(
CompactMask, edges, black_turn);
std::cout << "board_size " << Size << '\n'
<< "black " << board_names(InitialBlack) << '\n'
<< "white " << board_names(InitialWhite) << '\n'
<< "to_play " << (black_turn ? "black\n" : "white\n")
<< "result " << (mover_wins ? "win\n" : "loss\n")
<< "minimal_completions " << edges.size() << '\n'
<< "hypergraphs " << solver.hypergraphs() << '\n'
<< "states " << solver.states() << '\n'
<< "seconds "
<< std::chrono::duration<double>(
std::chrono::steady_clock::now() - started).count()
<< '\n';
if (mover_wins)
std::cout << "winning_batch "
<< names(solver.winning_batch(
CompactMask, edges, black_turn))
<< '\n';
return 0;
}
catch (const std::exception& error)
{
std::cerr << "error: " << error.what() << '\n';
return 2;
}
}