-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathpruning.py
207 lines (178 loc) · 9.71 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
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
# ##################### The pruning tables cut the search tree during the search. ######################################
# ############ The pruning values are stored modulo 3 which saves a lot of memory. #####################################
import defs
import enums
import moves as mv
import symmetries as sy
import cubie as cb
from os import path
import array as ar
uint32 = 'I' if ar.array('I').itemsize >= 4 else 'L' # type codes differ between architectures
flipslicesorted_twist_depth3 = ar.array # global variables, initialized during pruning table creation
corner_depth = None
# ####################### functions to extract or set values in the pruning tables #####################################
def get_flipslicesorted_twist_depth3(ix):
"""get_fsst_depth3(ix) is *exactly* the number of moves % 3 to solve phase1x24 of a cube with index ix"""
y = flipslicesorted_twist_depth3[ix // 16]
y >>= (ix % 16) * 2
return y & 3
def set_flipslicesorted_twist_depth3(ix, value):
shift = (ix % 16) * 2
base = ix >> 4
flipslicesorted_twist_depth3[base] &= ~(3 << shift) & 0xffffffff
flipslicesorted_twist_depth3[base] |= value << shift
########################################################################################################################
def create_phase1x24_prun_table():
"""Create/load the flipslicesorted_twist_depth3 pruning table, 24x the phase1 table of the two-phase alg."""
global flipslicesorted_twist_depth3
total = defs.N_FLIPSLICESORTED_CLASS * defs.N_TWIST
fname = "phase1x24_prun"
if not path.isfile(fname):
print("creating " + fname + " table...")
print('This may take 8 hours or even longer, depending on the hardware and the Python version.')
print('Using PyPy instead of CPython gives a table creation speedup by a factor of about 20.')
flipslicesorted_twist_depth3 = ar.array(uint32, [0xffffffff] * (total // 16 + 1))
# #################### create table with the symmetries of the flipslicesorted classes #########################
cc = cb.CubieCube()
fs_sym = ar.array(uint32, [0] * defs.N_FLIPSLICESORTED_CLASS)
for i in range(defs.N_FLIPSLICESORTED_CLASS):
if (i + 1) % 24000 == 0:
print('.', end='', flush=True)
rep = sy.flipslicesorted_rep[i]
cc.set_slice_sorted(rep // defs.N_FLIP)
cc.set_flip(rep % defs.N_FLIP)
for s in range(defs.N_SYM_D4h):
ss = cb.CubieCube(sy.symCube[s].cp, sy.symCube[s].co, sy.symCube[s].ep,
sy.symCube[s].eo) # copy cube
ss.edge_multiply(cc) # s*cc
ss.edge_multiply(sy.symCube[sy.inv_idx[s]]) # s*cc*s^-1
if ss.get_slice_sorted() == rep // defs.N_FLIP and ss.get_flip() == rep % defs.N_FLIP:
fs_sym[i] |= 1 << s
print()
# ##################################################################################################################
fs_classidx = 0 # value for solved phase1x24
twist = 0
set_flipslicesorted_twist_depth3(defs.N_TWIST * fs_classidx + twist, 0)
done = 1
depth = 0
backsearch = False
print('depth:', depth, 'done: ' + str(done) + '/' + str(total))
while done != total:
depth3 = depth % 3
if depth == 10:
# backwards search is faster for depth >= 9 ANPASSEN!!!
print('flipping to backwards search...')
backsearch = True
idx = 0
for fs_classidx in range(defs.N_FLIPSLICESORTED_CLASS):
if (fs_classidx + 1) % 20000 == 0:
print('.', end='', flush=True)
if (fs_classidx + 1) % 1600000 == 0:
print('')
twist = 0
while twist < defs.N_TWIST:
# ########## if table entries are not populated, this is very fast: ################################
if not backsearch and idx % 16 == 0 and flipslicesorted_twist_depth3[idx // 16] == 0xffffffff \
and twist < defs.N_TWIST - 16:
twist += 16
idx += 16
continue
####################################################################################################
if backsearch:
match = (get_flipslicesorted_twist_depth3(idx) == 3)
else:
match = (get_flipslicesorted_twist_depth3(idx) == depth3)
if match:
flipslicesorted = sy.flipslicesorted_rep[fs_classidx]
flip = flipslicesorted % 2048 # defs.N_FLIP = 2048
slicesorted = flipslicesorted >> 11 # // defs.N_FLIP
for m in enums.Move:
twist1 = mv.twist_move[18 * twist + m] # defs.N_MOVE = 18
flip1 = mv.flip_move[18 * flip + m]
slicesorted1 = mv.slice_sorted_move[18 * slicesorted + m]
flipslicesorted1 = (slicesorted1 << 11) + flip1
fs1_classidx = sy.flipslicesorted_classidx[flipslicesorted1]
fs1_sym = sy.flipslicesorted_sym[flipslicesorted1]
twist1 = sy.twist_conj[(twist1 << 4) + fs1_sym]
idx1 = 2187 * fs1_classidx + twist1 # defs.N_TWIST = 2187
if not backsearch:
if get_flipslicesorted_twist_depth3(idx1) == 3: # entry not yet filled
set_flipslicesorted_twist_depth3(idx1, (depth + 1) % 3)
done += 1
# ####symmetric position has eventually more than one representation ###############
sym = fs_sym[fs1_classidx]
if sym != 1:
for k in range(1, 16):
sym >>= 1
if sym % 2 == 1:
twist2 = sy.twist_conj[(twist1 << 4) + k]
# fs2_classidx = fs1_classidx due to symmetry
idx2 = 2187 * fs1_classidx + twist2
if get_flipslicesorted_twist_depth3(idx2) == 3:
set_flipslicesorted_twist_depth3(idx2, (depth + 1) % 3)
done += 1
####################################################################################
else: # backwards search
if get_flipslicesorted_twist_depth3(idx1) == depth3:
set_flipslicesorted_twist_depth3(idx, (depth + 1) % 3)
done += 1
break
twist += 1
idx += 1 # idx = defs.N_TWIST * fs_class + twist
depth += 1
print()
print('depth:', depth, 'done: ' + str(done) + '/' + str(total))
fh = open(fname, "wb")
flipslicesorted_twist_depth3.tofile(fh)
else:
print("loading " + fname + " table...")
fh = open(fname, "rb")
flipslicesorted_twist_depth3 = ar.array(uint32)
flipslicesorted_twist_depth3.fromfile(fh, total // 16 + 1)
fh.close()
def create_cornerprun_table():
"""Create/load the corner_depth pruning table. Entry gives the number of moves which are at least necessary
to restore the corners."""
fname = "cornerprun"
global corner_depth
if not path.isfile(fname):
print("creating " + fname + " table...")
corner_depth = ar.array('b', [-1] * defs.N_CORNERS)
corners = 0 # value for solved corners
corner_depth[corners] = 0
done = 1
depth = 0
while done != defs.N_CORNERS:
for corners in range(defs.N_CORNERS):
if corner_depth[corners] == depth:
for m in enums.Move:
corners1 = mv.corners_move[18 * corners + m]
if corner_depth[corners1] == -1: # entry not yet filled
corner_depth[corners1] = depth + 1
done += 1
if done % 1000 == 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)
fh.close()
# # array distance computes the new distance from the old_distance i and the new_distance_mod3 j. ######################
# # We need this array because the pruning tables only store the distances mod 3. ######################################
# # The advantage of storing distances mod 3 is that we need only 2 bit per entry to store values 0, 1 or 2 and still
# # have value 3 left to indicate a still empty entry in the tables during table creation.
distance = ar.array('b', [0 for i in range(60)])
for i in range(20):
for j in range(3):
distance[3 * i + j] = (i // 3) * 3 + j
if i % 3 == 2 and j == 0:
distance[3 * i + j] += 3
elif i % 3 == 0 and j == 2:
distance[3 * i + j] -= 3
create_phase1x24_prun_table()
create_cornerprun_table()