forked from donkirkby/donimoes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_dominosa.py
525 lines (409 loc) · 10.5 KB
/
test_dominosa.py
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
from networkx import DiGraph
from diagram import draw_diagram
from domino_puzzle import MoveDescription
from dominosa import DominosaBoard, PairState, DominosaGraph, DominosaProblem, \
generate_moves_from_unique_pairs, generate_moves_from_single_neighbours, \
generate_moves_from_newly_joined, generate_moves_from_newly_split, \
generate_moves_from_duplicate_neighbours, FitnessCalculator, strip_solution
from svg_diagram import SvgDiagram
# noinspection PyUnresolvedReferences
from diagram_differ import drawing_differ
def test_rule6_unique_pairs_vertical():
start_state = """\
1 0 1
1 0 0
"""
board = DominosaBoard.create(start_state, max_pips=1)
expected_display = """\
1 0 1
j
1 0 0
"""
expected_moves = [('6:00j01', expected_display)]
moves = list(generate_moves_from_unique_pairs(board))
final_state = board.display()
assert moves == expected_moves
assert final_state == start_state
def test_rule6_unique_pairs_horizontal():
board = DominosaBoard.create("""\
1 1
0 0
1 0
""", max_pips=1)
expected_display = """\
1j1
0 0
1 0
"""
expected_moves = [('6:02j12', expected_display)]
moves = list(generate_moves_from_unique_pairs(board))
assert moves == expected_moves
def test_rule1_single_neighbour():
start_state = """\
1 1 0 1
s s
0|0s2 0
s s
1 2 2 2
"""
board = DominosaBoard.create(start_state, max_pips=2)
expected_display1 = """\
1 1 0 1
s s
0|0s2 0
s s
1j2 2 2
"""
expected_display2 = """\
1j1 0 1
s s
0|0s2 0
s s
1 2 2 2
"""
expected_moves = [('1:02j12', expected_display2),
('1:00j10', expected_display1)]
moves = list(generate_moves_from_single_neighbours(board))
final_state = board.display()
assert moves == expected_moves
assert final_state == start_state
def test_rule1_single_neighbour_for_cell():
start_state = """\
1 1 0 1
s
0s0 2 0
-
1s2 2 2
"""
board = DominosaBoard.create(start_state, max_pips=2)
expected_display = """\
1j1 0 1
s
0s0 2 0
-
1s2 2 2
"""
expected_moves = [('1:02j12', expected_display)]
moves = list(generate_moves_from_single_neighbours(board))
final_state = board.display()
assert moves == expected_moves
assert final_state == start_state
def test_rule6_big_head():
start_state = """\
2 2s1|1s0
s s -
2 1s0|0s2
s s s s s
3 3 3 2 3
s
0 1 0 3 1
"""
board = DominosaBoard.create(start_state, max_pips=2)
expected_moves = []
graph = DominosaGraph(DominosaBoard)
moves = list(graph.generate_moves(board))
final_state = board.display()
assert moves == expected_moves
assert final_state == start_state
def test_rule1_to_solution():
start_state = """\
0 0 1
s s j
1|1s0
"""
board = DominosaBoard.create(start_state, max_pips=1)
expected_display = """\
0|0 1
-
1|1 0
"""
expected_moves = [MoveDescription('1:01j11',
expected_display,
{'weight': 1, 'move_num': 1})]
graph = DominosaGraph(DominosaBoard, move_weights={1: 1})
moves = list(graph.generate_moves(board))
final_state = board.display()
assert moves == expected_moves
assert final_state == start_state
def test_rule2_split_neighbours():
start_state = """\
1 0 1
j
1 0 0
"""
board = DominosaBoard.create(start_state, max_pips=1)
expected_display = """\
1s0 1
-
1s0 0
"""
expected_moves = [('2:00|01,00s10,01s11', expected_display)]
moves = list(generate_moves_from_newly_joined(board))
final_state = board.display()
assert moves == expected_moves
assert final_state == start_state
def test_rule2_split_duplicate():
start_state = """\
2j2 2 0
~ ~
0|0 2 1
1|1 0 1
"""
board = DominosaBoard.create(start_state, max_pips=2)
expected_display = """\
2|2s2 0
~ ~ s
0|0 2 1
1|1 0 1
"""
expected_moves = [('2:02|12,12s22,21s22', expected_display)]
moves = list(generate_moves_from_newly_joined(board))
final_state = board.display()
assert moves == expected_moves
assert final_state == start_state
def test_rule3_newly_split():
start_state = """\
1s1 2 3 1
-
0s2 2 2 3
s
0 2 0 3 1
3 0 0 3 1
"""
board = DominosaBoard.create(start_state, max_pips=3)
expected_display = """\
1S1 2 3 1
-
0S2 2 2 3
~
0 2 0 3 1
j
3 0 0 3 1
"""
expected_moves = [('3:01S02,02S12,03S13,40j41', expected_display)]
moves = list(generate_moves_from_newly_split(board))
final_state = board.display()
assert moves == expected_moves
assert final_state == start_state
def test_rule4_duplicate_neighours():
start_state = """\
2 2 0 1
2 1 0 1
2 0 0 1
"""
board = DominosaBoard.create(start_state, max_pips=3)
expected_display = """\
2 2 0 1
2 1 0 1
s
2 0 0 1
"""
expected_moves = [('4:02,00s01', expected_display)]
moves = list(generate_moves_from_duplicate_neighbours(board))
final_state = board.display()
assert moves == expected_moves
assert final_state == start_state
def test_rule5_shared_space():
start_state = """\
2 3 3 1 3
0 1 2 0 3
0 0 2 2 3
~ ~
1|1S2 1 0
"""
board = DominosaBoard.create(start_state, max_pips=3)
expected_display = """\
2 3 3 1 3
0 1 2 0 3
0 0s2 2 3
~ ~
1|1S2 1 0
"""
expected_moves = [MoveDescription('5:21,21s11',
expected_display,
{'weight': 1, 'move_num': 5},
remaining=9)]
graph = DominosaGraph(DominosaBoard, move_weights={5: 1, 6: 10})
moves = list(graph.generate_moves(board))
final_state = board.display()
assert moves == expected_moves
assert final_state == start_state
def test_move_weights():
start_state = """\
1 1 0
0 0 1
"""
board = DominosaBoard.create(start_state, max_pips=1)
graph = DominosaGraph(DominosaBoard, move_weights={1: 2,
2: 3,
3: 4,
4: 5,
5: 10,
6: 7})
graph.walk(board)
graph_x: DiGraph = graph.graph
dup_state = """\
1 1s0
s s
0 0 1
"""
joined_state = """\
1j1s0
s s
0 0 1
"""
weight4 = graph_x.get_edge_data(start_state, dup_state)['weight']
weight1 = graph_x.get_edge_data(dup_state, joined_state)['weight']
assert weight4 == 5
assert weight1 == 2
def test_get_pair_state_default():
board = DominosaBoard.create("""\
1 0 1
1 0 0
""")
pair_state = board.get_pair_state(0, 0, 0, 1)
assert pair_state == PairState.UNDECIDED
def test_get_pair_state_vertical():
board = DominosaBoard.create("""\
1 0 1
j
1 0 0
""")
pair_state1 = board.get_pair_state(0, 0, 0, 1)
pair_state2 = board.get_pair_state(0, 1, 0, 0)
assert pair_state1 == PairState.NEWLY_JOINED
assert pair_state2 == PairState.NEWLY_JOINED
def test_get_pair_state_horizontal():
board = DominosaBoard.create("""\
1s0 1
-
1S0 0
""")
pair_state1 = board.get_pair_state(0, 1, 1, 1)
pair_state2 = board.get_pair_state(1, 1, 0, 1)
assert pair_state1 == PairState.NEWLY_SPLIT
assert pair_state2 == PairState.NEWLY_SPLIT
def test_display_pair_state():
expected_state = """\
1s0|1
-
1S0j0
"""
board = DominosaBoard.create(expected_state)
state = board.display()
assert state == expected_state
def test_random_init():
expected_keys = {'solution', 'max_pips'}
problem = DominosaProblem(init_params=dict(width=4, height=5, max_pips=3))
assert problem.value.keys() == expected_keys
lines = problem.value['solution'].splitlines(keepends=False)
assert len(lines) == 9
assert len(lines[0]) == 7
assert 'x' not in problem.value['solution']
def test_fitness_counts_unplaced_dominoes():
value = dict(solution="""\
0 1 1
- - -
0 1 0
""",
max_pips=1)
problem = DominosaProblem(value)
expected_dominoes_unused = 2
expected_move_types_unused = 2
expected_fitness = (-1_000_000 * expected_dominoes_unused +
-10_000 * expected_move_types_unused)
calculator = FitnessCalculator()
fitness = calculator.calculate(problem)
assert fitness == expected_fitness
def test_fitness_counts_solution_moves():
value = dict(solution="""\
1|2 0|1
0 0|2 2
- -
0 1|1 2
""",
max_pips=2)
problem = DominosaProblem(value)
rule5_move_count = 1
other_move_count = 9
unused_move_types = 3
expected_fitness = -(10*rule5_move_count +
other_move_count +
10_000*unused_move_types)
calculator = FitnessCalculator(move_weights={1: 1,
2: 1,
3: 1,
4: 1,
5: 10,
6: 100})
fitness = calculator.calculate(problem)
assert fitness == expected_fitness
def test_strip_solution():
solution = """\
1|2 3
-
4|5 6
"""
expected_stripped = """\
1 2 3
4 5 6"""
stripped = strip_solution(solution)
assert stripped == expected_stripped
# noinspection DuplicatedCode
def test_draw_vertical_split(drawing_differ):
state_without_split = """\
0 0 1
1 1 0
"""
state_with_split = """\
0 0S1
1 1 0
"""
expected = SvgDiagram(500, 250)
actual = SvgDiagram(500, 250)
t = expected.turtle
t.up()
t.goto(-125, 100)
draw_diagram(t, state_without_split)
t.forward(200)
t.right(90)
t.forward(15)
t.down()
t.pencolor('black')
t.pensize(5)
t.forward(70)
t = actual.turtle
t.up()
t.goto(-125, 100)
draw_diagram(t, state_with_split, board_class=DominosaBoard)
drawing_differ.assert_equal(actual, expected, 'draw_split_vertical')
# noinspection DuplicatedCode
def test_draw_horizontal_split(drawing_differ):
state_without_split = """\
0 0 1
1 1 0
"""
state_with_split = """\
0 0 1
~
1 1 0
"""
expected = SvgDiagram(500, 250)
actual = SvgDiagram(500, 250)
t = expected.turtle
t.up()
t.goto(-125, 100)
draw_diagram(t, state_without_split)
t.right(90)
t.forward(100)
t.left(90)
t.forward(115)
t.down()
t.pencolor('black')
t.pensize(5)
t.forward(70)
t = actual.turtle
t.up()
t.goto(-125, 100)
draw_diagram(t, state_with_split, board_class=DominosaBoard)
drawing_differ.assert_equal(actual, expected, 'draw_split_horizontal')