forked from daniel-j/node-two-phase-algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSearch.js
More file actions
351 lines (309 loc) · 9.73 KB
/
Search.js
File metadata and controls
351 lines (309 loc) · 9.73 KB
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
'use strict';
var Color = require('./Color.js');
var FaceCube = require('./FaceCube.js');
var CoordCube = require('./CoordCube.js');
var Search = {};
module.exports = Search;
var ax = new Int32Array(31); // The axis of the move
var po = new Int32Array(31); // The power of the move
var flip = new Int32Array(31); // phase1 coordinates
var twist = new Int32Array(31);
var slice = new Int32Array(31);
var parity = new Int32Array(31); // phase2 coordinates
var URFtoDLF = new Int32Array(31);
var FRtoBR = new Int32Array(31);
var URtoUL = new Int32Array(31);
var UBtoDF = new Int32Array(31);
var URtoDF = new Int32Array(31);
var minDistPhase1 = new Int32Array(31); // IDA* distance do goal estimations
var minDistPhase2 = new Int32Array(31);
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// generate the solution string from the array data
function solutionToString(length) {
var s = "";
for (var i = 0; i < length; i++) {
switch (ax[i]) {
case 0:
s += "U";
break;
case 1:
s += "R";
break;
case 2:
s += "F";
break;
case 3:
s += "D";
break;
case 4:
s += "L";
break;
case 5:
s += "B";
break;
}
switch (po[i]) {
case 1:
s += " ";
break;
case 2:
s += "2 ";
break;
case 3:
s += "' ";
break;
}
}
return s;
};
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// generate the solution string from the array data including a separator between phase1 and phase2 moves
function solutionToStringDepthPhase(length, depthPhase1) {
var s = "";
for (var i = 0; i < length; i++) {
switch (ax[i]) {
case 0:
s += "U";
break;
case 1:
s += "R";
break;
case 2:
s += "F";
break;
case 3:
s += "D";
break;
case 4:
s += "L";
break;
case 5:
s += "B";
break;
}
switch (po[i]) {
case 1:
s += " ";
break;
case 2:
s += "2 ";
break;
case 3:
s += "' ";
break;
}
if (i == depthPhase1 - 1)
s += ". ";
}
return s;
};
/**
* Computes the solver string for a given cube.
*
* @param facelets
* is the cube definition string, see {@link Facelet} for the format.
*
* @param maxDepth
* defines the maximal allowed maneuver length. For random cubes, a maxDepth of 21 usually will return a
* solution in less than 0.5 seconds. With a maxDepth of 20 it takes a few seconds on average to find a
* solution, but it may take much longer for specific cubes.
*
*@param timeOut
* defines the maximum computing time of the method in seconds. If it does not return with a solution, it returns with
* an error code.
*
* @param useSeparator
* determines if a " . " separates the phase1 and phase2 parts of the solver string like in F' R B R L2 F .
* U2 U D for example.<br>
* @return The solution string or an error code:<br>
* Error 1: There is not exactly one facelet of each colour<br>
* Error 2: Not all 12 edges exist exactly once<br>
* Error 3: Flip error: One edge has to be flipped<br>
* Error 4: Not all corners exist exactly once<br>
* Error 5: Twist error: One corner has to be twisted<br>
* Error 6: Parity error: Two corners or two edges have to be exchanged<br>
* Error 7: No solution exists for the given maxDepth<br>
* Error 8: Timeout, no solution within given time
*/
function solution(facelets, maxDepth, timeOut, useSeparator) {
var s;
// +++++++++++++++++++++check for wrong input +++++++++++++++++++++++++++++
var count = new Int32Array(6);
try {
for (var i = 0; i < 54; i++) {
count[Color.valueOfStr(facelets.substring(i, i + 1))]++;
}
} catch (e) {
return 1;
}
for (var i = 0; i < 6; i++)
if (count[i] !== 9)
return 1;
var fc = new FaceCube(facelets);
var cc = fc.toCubieCube();
if ((s = cc.verify()) !== 0)
return Math.abs(s);
// +++++++++++++++++++++++ initialization +++++++++++++++++++++++++++++++++
var c = new CoordCube(cc);
po[0] = 0;
ax[0] = 0;
flip[0] = c.flip;
twist[0] = c.twist;
parity[0] = c.parity;
slice[0] = c.FRtoBR / 24;
URFtoDLF[0] = c.URFtoDLF;
FRtoBR[0] = c.FRtoBR;
URtoUL[0] = c.URtoUL;
UBtoDF[0] = c.UBtoDF;
minDistPhase1[1] = 1;// else failure for depth=1, n=0
var mv = 0, n = 0;
var busy = false;
var depthPhase1 = 1;
var tStart = Date.now();
// +++++++++++++++++++ Main loop ++++++++++++++++++++++++++++++++++++++++++
do {
do {
if ((depthPhase1 - n > minDistPhase1[n + 1]) && !busy) {
if (ax[n] === 0 || ax[n] === 3)// Initialize next move
ax[++n] = 1;
else
ax[++n] = 0;
po[n] = 1;
} else if (++po[n] > 3) {
do {// increment axis
if (++ax[n] > 5) {
if (Date.now() - tStart > timeOut << 10)
return 8;
if (n === 0) {
if (depthPhase1 >= maxDepth)
return 7;
else {
depthPhase1++;
ax[n] = 0;
po[n] = 1;
busy = false;
break;
}
} else {
n--;
busy = true;
break;
}
} else {
po[n] = 1;
busy = false;
}
} while (n !== 0 && (ax[n - 1] === ax[n] || ax[n - 1] - 3 === ax[n]));
} else
busy = false;
} while (busy);
// +++++++++++++ compute new coordinates and new minDistPhase1 ++++++++++
// if minDistPhase1 =0, the H subgroup is reached
mv = 3 * ax[n] + po[n] - 1;
flip[n + 1] = CoordCube.flipMove[flip[n]][mv];
twist[n + 1] = CoordCube.twistMove[twist[n]][mv];
slice[n + 1] = CoordCube.FRtoBR_Move[slice[n] * 24][mv] / 24;
minDistPhase1[n + 1] = Math.max(CoordCube.getPruning(CoordCube.Slice_Flip_Prun, CoordCube.N_SLICE1 * flip[n + 1]
+ slice[n + 1]), CoordCube.getPruning(CoordCube.Slice_Twist_Prun, CoordCube.N_SLICE1 * twist[n + 1]
+ slice[n + 1]));
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
if (minDistPhase1[n + 1] === 0 && n >= depthPhase1 - 5) {
minDistPhase1[n + 1] = 10;// instead of 10 any value >5 is possible
if (n === depthPhase1 - 1 && (s = totalDepth(depthPhase1, maxDepth)) >= 0) {
if (s === depthPhase1
|| (ax[depthPhase1 - 1] !== ax[depthPhase1] && ax[depthPhase1 - 1] !== ax[depthPhase1] + 3))
return useSeparator ? solutionToStringDepthPhase(s, depthPhase1) : solutionToString(s);
}
}
} while (true);
}
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// Apply phase2 of algorithm and return the combined phase1 and phase2 depth. In phase2, only the moves
// U,D,R2,F2,L2 and B2 are allowed.
function totalDepth(depthPhase1, maxDepth) {
var mv = 0, d1 = 0, d2 = 0;
var maxDepthPhase2 = Math.min(10, maxDepth - depthPhase1);// Allow only max 10 moves in phase2
for (var i = 0; i < depthPhase1; i++) {
mv = 3 * ax[i] + po[i] - 1;
URFtoDLF[i + 1] = CoordCube.URFtoDLF_Move[URFtoDLF[i]][mv];
FRtoBR[i + 1] = CoordCube.FRtoBR_Move[FRtoBR[i]][mv];
parity[i + 1] = CoordCube.parityMove[parity[i]][mv];
}
if ((d1 = CoordCube.getPruning(CoordCube.Slice_URFtoDLF_Parity_Prun,
(CoordCube.N_SLICE2 * URFtoDLF[depthPhase1] + FRtoBR[depthPhase1]) * 2 + parity[depthPhase1])) > maxDepthPhase2)
return -1;
for (var i = 0; i < depthPhase1; i++) {
mv = 3 * ax[i] + po[i] - 1;
URtoUL[i + 1] = CoordCube.URtoUL_Move[URtoUL[i]][mv];
UBtoDF[i + 1] = CoordCube.UBtoDF_Move[UBtoDF[i]][mv];
}
URtoDF[depthPhase1] = CoordCube.MergeURtoULandUBtoDF[URtoUL[depthPhase1]][UBtoDF[depthPhase1]];
if ((d2 = CoordCube.getPruning(CoordCube.Slice_URtoDF_Parity_Prun,
(CoordCube.N_SLICE2 * URtoDF[depthPhase1] + FRtoBR[depthPhase1]) * 2 + parity[depthPhase1])) > maxDepthPhase2)
return -1;
if ((minDistPhase2[depthPhase1] = Math.max(d1, d2)) === 0)// already solved
return depthPhase1;
// now set up search
var depthPhase2 = 1;
var n = depthPhase1;
var busy = false;
po[depthPhase1] = 0;
ax[depthPhase1] = 0;
minDistPhase2[n + 1] = 1;// else failure for depthPhase2=1, n=0
// +++++++++++++++++++ end initialization +++++++++++++++++++++++++++++++++
do {
do {
if ((depthPhase1 + depthPhase2 - n > minDistPhase2[n + 1]) && !busy) {
if (ax[n] === 0 || ax[n] === 3)// Initialize next move
{
ax[++n] = 1;
po[n] = 2;
} else {
ax[++n] = 0;
po[n] = 1;
}
} else if ((ax[n] === 0 || ax[n] === 3) ? (++po[n] > 3) : ((po[n] = po[n] + 2) > 3)) {
do {// increment axis
if (++ax[n] > 5) {
if (n === depthPhase1) {
if (depthPhase2 >= maxDepthPhase2)
return -1;
else {
depthPhase2++;
ax[n] = 0;
po[n] = 1;
busy = false;
break;
}
} else {
n--;
busy = true;
break;
}
} else {
if (ax[n] === 0 || ax[n] === 3)
po[n] = 1;
else
po[n] = 2;
busy = false;
}
} while (n !== depthPhase1 && (ax[n - 1] === ax[n] || ax[n - 1] - 3 === ax[n]));
} else
busy = false;
} while (busy);
// +++++++++++++ compute new coordinates and new minDist ++++++++++
mv = 3 * ax[n] + po[n] - 1;
URFtoDLF[n + 1] = CoordCube.URFtoDLF_Move[URFtoDLF[n]][mv];
FRtoBR[n + 1] = CoordCube.FRtoBR_Move[FRtoBR[n]][mv];
parity[n + 1] = CoordCube.parityMove[parity[n]][mv];
URtoDF[n + 1] = CoordCube.URtoDF_Move[URtoDF[n]][mv];
minDistPhase2[n + 1] = Math.max(CoordCube.getPruning(CoordCube.Slice_URtoDF_Parity_Prun, (CoordCube.N_SLICE2
* URtoDF[n + 1] + FRtoBR[n + 1])
* 2 + parity[n + 1]), CoordCube.getPruning(CoordCube.Slice_URFtoDLF_Parity_Prun, (CoordCube.N_SLICE2
* URFtoDLF[n + 1] + FRtoBR[n + 1])
* 2 + parity[n + 1]));
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
} while (minDistPhase2[n + 1] !== 0);
return depthPhase1 + depthPhase2;
}
Search.solution = solution;