-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhs_difficulty_test.go
346 lines (298 loc) · 8.77 KB
/
hs_difficulty_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
package sudoku
import (
"math"
"reflect"
"strings"
"testing"
)
var sampleSolveDirections SolveDirections
func init() {
sampleSolveDirections = SolveDirections{
nil,
[]*CompoundSolveStep{
{
FillStep: &SolveStep{
techniquesByName["Necessary In Row"],
nil,
nil,
nil,
nil,
nil,
},
},
{
FillStep: &SolveStep{
techniquesByName["Guess"],
nil,
nil,
nil,
nil,
nil,
},
},
{
PrecursorSteps: []*SolveStep{
{
techniquesByName["Naked Pair Block"],
nil,
nil,
nil,
nil,
nil,
},
},
FillStep: &SolveStep{
techniquesByName["Guess"],
nil,
nil,
nil,
nil,
nil,
},
},
},
}
}
func TestSetDifficultyModel(t *testing.T) {
currentModel := difficultySignalWeights
currentModelHash := DifficultyModelHash()
newModel := map[string]float64{
"a": 0.5,
"b": 0.6,
}
newModelExpectedHash := "2EE69110CFF4112E325A099D3B83DA0F7B2DC737"
LoadDifficultyModel(newModel)
if !reflect.DeepEqual(difficultySignalWeights, newModel) {
t.Error("LoadDifficultyModel didn't set model")
}
if currentModelHash == DifficultyModelHash() {
t.Error("Model hash didn't change when setting new model")
}
if DifficultyModelHash() != newModelExpectedHash {
t.Error("Model hash wasn't what was expected. Got", DifficultyModelHash(), "expected", newModelExpectedHash)
}
LoadDifficultyModel(currentModel)
if DifficultyModelHash() != currentModelHash {
t.Error("Setting back to old model didn't set back to old hash")
}
}
func TestHintDirections(t *testing.T) {
grid := NewGrid()
shortSolveDirections := SolveDirections{
grid,
[]*CompoundSolveStep{
{
FillStep: &SolveStep{
techniquesByName["Necessary In Row"],
CellRefSlice{
CellRef{2, 3},
},
IntSlice{4},
nil,
nil,
nil,
},
},
},
}
descriptions := strings.Join(shortSolveDirections.Description(), " ")
shortGolden := "Based on the other numbers you've entered, (2,3) can only be a 4. How do we know that? We put 4 in cell (2,3) because 4 is required in the 2 row, and 3 is the only column it fits."
if descriptions != shortGolden {
t.Error("Got wrong description for hint. Got", descriptions, "wanted", shortGolden)
}
multiStepSolveDirections := SolveDirections{
grid,
[]*CompoundSolveStep{
{
PrecursorSteps: []*SolveStep{
{
techniquesByName["Naked Pair Block"],
CellRefSlice{
CellRef{2, 3},
CellRef{2, 3},
},
IntSlice{4, 5},
CellRefSlice{
CellRef{3, 4},
CellRef{4, 5},
},
IntSlice{2, 3},
nil,
},
{
techniquesByName["Naked Pair Block"],
CellRefSlice{
CellRef{3, 2},
CellRef{3, 2},
},
IntSlice{4, 5},
CellRefSlice{
CellRef{5, 5},
CellRef{4, 6},
},
IntSlice{2, 3},
nil,
},
},
FillStep: &SolveStep{
techniquesByName["Necessary In Row"],
CellRefSlice{
CellRef{2, 3},
},
IntSlice{4},
nil,
nil,
nil,
},
},
},
}
descriptions = strings.Join(multiStepSolveDirections.Description(), " ")
longGolden := "Based on the other numbers you've entered, (2,3) can only be a 4. How do we know that? We can't fill any cells right away so first we need to cull some possibilities. First, we remove the possibilities 4 and 5 from cells (2,3) and (2,3) because 4 and 5 are only possible in (3,4) and (4,5), which means that they can't be in any other cell in block 1. Next, we remove the possibilities 4 and 5 from cells (3,2) and (3,2) because 4 and 5 are only possible in (5,5) and (4,6), which means that they can't be in any other cell in block 3. Finally, we put 4 in cell (2,3) because 4 is required in the 2 row, and 3 is the only column it fits."
if descriptions != longGolden {
t.Error("Got wrong description for hint. Got", descriptions, "wanted", longGolden)
}
}
//TODO: the other solvedirections tests should be in this file.
func TestDifficultySignals(t *testing.T) {
signals := DifficultySignals{"a": 1.0, "b": 5.0}
other := DifficultySignals{"a": 3.2, "c": 6.0}
golden := DifficultySignals{"a": 3.2, "b": 5.0, "c": 6.0}
signals.add(other)
if !reflect.DeepEqual(signals, golden) {
t.Error("Signals when added didn't have right values. Got", signals, "expected", golden)
}
}
func TestSumDifficultySignals(t *testing.T) {
signals := DifficultySignals{
"a": 0.5,
"b": 1.0,
"c": 0.6,
}
other := DifficultySignals{
"b": 2.0,
"d": 1.0,
}
golden := DifficultySignals{
"a": 0.5,
"b": 3.0,
"c": 0.6,
"d": 1.0,
}
signals.sum(other)
if !reflect.DeepEqual(signals, golden) {
t.Error("Signas when summed didn't have right values. Got", signals, "expected", golden)
}
}
func TestSolveDirectionsSignals(t *testing.T) {
result := sampleSolveDirections.Signals()
golden := DifficultySignals{}
for _, techniqueName := range AllTechniqueVariants {
golden[techniqueName+" Count"] = 0.0
golden[techniqueName+" Percentage"] = 0.0
}
golden["Guess Count"] = 2.0
golden["Necessary In Row Count"] = 1.0
golden["Naked Pair Block Count"] = 1.0
golden["Number of Steps"] = 4.0
golden["Percentage Fill Steps"] = 0.75
golden["Number Unfilled Cells"] = 3.0
golden["Steps Until Nonfill"] = 2.0
golden["Guess Percentage"] = 0.5
golden["Necessary In Row Percentage"] = 0.25
golden["Naked Pair Block Percentage"] = 0.25
golden["Average PrecursorSteps Length"] = 0.3333333333333333
golden["Longest PrecursorSteps Length"] = 1.0
if !reflect.DeepEqual(result, golden) {
t.Error("SolveDirections.Signals on sampleSolveDirections didn't return right value. Got: ", result, " expected: ", golden)
}
//We're going to swap out the real difficulty signal weights for the test.
realWeights := difficultySignalWeights
defer func() {
difficultySignalWeights = realWeights
}()
difficultySignalWeights = map[string]float64{
"Constant": 0.5,
"Guess Count": -0.09,
"Necessary In Row Count": -0.07,
"Naked Pair Block Count": 0.13,
"Number of Steps": 0.11,
}
difficulty := result.difficulty()
expectedDifficulty := 0.82
if math.Abs(difficulty-expectedDifficulty) > 0.00000000001 {
t.Error("Got wrong difficulty from baked signals: ", difficulty, "expected", 0.82)
}
}
func TestTechniqueSignal(t *testing.T) {
result := signalTechnique(sampleSolveDirections)
golden := DifficultySignals{}
for _, techniqueName := range AllTechniqueVariants {
golden[techniqueName+" Count"] = 0.0
}
golden["Guess Count"] = 2.0
golden["Necessary In Row Count"] = 1.0
golden["Naked Pair Block Count"] = 1.0
if !reflect.DeepEqual(result, golden) {
t.Error("Technique signal didn't work as expected. Got", result, "expected", golden)
}
}
func TestTechniqueSignalPercentage(t *testing.T) {
result := signalTechniquePercentage(sampleSolveDirections)
golden := DifficultySignals{}
for _, techniqueName := range AllTechniqueVariants {
golden[techniqueName+" Percentage"] = 0.0
}
golden["Guess Percentage"] = 0.5
golden["Necessary In Row Percentage"] = 0.25
golden["Naked Pair Block Percentage"] = 0.25
if !reflect.DeepEqual(result, golden) {
t.Error("Technique signal percentage didn't work as expected. Got", result, "expected", golden)
}
}
func TestSignalNumberOfSteps(t *testing.T) {
result := signalNumberOfSteps(sampleSolveDirections)
golden := DifficultySignals{
"Number of Steps": 4.0,
}
if !reflect.DeepEqual(result, golden) {
t.Error("Number of steps signal didn't work as expected. Got ", result, "expected", golden)
}
}
func TestSignalPercentageFillSteps(t *testing.T) {
result := signalPercentageFilledSteps(sampleSolveDirections)
golden := DifficultySignals{
"Percentage Fill Steps": 0.75,
}
if !reflect.DeepEqual(result, golden) {
t.Error("Percentage fill steps didn't work as expected. Got ", result, " expected ", golden)
}
}
func TestSignalNumberUnfilled(t *testing.T) {
result := signalNumberUnfilled(sampleSolveDirections)
golden := DifficultySignals{
"Number Unfilled Cells": 3.0,
}
if !reflect.DeepEqual(result, golden) {
t.Error("Number unfilled cells didn't work as expected. Got", result, "expected ", golden)
}
}
func TestSignalStepsUntilNonFill(t *testing.T) {
result := signalStepsUntilNonFill(sampleSolveDirections)
golden := DifficultySignals{
"Steps Until Nonfill": 2.0,
}
if !reflect.DeepEqual(result, golden) {
t.Error("Steps until nonfill didn't work as expected. Got ", result, "expected", golden)
}
}
func TestSignalPrecursorStepsLength(t *testing.T) {
result := signalPrecursorStepsLength(sampleSolveDirections)
golden := DifficultySignals{
"Average PrecursorSteps Length": 0.3333333333333333,
"Longest PrecursorSteps Length": 1.0,
}
if !reflect.DeepEqual(result, golden) {
t.Error("PresursorSteps length didn't work as expected. Got", result, "expected", golden)
}
}