-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExactCover.cpp
More file actions
320 lines (261 loc) · 7.77 KB
/
ExactCover.cpp
File metadata and controls
320 lines (261 loc) · 7.77 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
#include "ExactCover.h"
#include "ExactCover.h"
#include "ExactCover.h"
#include <limits>
#include <iostream>
#undef VERBOSE_DEBUG
template<typename Container>
void EnsureCapacity(Container& container, std::size_t required = 1)
{
uint64_t oldSize = container.size();
uint64_t oldCapacity = container.capacity();
uint64_t newCapacity = oldSize + required;
if (newCapacity < oldCapacity)
{
newCapacity = std::min<std::size_t>(newCapacity,
(oldCapacity * 3 + 1) / 2);
newCapacity = std::max<std::size_t>(newCapacity, 4);
container.reserve(newCapacity);
}
}
struct ExactCover::Entry {
id_t left, right, up, down;
id_t constraint = std::numeric_limits<id_t>::max();
id_t move = std::numeric_limits<id_t>::max();
Entry(id_t id)
: left(id), right(id), up(id), down(id)
{
}
};
struct ExactCover::Move {
std::string name;
id_t head = 0;
Move(std::string name)
: name(std::move(name))
{
}
};
struct ExactCover::Constraint {
std::string name;
id_t head = 0;
std::size_t count = 0;
Constraint(std::string name)
: name(std::move(name))
{
}
};
ExactCover::ExactCover()
{
constraintHead = constraints.size();
constraints.emplace_back("MandatoryConstraintHead");
auto& chead = constraints[constraintHead];
constraints[constraintHead].head = MakeEntry();
entries[constraints[constraintHead].head].constraint = constraintHead;
moveHead = moves.size();
moves.emplace_back("MoveHead");
moves[moveHead].head = MakeEntry();
entries[moves[moveHead].head].move = moveHead;
}
ExactCover::~ExactCover()
{
}
ExactCover::ExactCover(const ExactCover&) = default;
ExactCover& ExactCover::operator=(const ExactCover&) = default;
void ExactCover::SolutionSize(std::size_t size)
{
solutionSize = size;
}
ExactCover::id_t ExactCover::MakeEntry()
{
EnsureCapacity(entries);
id_t id = entries.size();
entries.emplace_back(id);
return id;
}
ExactCover::id_t ExactCover::AddConstraint(const std::string& name, bool mandatory)
{
EnsureCapacity(constraints);
id_t id = constraints.size();
constraints.emplace_back(name);
auto entryid = MakeEntry();
constraints[id].head = entryid;
entries[entryid].constraint = id;
if (mandatory) {
auto& entry = entries[entryid];
entry.left = entries[constraints[constraintHead].head].left;
entry.right = constraints[constraintHead].head;
Link(entryid);
}
return id;
}
const std::string& ExactCover::ConstraintName(id_t id)
{
return constraints[id].name;
}
ExactCover::id_t ExactCover::AddMove(const std::string& name)
{
EnsureCapacity(moves);
id_t id = moves.size();
moves.emplace_back(name);
auto entryid = MakeEntry();
moves[id].head = entryid;
auto& mhead = entries[moveHead];
auto& mentry = entries[entryid];
mentry.move = id;
mentry.up = mhead.up;
mentry.down = moveHead;
LinkVert(entryid);
return id;
}
const std::string& ExactCover::MoveName(id_t id)
{
return moves[id].name;
}
void ExactCover::ConstrainMove(id_t move, id_t constraint)
{
auto id = MakeEntry();
auto& entry = entries[id];
entry.constraint = constraint;
entry.move = move;
++constraints[constraint].count;
auto cheadid = constraints[constraint].head;
entry.up = entries[cheadid].up;
entry.down = cheadid;
auto mheadid = moves[move].head;
entry.left = entries[mheadid].left;
entry.right = mheadid;
Link(id);
LinkVert(id);
}
std::uint32_t ExactCover::Solve(std::deque<solution_t>& spud)
{
solution_t curSolution;
curSolution.reserve(solutionSize);
RecursiveSolve(curSolution, spud);
return spud.size();
}
void
ExactCover::CoverColumn(id_t constraint)
{
auto& col = constraints[constraint];
auto& centry = entries[col.head];
Unlink(col.head);
for (auto i = centry.down; i != col.head; i = entries[i].down) {
auto rheadid = i;
auto& rentry = entries[rheadid];
for (auto j = rentry.right; j != rheadid; j = entries[j].right) {
if (entries[j].constraint != std::numeric_limits<id_t>::max()) {
UnlinkVert(j);
--constraints[entries[j].constraint].count;
}
}
}
}
void
ExactCover::UncoverColumn(id_t constraint)
{
auto& col = constraints[constraint];
auto& centry = entries[col.head];
for (auto i = centry.up; i != col.head; i = entries[i].up) {
// auto rheadid = moves[entries[i].move].head;
auto rheadid = i;
auto& rentry = entries[rheadid];
for (auto j = rentry.left; j != rheadid; j = entries[j].left) {
if (entries[j].constraint != std::numeric_limits<id_t>::max()) {
LinkVert(j);
++constraints[entries[j].constraint].count;
}
}
}
Link(col.head);
}
void
ExactCover::RecursiveSolve(solution_t& curSolution, std::deque<solution_t>& spud)
{
#ifdef VERBOSE_DEBUG
auto depth = curSolution.size();
#endif
// Choose a constraint.
id_t c = constraintHead;
auto chead = constraints[c].head;
std::size_t minSize = std::numeric_limits<size_t>::max();
for (id_t i = entries[chead].right; i != chead; i = entries[i].right) {
if (constraints[entries[i].constraint].count < minSize) {
c = entries[i].constraint;
minSize = constraints[c].count;
}
}
if (!minSize) {
// Unsatisfiable constraint.
#ifdef VERBOSE_DEBUG
std::cerr << depth << ": Unsatisfiable constraint " << constraints[c].name << "\n";
for (auto m : curSolution) {
std::cerr << ' ' << moves[m].name;
}
std::cerr << '\n';
#endif
return;
}
// If all constraints are satisfied, we have a solution.
if (c == constraintHead) {
#ifdef VERBOSE_DEBUG
std::cerr << depth << ": Found solution\n";
#endif
spud.push_back(curSolution);
return;
}
// Otherwise, try to satisfy this constraint.
#ifdef VERBOSE_DEBUG
std::cerr << depth << ": Using constraint " << constraints[c].name << " (size " << minSize << ")\n";
#endif
CoverColumn(c);
chead = constraints[c].head;
for (id_t i = entries[chead].down; i != chead; i = entries[i].down) {
auto move = entries[i].move;
#ifdef VERBOSE_DEBUG
std::cerr << depth << ": Trying move " << moves[move].name << '\n';
#endif
curSolution.emplace_back(move);
for (id_t j = entries[i].right; j != i; j = entries[j].right) {
if (entries[j].constraint != std::numeric_limits<id_t>::max()) {
// std::cerr << depth << ": Eliminating constraint " << constraints[entries[j].constraint].name << '\n';
CoverColumn(entries[j].constraint);
}
}
RecursiveSolve(curSolution, spud);
#ifdef VERBOSE_DEBUG
std::cerr << depth << ": Unwinding move " << moves[move].name << '\n';
#endif
for (id_t j = entries[i].left; j != i; j = entries[j].left) {
if (entries[j].constraint != std::numeric_limits<id_t>::max()) {
UncoverColumn(entries[j].constraint);
}
}
curSolution.pop_back();
}
UncoverColumn(c);
}
void ExactCover::Unlink(id_t id)
{
auto& entry = entries[id];
entries[entry.right].left = entry.left;
entries[entry.left].right = entry.right;
}
void ExactCover::Link(id_t id)
{
auto& entry = entries[id];
entries[entry.right].left = id;
entries[entry.left].right = id;
}
void ExactCover::LinkVert(id_t id)
{
auto& entry = entries[id];
entries[entry.down].up = id;
entries[entry.up].down = id;
}
void ExactCover::UnlinkVert(id_t id)
{
auto& entry = entries[id];
entries[entry.down].up = entry.up;
entries[entry.up].down = entry.down;
}