-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtwiddlers_test.go
392 lines (351 loc) · 7.43 KB
/
twiddlers_test.go
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
package sudoku
import (
"math"
"testing"
)
func TestTwiddleTwiddle(t *testing.T) {
twiddlerFMaker := func(returnValue float64) probabilityTwiddler {
return func(proposedStep *SolveStep, inProgressCompoundStep []*SolveStep, pastSteps []*CompoundSolveStep, previousGrid Grid) probabilityTweak {
return probabilityTweak(returnValue)
}
}
tests := []struct {
item *probabilityTwiddlerItem
expected probabilityTweak
description string
}{
{
&probabilityTwiddlerItem{
f: twiddlerFMaker(10.0),
weight: -1.0,
},
10.0,
"Large value negative weight",
},
{
&probabilityTwiddlerItem{
f: twiddlerFMaker(0.0),
weight: 4.0,
},
0.0,
"Normal with 0 return and normal weight",
},
{
&probabilityTwiddlerItem{
f: twiddlerFMaker(0.5),
weight: 4.0,
},
2.0,
"Normal with 0.5 return 4.0 weight",
},
}
for i, test := range tests {
result := test.item.Twiddle(nil, nil, nil, nil)
if result != test.expected {
t.Error("Got wrong result for item", i, test.description, "Got", result, "Expected", test.expected)
}
}
}
func TestTwiddleHumanLikelihood(t *testing.T) {
possibilities := []*SolveStep{
{
Technique: techniquesByName["Only Legal Number"],
},
{
Technique: techniquesByName["Guess"],
},
}
for i, step := range possibilities {
result := twiddleHumanLikelihood(step, nil, nil, nil)
expected := probabilityTweak(step.HumanLikelihood())
if result != expected {
t.Error("Got wrong twiddle for human likelihood at index", i, "got", result, "expected", expected)
}
}
}
func TestTwiddlePointingTargetOverlap(t *testing.T) {
grid := NewGrid()
tests := []struct {
lastStep *SolveStep
currentStep *SolveStep
expected probabilityTweak
description string
}{
{
&SolveStep{
TargetCells: row(0),
},
&SolveStep{
PointerCells: row(0),
},
0.000001,
"Full pointer/cell overlap",
},
{
&SolveStep{
TargetCells: row(0),
},
&SolveStep{
TargetCells: row(0),
},
0.010000000000000018,
"Full target/target overlap",
},
{
&SolveStep{
TargetCells: row(0),
},
&SolveStep{
TargetCells: CellRefSlice{
CellRef{0, 0},
},
},
0.7704938271604939,
"Single cell out of 9",
},
{
&SolveStep{
TargetCells: row(0).Intersection(block(0)),
},
&SolveStep{
TargetCells: CellRefSlice{
CellRef{0, 0},
},
},
0.4011111111111111,
"Single cell out of three",
},
{
&SolveStep{
TargetCells: row(0),
},
&SolveStep{
TargetCells: row(7),
},
1.0,
"Two rows no overlap",
},
{
&SolveStep{
TargetCells: row(0).Intersection(block(0)),
},
&SolveStep{
TargetCells: row(DIM - 1).Intersection(block(DIM - 1)),
},
1.0,
"Two three-cell rows no overlap",
},
{
&SolveStep{
TargetCells: CellRefSlice{
CellRef{0, 0},
},
},
&SolveStep{
TargetCells: CellRefSlice{
CellRef{0, 0},
},
},
0.010000000000000018,
"Two individual cells overlapping",
},
{
&SolveStep{
TargetCells: row(0),
},
&SolveStep{
TargetCells: col(0),
},
0.8747750865051903,
"Row and col intersecting at one point",
},
{
&SolveStep{
TargetCells: row(0),
},
&SolveStep{
TargetCells: block(0),
},
0.6084,
"First row and first block overlapping",
},
{
&SolveStep{
TargetCells: row(0).Intersection(block(0)),
},
&SolveStep{
TargetCells: block(0),
},
0.4011111111111111,
"First three cells and first block overlapping",
},
}
for i, test := range tests {
result := twiddlePointingTargetOverlap(test.currentStep, []*SolveStep{test.lastStep}, nil, grid)
if math.IsNaN(float64(result)) {
t.Error("Got NaN on test", i, test.description)
}
if math.Abs(float64(result-test.expected)) > 0.000001 {
t.Error("Test", i, "got wrong result. Got", result, "expected", test.expected, test.description)
}
}
}
func TestTwiddleCommonNumbers(t *testing.T) {
grid := MutableLoadSDK(TEST_GRID)
//Fill all of the 4's
solvedGrid := MutableLoadSDK(SOLVED_TEST_GRID)
for _, cell := range solvedGrid.Cells() {
if cell.Number() == 4 {
otherCell := cell.MutableInGrid(grid)
otherCell.SetNumber(4)
}
}
possibilities := []*SolveStep{
//Step with 2 filled
{
techniquesByName["Only Legal Number"],
CellRefSlice{{1, 0}},
IntSlice{8},
nil,
nil,
nil,
},
//Step with non-fill technique
{
techniquesByName["Hidden Pair Block"],
CellRefSlice{{1, 0}},
IntSlice{8},
nil,
nil,
nil,
},
//High valued 1
{
techniquesByName["Only Legal Number"],
CellRefSlice{{0, 4}},
IntSlice{5},
nil,
nil,
nil,
},
//Already-filled number
{
techniquesByName["Only Legal Number"],
CellRefSlice{{0, 4}},
IntSlice{4},
nil,
nil,
nil,
},
}
expected := []probabilityTweak{
0.7777777777777778,
0.0,
0.4444444444444444,
0.0,
}
for i, step := range possibilities {
result := twiddleCommonNumbers(step, nil, nil, grid)
if result != expected[i] {
t.Error("Twiddle Common Numbers wrong for", i, "got", result, "expected", expected[i])
}
}
}
func TestTwiddleChainedSteps(t *testing.T) {
//TODO: test other, harder cases as well.
grid := NewGrid()
lastStep := []*SolveStep{
{
nil,
CellRefSlice{{0, 0}},
nil,
nil,
nil,
nil,
},
}
possibilities := []*SolveStep{
{
nil,
CellRefSlice{
{1, 0},
},
nil,
nil,
nil,
nil,
},
{
nil,
CellRefSlice{
{2, 2},
},
nil,
nil,
nil,
nil,
},
{
nil,
CellRefSlice{
{7, 7},
},
nil,
nil,
nil,
nil,
},
}
expected := []probabilityTweak{
0.12316184623065915,
0.22758459260747887,
1.0,
}
lastResult := probabilityTweak(math.SmallestNonzeroFloat64)
for i, step := range possibilities {
result := twiddleChainedSteps(step, lastStep, nil, grid)
expectedResult := expected[i]
if result <= lastResult {
t.Error("Tweak Chained Steps Weights didn't tweak things in the right direction: ", result, "at", i)
}
lastResult = result
if math.Abs(float64(expectedResult-result)) > 0.00001 {
t.Error("Twiddle chained steps at", i, "got", result, "expected", expectedResult)
}
}
}
func TestTwiddlePreferFilledGroups(t *testing.T) {
grid := NewGrid()
keyCell := grid.MutableCell(0, 0)
step := &SolveStep{
TargetCells: CellRefSlice{
keyCell.Reference(),
},
Technique: techniquesByName["Only Legal Number"],
}
//TODO: instead of testing for the exact values, just make sure that every
//value is lower than the one before it.
//TODO: more exhaustive tests
testHelper := func(expected probabilityTweak, description string) {
result := twiddlePreferFilledGroups(step, nil, nil, grid)
if result != expected {
t.Error("Got wrong result for", description, "Got", result, "Expected", expected)
}
}
testHelper(0.7043478260869563, "Completely empty grid")
//Fill the rest of the block
for _, cell := range grid.MutableBlock(0).RemoveCells(CellSlice{keyCell}) {
cell.SetNumber(1)
}
testHelper(0.28695652173913044, "Full block, empty everything else")
//Fill the rest of the row, too
for _, cell := range grid.MutableRow(0).RemoveCells(CellSlice{keyCell}) {
cell.SetNumber(1)
}
testHelper(0.11086956521739132, "Full block and row, otherwise empty col")
//Fill the rest of the col, too.
for _, cell := range grid.MutableCol(0).RemoveCells(CellSlice{keyCell}) {
cell.SetNumber(1)
}
testHelper(0.0782608695652174, "Full block, row, col")
}