-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdlx_solver.kt
More file actions
256 lines (219 loc) · 6.95 KB
/
dlx_solver.kt
File metadata and controls
256 lines (219 loc) · 6.95 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
abstract class DLXSolver<in R : Any, A : Any>(
requirements: List<R>,
actions: Map<A, List<R>>,
optionalRequirements: List<R> = emptyList()
) {
private val solution: MutableList<A> = mutableListOf()
private val allSolutions: MutableList<List<A>> = mutableListOf()
private val matrixRoot = DLXCell(Unit)
private val optionalRequirements = optionalRequirements.toSet()
private val columnHeaders = (requirements + optionalRequirements).associateWith { requirement -> DLXCell(requirement) }
private val rowHeaders = actions.keys.associateWith { action -> DLXCell(action) }
private val history = mutableListOf(mutableSetOf<Any>())
private var solutionIsValid = true
init {
for (columnHeader in columnHeaders) {
matrixRoot.attachHorizontal(columnHeader.value)
}
for ((action, satisfiedRequirements) in actions) {
val previousCell = rowHeaders[action] ?: throw IllegalStateException()
for (requirement in satisfiedRequirements) {
val cHeader = columnHeaders[requirement]
val rHeader = previousCell
val nextCell = DLXCell("${cHeader?.content}/${rHeader.content}").apply {
columnHeader = columnHeaders[requirement] ?: throw IllegalStateException("Action - $action expects not specified requirement - $requirement")
rowHeader = previousCell
columnHeader.attachVertical(this)
columnHeader.size++
}
previousCell.attachHorizontal(nextCell)
}
}
}
private var breaking = false
private var counter = 0
fun solve() {
counter++
var bestColumn = matrixRoot
var bestValue = Int.MAX_VALUE
var n = matrixRoot.nextX
while (n != matrixRoot) {
if (n.content !in optionalRequirements) {
val value = requirementSortCriteria(n, n.content as R)
if (bestColumn == matrixRoot || value < bestValue) {
bestColumn = n
bestValue = value
}
n = n.nextX
} else {
n = matrixRoot
}
}
if (bestColumn == matrixRoot) {
if (solutionIsValid) {
if (processSolution(solution)) { breaking = true }
}
allSolutions += solution.map { it }
} else {
val actions = mutableListOf<DLXCell>()
n = bestColumn.nextY
while (n != bestColumn) {
actions += n
n = n.nextY
}
history.add(history.last().toMutableSet())
for (node in actions.sortedBy { actionSortCriteria(it, it.rowHeader.content as A) }) {
select(node)
if (solutionIsValid) {
solve()
}
if (breaking) {
checkFinish()
return
}
deselect(node)
solutionIsValid = true
}
history.removeAt(history.lastIndex)
}
checkFinish()
}
private fun checkFinish() {
// added to break recursion
counter--
if (counter == 0) onFinished(allSolutions)
}
private fun select(cell: DLXCell) {
cell.select()
solution.add(cell.rowHeader.content as? A ?: throw IllegalStateException())
processRowSelection(cell.rowHeader, cell.rowHeader.content as? A ?: throw IllegalStateException())
}
private fun deselect(cell: DLXCell) {
cell.unselect()
solution.removeLast()
processRowDeselection(cell.rowHeader, cell.rowHeader.content as? A ?: throw IllegalStateException())
}
/**
* Call this function during [processRowSelection] in order
* to remember provided [data]. If there are being same [data]
* stored in same depth of search, solution is automatically
* discarded.
*/
fun remember(data: Any) {
if (data in history.last()) {
solutionIsValid = false
} else {
history.last().add(data)
}
}
fun markAsInvalid() {
solutionIsValid = false
}
open fun actionSortCriteria(rowHeader: DLXCell, action: A) = 0
open fun requirementSortCriteria(columnHeader: DLXCell, requirement: R) = columnHeader.size
open fun processRowSelection(row: DLXCell, action: A) {}
open fun processRowDeselection(row: DLXCell, action: A) {}
/**
* Called when solution is found. If there are multiple solutions
* this function will be called for each of them.
*
* @param solution current solution
* @return true to stop searching
*/
open fun processSolution(solution: List<A>): Boolean = false
/**
* All solutions found
*
* @param solutions found
*/
open fun onFinished(solutions: List<List<A>>) {}
}
class DLXCell(val content: Any) {
var prevX: DLXCell = this
var nextX: DLXCell = this
var prevY: DLXCell = this
var nextY: DLXCell = this
var size: Int = 0
var columnHeader = this
var rowHeader = this
fun removeX() {
prevX.nextX = nextX
nextX.prevX = prevX
}
fun removeY() {
prevY.nextY = nextY
nextY.prevY = prevY
}
fun restoreX() {
prevX.nextX = this
nextX.prevX = this
}
fun restoreY() {
prevY.nextY = this
nextY.prevY = this
}
fun attachHorizontal(other: DLXCell) {
val n = prevX
other.prevX = n
n.nextX = other
prevX = other
other.nextX = this
}
fun attachVertical(other: DLXCell) {
val n = prevY
other.prevY = n
n.nextY = other
prevY = other
other.nextY = this
}
fun removeColumn() {
removeX()
var node = nextY
while (node != this) {
node.removeRow()
node = node.nextY
}
}
fun restoreColumn() {
var node = prevY
while (node != this) {
node.restoreRow()
node = node.prevY
}
restoreX()
}
fun removeRow() {
var node = nextX
while (node != this) {
node.columnHeader.size--
node.removeY()
node = node.nextX
}
}
fun restoreRow() {
var node = prevX
while(node != this) {
node.columnHeader.size++
node.restoreY()
node = node.prevX
}
}
fun select() {
var node = this
do {
node.removeY()
node.columnHeader.removeColumn()
node = node.nextX
} while (node != this)
}
fun unselect() {
var node = prevX
while(node != this) {
node.columnHeader.restoreColumn()
node.restoreY()
node = node.prevX
}
node.columnHeader.restoreColumn()
node.restoreY()
}
}