-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathpruning.py
51 lines (44 loc) · 1.88 KB
/
pruning.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
# ##################### The pruning table cuts the search tree during the search. ######################################
# ########################## In this case it it gives the exact distance to the solved state. ##########################
import defs
import enums
import moves as mv
from os import path
import array as ar
corner_depth = None
def create_cornerprun_table():
"""Creates/loads the corner pruning table."""
fname = "cornerprun"
global corner_depth
if not path.isfile(fname):
print("creating " + fname + " table...")
corner_depth = ar.array('b', [-1] * (defs.N_CORNERS * defs.N_TWIST))
corners = 0 # values for solved cube
twist = 0
corner_depth[defs.N_TWIST * corners + twist] = 0
done = 1
depth = 0
while done != defs.N_CORNERS * defs.N_TWIST:
for corners in range(defs.N_CORNERS):
for twist in range(defs.N_TWIST):
if corner_depth[defs.N_TWIST * corners + twist] == depth:
for m in enums.Move:
corners1 = mv.cornperm_move[9 * corners + m]
twist1 = mv.corntwist_move[9 * twist + m]
idx1 = defs.N_TWIST * corners1 + twist1
if corner_depth[idx1] == -1: # entry not yet filled
corner_depth[idx1] = depth + 1
done += 1
if done % 50000 == 0:
print('.', end='', flush=True)
depth += 1
print()
fh = open(fname, "wb")
corner_depth.tofile(fh)
else:
print("loading " + fname + " table...")
fh = open(fname, "rb")
corner_depth = ar.array('b')
corner_depth.fromfile(fh, defs.N_CORNERS * defs.N_TWIST)
fh.close()
create_cornerprun_table()