forked from hkociemba/CubeExplorer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSearch.pas
More file actions
1802 lines (1519 loc) · 58.5 KB
/
Search.pas
File metadata and controls
1802 lines (1519 loc) · 58.5 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
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
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
unit Search;
interface
uses CubeDefs,Symmetries,CordCube,graphics,classes;
{$IF not QTM}
const MAXNODES = 31;
{$ELSE}
const MAXNODES = 40;
{$IFEND}
type
//+++++++++++++++Node in search tree++++++++++++++++++++++++++++++++++++++++++++
Node = record
axis:TurnAxis;
power,UDIdx,UDSym,UDTwist,RLIdx,
RLSym,RLTwist,FBIdx,FBSym,FBTwist: Integer;
UDPrun,RLPrun,FBPrun:Integer;
UDSliceSorted,RLSliceSorted,FBSliceSorted,
edge8Pos,cornPos: Integer;//phase2 coordinates
parityEven: Boolean;//used in QTM
UDSliceSortedSymIdx,RLSliceSortedSymIdx,FBSliceSortedSymIdx,
UDSliceSortedSymSym,RLSliceSortedSymSym,FBSliceSortedSymSym : Integer;
UDPrunBig,RLPrunBig,FBPrunBig:Integer;
UDFlip,RLFlip,FBFlip:Integer;
UDTetra,RLTetra,FBTetra:Integer;
UDCenTwist,RLCenTwist,FBCenTwist:Integer;//for oriented Cubes
UDCentRFLBMod2Twist,RLCentRFLBMod2Twist,FBCentRFLBMod2Twist:Integer;
//UDSlice,RLSlice,FBSlice: Integer;
nodenum: Int64;//Anzahl der Züge, um diese Tiefe zu vollenden
{$IF QTM}
virtualmove: Boolean;//true wenn in Phase2 in Zug R,F,L,B,Rs,Fs ausgeführt wird
{$IFEND}
end;
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//+++++++++++++++IDA object does the search+++++++++++++++++++++++++++++++++++++
IDA = class(TThread)
public
n: array[0..MAXNODES] of Node;//30 moves suffice!
nodeCount: Int64;
sym: Int64;//Symmetries of the cube
depth,depth2,maxLength,inValid,returnLength: Integer;
runOptimal,runCoset,isOriented:Boolean;
constructor Create(cc:CoordCube);overload;
constructor Create(ia:Ida);overload;
function Next2PhaseSolution:Integer;
function NextSolution(maxLen:Integer):Integer;
procedure NextPhase2Id;
function IsIdCube:Boolean;
// function ExploreCoset: Integer;
// procedure SetBit;
// procedure PreScanNextLevel;
function SolverString:String;
procedure Execute; override;//for the thread
end;
var GetPruningLength: array [0..18,0..2] of Integer;
procedure CreateGetPruningLengthTable;
implementation
uses RubikMain,CubiCube,SysUtils,Windows,CosetExp,InComp;
//+++++++++++create an IDA-object from a cube on coordinate level+++++++++++++++
constructor IDA.Create(cc: CoordCube);
begin
inherited Create(true);//thread issues
Priority:=tpLower;
if Form1.FreeThreadsonTerminate1.Checked then FreeOnTerminate:= true;
isOriented:=cc.isOriented;
n[0].UDIdx:= cc.flipUDSlice.n;//initialize node
n[0].UDSym:= cc.flipUDSlice.s;
n[0].RLIdx:= cc.flipRLSlice.n;
n[0].RLSym:= cc.flipRLSlice.s;
n[0].FBIdx:= cc.flipFBSlice.n;
n[0].FBSym:= cc.flipFBSlice.s;
if isOriented then
begin
n[0].UDPrun:= cc.GetCentPrun(0);
n[0].RLPrun:= cc.GetCentPrun(1);
n[0].FBPrun:= cc.GetCentPrun(2);
end
else
begin
n[0].UDPrun:= cc.GetPrun(0);
n[0].RLPrun:= cc.GetPrun(1);
n[0].FBPrun:= cc.GetPrun(2);
end;
n[0].UDTwist:=cc.UDTwist;
n[0].RLTwist:=cc.RLTwist;
n[0].FBTwist:=cc.FBTwist;
n[0].axis:=U;
n[0].power:=0;
{$IF QTM}
n[0].virtualmove:=false;//in Phase 1 gibt es keine virtual moves
{$IFEND}
depth:= 0;
depth2:= 0;//in case we use two phase algorithm
nodeCount:= 0;
sym:=cc.sym;
inValid:=0;
maxLength:=MAXNODES;
returnLength:=-1;
runOptimal:=false;//use two phase algorithm as default
n[0].cornPos:= cc.cornPos;
n[0].UDSliceSorted:=cc.UDSliceSorted;
n[0].RLSliceSorted:=cc.RLSliceSorted;
n[0].FBSliceSorted:=cc.FBSliceSorted;
//big solver coordinates
{$IF UHUGE}
if USES_BIG then
begin
n[0].UDPrunBig:= cc.GetPrunUBig(0);
n[0].RLPrunBig:= cc.GetPrunUBig(1);
n[0].FBPrunBig:= cc.GetPrunUBig(2);
n[0].UDTetra:= cc.UDTetra;
n[0].RLTetra:= cc.RLTetra;
n[0].FBTetra:= cc.FBTetra;
end;
{$ELSE}
n[0].UDSliceSortedSymIdx:=cc.UDSliceSortedSym.n;
n[0].RLSliceSortedSymIdx:=cc.RLSliceSortedSym.n;
n[0].FBSliceSortedSymIdx:=cc.FBSliceSortedSym.n;
n[0].UDSliceSortedSymSym:=cc.UDSliceSortedSym.s;
n[0].RLSliceSortedSymSym:=cc.RLSliceSortedSym.s;
n[0].FBSliceSortedSymSym:=cc.FBSliceSortedSym.s;
n[0].UDFlip:=cc.UDFlip;
n[0].RLFlip:=cc.RLFlip;
n[0].FBFlip:=cc.FBFlip;
if USES_BIG then
begin
n[0].UDPrunBig:= cc.GetPrunBig(0);
n[0].RLPrunBig:= cc.GetPrunBig(1);
n[0].FBPrunBig:= cc.GetPrunBig(2);
end;
{$IFEND}
n[0].UDCenTwist:= cc.UDCenTwist;//for oriented Cubes
n[0].RLCenTwist:= cc.RLCenTwist;
n[0].FBCenTwist:= cc.FBCenTwist;
// n[0].UDSlice:= cc.UDSlice;
// n[0].RLSlice:= cc.RLSlice;
// n[0].FBSlice:= cc.FBSlice;
n[0].UDCentRFLBMod2Twist:=cc.UDCentRFLBMod2Twist;
n[0].RLCentRFLBMod2Twist:=cc.RLCentRFLBMod2Twist;
n[0].FBCentRFLBMod2Twist:=cc.FBCentRFLBMod2Twist;
n[0].parityEven:= cc.parityEven;
end;
//+++++++End create IDA-object from cube on coordinate level++++++++++++++++++++
//+++++++++++++++++++find next solution of optimal solver+++++++++++++++++++++++
function IDA.NextSolution(maxLen: Integer):Integer;
var np,np1,np2: ^Node;
x,r_depth,i,j,k,fixNum: Integer;
m,m1: Move;
ax: TurnAxis;
t_sym:Int64;
label incPower,turn,right,incAxis,checkNeighbourAxis,left,ende;
begin
PostMessage(Form1.Handle,WM_NEXTLEVEL,Integer(self),depth+1);
maxLength:=maxLen;
np:= @n[depth];
r_depth:=0; //r_depth zählt die noch zu besetzenden freien Felder rechts (mit wachsendem Index)
incPower:
Inc(np^.power);
{$IF QTM}
if (np^.power=2) then Inc(np^.power);
{$IFEND}
if (np^.power>3) then goto incAxis;
if (depth<>r_depth) then //sonst kein linker Nachbar
begin
np1:=np; Dec(np1);
{$IF QTM}
if (np1^.axis=np^.axis)and ((np1^.power=3) or (np^.power=3)) then goto incAxis; //only X*X, not X*X',X'*X or X'*X'
{$IFEND}
if slicemode and (np1^.axis<=F) and (np^.axis=Turnaxis(Ord(np1^.axis)+3))
and (np1^.power + np^.power = 4) then goto incPower;//kein UD', U2D2, U'D etc wenn slices
end;
turn:
np1:=np;
Inc(np1);
Inc(NodeCount);
{$IF QTM}
np1^.parityEven:= not np^.parityEven;
{$IFEND}
m:= Move(3*Ord(np^.axis) + np^.power - 1);
{$IF UHUGE} //UltraHuge Solver
if USES_BIG and not isOriented then //Ultrabig Solver für orientierte Cubes lansamer als Standard
begin
if isOriented then
begin
np1^.UDCentRFLBMod2Twist:= CentOriRFLBMod2Move[np^.UDCentRFLBMod2Twist,m];
end;
np1^.UDTwist:= TwistMove[np^.UDTwist,m];
np1^.UDTetra:= TetraMove[np^.UDTetra,m];
x:= flipSliceMove[np^.UDIdx,Ord(SymMove[np^.UDSym,m])];
np1^.UDSym:=SymMult[x and 15,np^.UDSym];
np1^.UDIdx:= x shr 4;
m:= SymMove[16,m];
if isOriented then
begin
np1^.RLCentRFLBMod2Twist:= CentOriRFLBMod2Move[np^.RLCentRFLBMod2Twist,m];
end;
np1^.RLTwist:= TwistMove[np^.RLTwist,m];
np1^.RLTetra:= TetraMove[np^.RLTetra,m];
x:= flipSliceMove[np^.RLIdx,Ord(SymMove[np^.RLSym,m])];
np1^.RLSym:=SymMult[x and 15,np^.RLSym];
np1^.RLIdx:= x shr 4;
m:= SymMove[16,m];
np1^.FBTwist:= TwistMove[np^.FBTwist,m];
np1^.FBTetra:= TetraMove[np^.FBTetra,m];
x:= flipSliceMove[np^.FBIdx,Ord(SymMove[np^.FBSym,m])];
np1^.FBSym:=SymMult[x and 15,np^.FBSym];
np1^.FBIdx:= x shr 4;
if isOriented then
begin
np1^.FBCentRFLBMod2Twist:= CentOriRFLBMod2Move[np^.FBCentRFLBMod2Twist,m];
end;
np1^.UDPrunBig:= GetPruningLength[np^.UDPrunBig,GetPruningUBigP(2187*np1^.UDIdx +
TwistConjugate[np1^.UDTwist,np1^.UDSym],@PruningUBigP[TetraConjugate[np1^.UDTetra,np1^.UDSym]])];
np1^.RLPrunBig:= GetPruningLength[np^.RLPrunBig,GetPruningUBigP(2187*np1^.RLIdx +
TwistConjugate[np1^.RLTwist,np1^.RLSym],@PruningUBigP[TetraConjugate[np1^.RLTetra,np1^.RLSym]])];
np1^.FBPrunBig:= GetPruningLength[np^.FBPrunBig,GetPruningUBigP(2187*np1^.FBIdx
+ TwistConjugate[np1^.FBTwist,np1^.FBSym],@PruningUBigP[TetraConjugate[np1^.FBTetra,np1^.FBSym]])];
if isOriented then
begin
np1^.UDPrun:= GetPruningLength[np^.UDPrun,GetPruningCentP((Int64(np1^.UDIdx)*16+
CentOriRFLBMod2Conjugate[np1^.UDCentRFLBMod2Twist,np1^.UDSym])*2187+
TwistConjugate[np1^.UDTwist,np1^.UDSym])];
np1^.RLPrun:= GetPruningLength[np^.RLPrun,GetPruningCentP((Int64(np1^.RLIdx)*16+
CentOriRFLBMod2Conjugate[np1^.RLCentRFLBMod2Twist,np1^.RLSym])*2187+
TwistConjugate[np1^.RLTwist,np1^.RLSym])];
np1^.FBPrun:= GetPruningLength[np^.FBPrun, GetPruningCentP((Int64(np1^.FBIdx)*16+
CentOriRFLBMod2Conjugate[np1^.FBCentRFLBMod2Twist,np1^.FBSym])*2187+
TwistConjugate[np1^.FBTwist,np1^.FBSym])];
end;
if not Terminated then
begin
{$IF QTM}
if (not slicemode) and not (Odd(r_depth) xor np1^.parityEven) then goto incAxis;//im slicemode ändern manche Züge nicht die Parität der Ecken!
{$IFEND}
//Es kann gefolgert werden, dass der Pruningwert eins höher ist, wenn alle gleich sind.
if (r_depth>0) and (np1^.UDPrunBig=np1^.RLPrunBig)
and (np1^.RLPrunBig=np1^.FBPrunBig) then
begin
{$IF not QTM}
if (np1^.UDPrunBig>r_depth) then goto incAxis;
if (np1^.UDPrunBig>r_depth-1) then goto incPower;
end;
if (np1^.UDPrunBig>r_depth+1) or (np1^.RLPrunBig>r_depth+1) or (np1^.FBPrunBig>r_depth+1) then goto incAxis;
if (np1^.UDPrunBig>r_depth) or (np1^.RLPrunBig>r_depth) or (np1^.FBPrunBig>r_depth) then goto incPower;
{$ELSE}
if (np1^.UDPrunBig>r_depth+1) then goto incAxis; //QTM r_depth+1
if (np1^.UDPrunBig>r_depth-1) then goto incPower;
end;
if (np1^.UDPrunBig>r_depth+2) or (np1^.RLPrunBig>r_depth+2) or (np1^.FBPrunBig>r_depth+2) then goto incAxis; //QTM r_depth+2
if (np1^.UDPrunBig>r_depth) or (np1^.RLPrunBig>r_depth) or (np1^.FBPrunBig>r_depth) then goto incPower;
{$IFEND}
if isOriented then //solte dass nicht besser in die terminated Schleife?
if (np1^.UDPrun>r_depth) or (np1^.RLPrun>r_depth) or (np1^.FBPrun>r_depth) then goto incPower;
end;
{$ELSE} //Huge Solver
if USES_BIG and not isOriented then //auch nicht für oriented cubes
begin
np1^.UDTwist:= TwistMove[np^.UDTwist,m];
x:= UDSliceSortedSymMove[np^.UDSliceSortedSymIdx,SymMove[np^.UDSliceSortedSymSym,m]];
np1^.UDSliceSortedSymSym:=SymMult[x and 15,np^.UDSliceSortedSymSym];
np1^.UDSliceSortedSymIdx:= x shr 4;
np1^.UDFlip:= FlipMove[np^.UDFlip,m];
m:= SymMove[16,m];
np1^.RLTwist:= TwistMove[np^.RLTwist,m];
x:= UDSliceSortedSymMove[np^.RLSliceSortedSymIdx,SymMove[np^.RLSliceSortedSymSym,m]];
np1^.RLSliceSortedSymSym:=SymMult[x and 15,np^.RLSliceSortedSymSym];
np1^.RLSliceSortedSymIdx:= x shr 4;
np1^.RLFlip:= FlipMove[np^.RLFlip,m];
m:= SymMove[16,m];
np1^.FBTwist:= TwistMove[np^.FBTwist,m];
x:= UDSliceSortedSymMove[np^.FBSliceSortedSymIdx,SymMove[np^.FBSliceSortedSymSym,m]];
np1^.FBSliceSortedSymSym:=SymMult[x and 15,np^.FBSliceSortedSymSym];
np1^.FBSliceSortedSymIdx:= x shr 4;
np1^.FBFlip:= FlipMove[np^.FBFlip,m];
np1^.UDPrunBig:= GetPruningLength[np^.UDPrunBig,
GetPruningBigP((Int64(np1^.UDSliceSortedSymIdx)*2048+
FlipConjugate[np1^.UDFlip,np1^.UDSliceSortedSymSym,np1^.UDSliceSortedSymIdx])*2187+
TwistConjugate[np1^.UDTwist,np1^.UDSliceSortedSymSym])];
np1^.RLPrunBig:= GetPruningLength[np^.RLPrunBig,
GetPruningBigP((Int64(np1^.RLSliceSortedSymIdx)*2048+
FlipConjugate[np1^.RLFlip,np1^.RLSliceSortedSymSym,np1^.RLSliceSortedSymIdx])*2187+
TwistConjugate[np1^.RLTwist,np1^.RLSliceSortedSymSym])];
np1^.FBPrunBig:= GetPruningLength[np^.FBPrunBig,
GetPruningBigP((Int64(np1^.FBSliceSortedSymIdx)*2048+
FlipConjugate[np1^.FBFlip,np1^.FBSliceSortedSymSym,np1^.FBSliceSortedSymIdx])*2187+
TwistConjugate[np1^.FBTwist,np1^.FBSliceSortedSymSym])];
if not Terminated then
begin
{$IF QTM}
if (not slicemode) and not (Odd(r_depth) xor np1^.parityEven) then goto incAxis;//im slicemode ändern manche Züge nicht die Parität der Ecken!
{$IFEND}
//if we do the three possible moves on some fixed face in FTM, the three pruning value
//can only differ by 1
//Wie beim Huge Solver kann gefolgert werden, dass der Pruning
//Wert eins höher ist, wenn alle gleich sind.
if (r_depth>0) and (np1^.UDPrunBig=np1^.RLPrunBig)
and (np1^.RLPrunBig=np1^.FBPrunBig) then
begin
{$IF not QTM}
if (np1^.UDPrunBig>r_depth) then goto incAxis;
if (np1^.UDPrunBig>r_depth-1) then goto incPower;
end;
if (np1^.UDPrunBig>r_depth+1) or (np1^.RLPrunBig>r_depth+1) or (np1^.FBPrunBig>r_depth+1) then goto incAxis;
if (np1^.UDPrunBig>r_depth) or (np1^.RLPrunBig>r_depth) or (np1^.FBPrunBig>r_depth) then goto incPower;
{$ELSE}
if (np1^.UDPrunBig>r_depth+1) then goto incAxis; //QTM r_depth+1
if (np1^.UDPrunBig>r_depth-1) then goto incPower;
end;
if (np1^.UDPrunBig>r_depth+2) or (np1^.RLPrunBig>r_depth+2) or (np1^.FBPrunBig>r_depth+2) then goto incAxis; //QTM r_depth+2
if (np1^.UDPrunBig>r_depth) or (np1^.RLPrunBig>r_depth) or (np1^.FBPrunBig>r_depth) then goto incPower;
{$IFEND}
end;
{$IFEND} //Huge Optimal Solver
end
else//standard optimal solver, löst auch oriented cubes
begin
if isOriented then
begin
np1^.UDSliceSorted:= UDSliceSortedMove[np^.UDSliceSorted,m];//für das Pruning aus UDSliceSorted und UDcenTwist 11880*4096
np1^.UDcenTwist:= CentOriMove[np^.UDcenTwist,m];
np1^.UDTwist:= TwistMove[np^.UDTwist,m];
np1^.UDCentRFLBMod2Twist:= CentOriRFLBMod2Move[np^.UDCentRFLBMod2Twist,m];
x:= flipSliceMove[np^.UDIdx,Ord(SymMove[np^.UDSym,m])];//wird für das Pruning aus flipslice, twist und centrflbMod2 gebraucht
np1^.UDSym:=SymMult[x and 15,np^.UDSym];
np1^.UDIdx:= x shr 4;
m:= SymMove[16,m];
np1^.RLSliceSorted:= UDSliceSortedMove[np^.RLSliceSorted,m];
np1^.RLcenTwist:= CentOriMove[np^.RLcenTwist,m];
np1^.RLTwist:= TwistMove[np^.RLTwist,m];
np1^.RLCentRFLBMod2Twist:= CentOriRFLBMod2Move[np^.RLCentRFLBMod2Twist,m];
x:= flipSliceMove[np^.RLIdx,Ord(SymMove[np^.RLSym,m])];
np1^.RLSym:=SymMult[x and 15,np^.RLSym];
np1^.RLIdx:= x shr 4;
m:= SymMove[16,m];
np1^.FBSliceSorted:= UDSliceSortedMove[np^.FBSliceSorted,m];
np1^.FBcenTwist:= CentOriMove[np^.FBcenTwist,m];
np1^.FBTwist:= TwistMove[np^.FBTwist,m];
np1^.FBCentRFLBMod2Twist:= CentOriRFLBMod2Move[np^.FBCentRFLBMod2Twist,m];
x:= flipSliceMove[np^.FBIdx,Ord(SymMove[np^.FBSym,m])];
np1^.FBSym:=SymMult[x and 15,np^.FBSym];
np1^.FBIdx:= x shr 4;
np1^.UDPrun:= GetPruningLength[np^.UDPrun,
GetPruningCentP((Int64(np1^.UDIdx)*16+
CentOriRFLBMod2Conjugate[np1^.UDCentRFLBMod2Twist,np1^.UDSym])*2187+
TwistConjugate[np1^.UDTwist,np1^.UDSym])];
np1^.RLPrun:= GetPruningLength[np^.RLPrun,
GetPruningCentP((Int64(np1^.RLIdx)*16+
CentOriRFLBMod2Conjugate[np1^.RLCentRFLBMod2Twist,np1^.RLSym])*2187+
TwistConjugate[np1^.RLTwist,np1^.RLSym])];
np1^.FBPrun:= GetPruningLength[np^.FBPrun,
GetPruningCentP((Int64(np1^.FBIdx)*16+
CentOriRFLBMod2Conjugate[np1^.FBCentRFLBMod2Twist,np1^.FBSym])*2187+
TwistConjugate[np1^.FBTwist,np1^.FBSym])];
end
else
begin
np1^.UDTwist:= TwistMove[np^.UDTwist,m];
x:= flipSliceMove[np^.UDIdx,Ord(SymMove[np^.UDSym,m])];
np1^.UDSym:=SymMult[x and 15,np^.UDSym];
np1^.UDIdx:= x shr 4;
m:= SymMove[16,m];
np1^.RLTwist:= TwistMove[np^.RLTwist,m];
x:= flipSliceMove[np^.RLIdx,Ord(SymMove[np^.RLSym,m])];
np1^.RLSym:=SymMult[x and 15,np^.RLSym];
np1^.RLIdx:= x shr 4;
m:= SymMove[16,m];
np1^.FBTwist:= TwistMove[np^.FBTwist,m];
x:= flipSliceMove[np^.FBIdx,Ord(SymMove[np^.FBSym,m])];
np1^.FBSym:=SymMult[x and 15,np^.FBSym];
np1^.FBIdx:= x shr 4;
np1^.UDPrun:= GetPruningLength[np^.UDPrun,GetPruningP(2187*np1^.UDIdx + TwistConjugate[np1^.UDTwist,np1^.UDSym])];
np1^.RLPrun:= GetPruningLength[np^.RLPrun,GetPruningP(2187*np1^.RLIdx + TwistConjugate[np1^.RLTwist,np1^.RLSym])];
np1^.FBPrun:= GetPruningLength[np^.FBPrun,GetPruningP(2187*np1^.FBIdx + TwistConjugate[np1^.FBTwist,np1^.FBSym])];
end;
if not Terminated then
begin
{$IF QTM}
if (not slicemode) and not (Odd(r_depth) xor np1^.parityEven) then goto incAxis;//im slicemode ändern manche Züge nicht die Parität der Ecken!
{$IFEND}
//Wie beim Huge Solver kann gefolgert werden, dass der Pruning
//Wert eins höher ist, wenn alle gleich sind.
if (r_depth>0) and (np1^.UDPrun=np1^.RLPrun)
and (np1^.RLPrun=np1^.FBPrun) then
begin
{$IF not QTM}
if (np1^.UDPrun>r_depth) then goto incAxis;
if (np1^.UDPrun>r_depth-1) then goto incPower;
end;
if (np1^.UDPrun>r_depth+1) or (np1^.RLPrun>r_depth+1) or (np1^.FBPrun>r_depth+1) then goto incAxis;
if (np1^.UDPrun>r_depth) or (np1^.RLPrun>r_depth) or (np1^.FBPrun>r_depth) then goto incPower;
{$ELSE}
if (np1^.UDPrun>r_depth+1) then goto incAxis; //QTM r_depth+1
if (np1^.UDPrun>r_depth-1) then goto incPower;
end;
if (np1^.UDPrun>r_depth+2) or (np1^.RLPrun>r_depth+2) or (np1^.FBPrun>r_depth+2) then goto incAxis; //QTM r_depth+2
if (np1^.UDPrun>r_depth) or (np1^.RLPrun>r_depth) or (np1^.FBPrun>r_depth) then goto incPower;
{$IFEND}
end;
if isOriented then //solte dass nicht besser in die terminated Schleife?
begin
if (PruningCenTwistUDSliceSorted[np1^.UDSliceSorted shl 12 +np1^.UDcenTwist]>r_depth) then goto incPower;
if (PruningCenTwistUDSliceSorted[np1^.RLSliceSorted shl 12 +np1^.RLcenTwist]>r_depth) then goto incPower;
if (PruningCenTwistUDSliceSorted[np1^.FBSliceSorted shl 12 +np1^.FBcenTwist]>r_depth)then goto incPower;
end;
end;//standard Solver
//Symmetriecheck machen!
if (sym>1) and (depth - r_depth<=2) then //statt 3 kann man auch anderes nehmen
begin
fixNum:= depth - r_depth;
t_sym:=sym;
for i:= 1 to 47 do //Fall 0 uninteressant
begin
t_sym:= t_sym shr 1;
if not odd(t_sym) then continue;
np1:=np;
//zunächst mal die Bilder berechnen
for j:=fixNum downto 0 do Dec(np1);
//Züge werden verworfen, wenn die lex. Ordnung einer Transformation kleiner ist.
//Die Reihenfolge für parallele Achsen kann durch die Transf. vertauscht sein, das erhöht aber
//immer die lex. Ordnung. Es kann also höchstens passieren, dass ein Zug nicht
//verworfen wird, obwohl es möglich wäre.
//Man kann sich klarmachen, dass man nicht nur die Potenz, sondern immer
//auch die Achse (zumindest in FTM) verwerfen kann!
//Der lex. Unterschied ist immer! in der letzten Stelle, falls die Transf. kleiner,
//sonst wäre er ja schon vorher erkannt worden.
//Dann ist die Achse des Bildes kleiner oder die Potenz, falls die Achse die
//gleiche ist. Dann kommt aber nur x^3 -> x^1 in Frage, und dann wird die Achse
//im nächsten Zug sowieso erhöht.
for j:= 0 to fixNum do
begin
Inc(np1);
m:= Move(3*Ord(np1^.axis) + np1^.power-1);
m1:= SymMove[i,m];
if m1 < m then ///////////////////////////////////////////////
goto incAxis //kleiner, verwerfen
else if m1 > m then break;//größer
//bei Gleichheit nächste Stelle untersuchen
end;
end;//i
end;//if sym>1
//Ende Symmetriecheck
if (r_depth=0) then
begin
if Terminated then
begin
returnLength:=-2;
Result:=returnLength; //abort
goto ende;
end;
{$IF SPECIAL4}
if depth>12 then //bei 13 ist maximale Länge 14
begin
returnLength:=0;
Result:=returnLength; //abort
goto ende;
end;
{$IFEND}
if (not IsIdCube) then goto incPower;
returnLength:=depth+1;
Result:=returnLength; //solution
n[depth+1].nodenum:=nodecount;
PostMessage(Form1.Handle,WM_NEXTLEVEL,Integer(self),depth+100);//TForm1.ShowNextLevel
goto ende;
end;
right:
Dec(r_depth);
Inc(np); np^.axis:= U;
goto checkNeighbourAxis;
incAxis:
// if np^.axis=B then goto left;
// Inc(np^.axis);
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
if (not sliceMode and (np^.axis=B)) or (np^.axis=Fs) then goto left;
Inc(np^.axis);
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
checkNeighbourAxis:
if depth<>r_depth then //else no left neighbour
begin
np1:=np; Dec(np1);
{$IF not QTM}
if (np^.axis=np1^.axis) then goto incAxis;//in FTM no successive moves with same axis
{$ELSE}
if (np^.axis= np1^.axis) and (np1^.power = 3) then goto incAxis;//In QTM only X*X, not X'*X
np2:=np1;Dec(np2);
if (depth-r_depth>1) and //else no neighbour left of left neighbour
(np^.axis= np1^.axis) and (np^.axis=np2^.axis) then goto incAxis;
//in QTM not three successive moves with same axis
{$IFEND}
if np^.axis<=F then
if TurnAxis(Ord(np^.axis)+3)=np1^.axis then goto incAxis;//no D*U, L*R etc.
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
if sliceMode then
begin
if np^.axis<=F then
if TurnAxis(Ord(np^.axis)+6)=np1^.axis then goto incAxis;//no Us*U, Rs*R etc.
if np^.axis<=B then
if TurnAxis(Ord(np^.axis)+3)=np1^.axis then goto incAxis;//no Us*D, Rs*L etc.
//D*U, L*R already done
if np^.axis>=Us then
if TurnAxis(Ord(np^.axis)- 3)=np1^.axis then goto incAxis;//no D*Us, L*Rs etc.
if np^.axis>=Us then
if TurnAxis(Ord(np^.axis)- 6)=np1^.axis then goto incAxis;//no U*Us, R*Rs et.
end;
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
end;
// np^.power:=1;//all checks ok
// goto turn;
np^.power:=0;//all checks ok
goto incpower;
left:
if depth = r_depth then//depth is incremented
begin
if depth=maxLength-1 then
begin
Result:=-1;
goto ende
end;
Inc(depth);Inc(r_depth);
n[depth].nodenum:=nodecount;
PostMessage(Form1.Handle,WM_NEXTLEVEL,Integer(self),depth+1);//Communicate
np^.axis:=U;
np^.power:=1;
goto turn;
end
else
begin
Dec(np);
Inc(r_depth);
if inValid>depth-r_depth then inValid:=depth-r_depth;
goto incPower;
end;
ende:
end;
//++++++++++++++++End find next solution of optimal solver+++++++++++++++++++++++
//++++++++++++++++Check if the Phase 1 solution is a solved cube++++++++++++++++
function IDA.IsIdCube: Boolean;
var i: Integer;
m:Move;
np: ^Node;
begin
if isOriented then //dann wird im Augenblick nur der Standardsolver verwendet, da die großen nichts bringen
begin
for i:=invalid to depth do
begin
m:= Move(3*Ord(n[i].axis) + n[i].power - 1);
// n[i+1].UDcenTwist:= CentOriMove[n[i].UDcenTwist,m]; //beim Standard Solver nicht nötig
n[i+1].cornPos:=CornPermMove[n[i].cornPos,m];
// n[i+1].UDSliceSorted:=UDSliceSortedMove[n[i].UDSliceSorted,m]; //wird beim Standardsolver mit Orientierung mitgeführt
// m:= SymMove[16,m];
// n[i+1].RLSliceSorted:=UDSliceSortedMove[n[i].RLSliceSorted,m];
// m:= SymMove[16,m];
// n[i+1].FBSliceSorted:=UDSliceSortedMove[n[i].FBSliceSorted,m];
end;
end
else
begin
for i:=invalid to depth do
begin
m:= Move(3*Ord(n[i].axis) + n[i].power - 1);
n[i+1].cornPos:=CornPermMove[n[i].cornPos,m];
n[i+1].UDSliceSorted:=UDSliceSortedMove[n[i].UDSliceSorted,m]; //Nötig bei Standardsolver ohne Orientierung und beim UHUGE solver
m:= SymMove[16,m];
n[i+1].RLSliceSorted:=UDSliceSortedMove[n[i].RLSliceSorted,m];
m:= SymMove[16,m];
n[i+1].FBSliceSorted:=UDSliceSortedMove[n[i].FBSliceSorted,m];
end;
end;
inValid:=depth;
np:= @n[depth+1];
if (np^.cornPos=0) and (np^.UDSliceSorted=0)and (np^.RLSliceSorted=0) and (np^.FBSliceSorted=0)
then Result:=True
else Result:=False;
// if isOriented and (np^.UDCenTwist<>0) then Result:=False;
end;
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//++++++++++++++++Find next solution for Two-Phase-Algorithm++++++++++++++++++++
{$IF not QTM}
function IDA.Next2PhaseSolution: Integer;
var np,np1,np2: ^Node;
x,r_depth: Integer;
m: Move;
label incPower,turn,right,incAxis,checkNeighbourAxis,left,ende,phase2;
begin
np:= @n[depth];
r_depth:=0;
incPower:
Inc(np^.power);
if (np^.power>3) then goto incAxis;
if sliceMode and (depth<>r_depth) then //else no left neighbour
begin
np1:=np;Dec(np1);
if (np1^.axis<=F) and (np^.axis=Turnaxis(Ord(np1^.axis)+3))
and (np1^.power + np^.power = 4) then goto incPower;// goto incAxis; //kein UD', U2D2, U'D etc wenn slices
end;
turn:
np1:=np;
Inc(np1);
Inc(NodeCount);
m:= Move(3*Ord(np^.axis) + np^.power - 1);
// np1^.cenTwist:= CentOriMove[np^.cenTwist,m];
if isOriented then
begin
np1^.UDTwist:= TwistMove[np^.UDTwist,m];
np1^.UDCentRFLBMod2Twist:= CentOriRFLBMod2Move[np^.UDCentRFLBMod2Twist,m];
x:= flipSliceMove[np^.UDIdx,Ord(SymMove[np^.UDSym,m])];
np1^.UDSym:=SymMult[x and 15,np^.UDSym];
np1^.UDIdx:= x shr 4;
np1^.UDPrun:= GetPruningLength[np^.UDPrun,
GetPruningCentP((Int64(np1^.UDIdx)*16+
CentOriRFLBMod2Conjugate[np1^.UDCentRFLBMod2Twist,np1^.UDSym])*2187+
TwistConjugate[np1^.UDTwist,np1^.UDSym])];
end
else
begin
np1^.UDTwist:= TwistMove[np^.UDTwist,m];
x:= flipSliceMove[np^.UDIdx,Ord(SymMove[np^.UDSym,m])];
np1^.UDSym:=SymMult[x and 15,np^.UDSym];
np1^.UDIdx:= x shr 4;
np1^.UDPrun:= GetPruningLength[np^.UDPrun,GetPruningP(2187*np1^.UDIdx + TwistConjugate[np1^.UDTwist,np1^.UDSym])];
end;
if (np1^.UDPrun>r_depth+1) then goto incAxis;
if (np1^.UDPrun>r_depth) or (( np1^.UDPrun=0) and (r_depth>0)) then goto incPower;
//we deny phase2 states within phase1, so maybe we loose the best solution
//if (np1^.UDPrun>r_depth) then goto incPower; //Version, die auch Phase2 states erlaubt!
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// if sliceMode and (depth<>r_depth) then //else no left neighbour
// begin
// np2:=np;Dec(np2);
// if (np2^.axis<=F) and (np^.axis=Turnaxis(Ord(np2^.axis)+3))
// and (np2^.power + np^.power = 4) then
// goto incPower;// goto incAxis; //kein UD', U2D2, U'D etc wenn slices
// end;
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
if (r_depth=0) then
begin
phase2:
if Terminated then
begin
returnLength:=-2;
Result:=returnLength;
goto ende;
end;
//returnLength:=depth+1;Result:=depth+1; goto ende;
NextPhase2ID;
case depth2 of
-2: goto incPower;
-3: goto incAxis;
-4: goto left;
end;
if (depth=0) and (depth2=0) and (n[0].axis=U) and (n[0].power=1)
and (n[1].axis=U) and (n[1].power=3) then // in case of Id cube
begin
returnLength:=2;
Result:=2;
goto ende;
end;
// we do not allow UU,RR,FF,DU,DD,LL,LR,BB,BF between phase1 and 2
if depth2>=0 then
begin
if n[depth].axis=n[depth+1].axis then goto incPower;
if Ord(n[depth].axis)= Ord(n[depth+1].axis)+3 then goto incPower;
end;
returnLength:=depth+depth2+2;
Result:=returnLength; //solution
goto ende;
end;
right:
Dec(r_depth);
Inc(np); np^.axis:= U;
goto checkNeighbourAxis;
incAxis:
// if np^.axis=B then goto left;
// Inc(np^.axis);
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
if (not sliceMode and (np^.axis=B)) or (np^.axis=Fs) then goto left;
Inc(np^.axis);
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
checkNeighbourAxis:
if depth<>r_depth then //else no left neighbour
begin
np1:=np; Dec(np1);
if (np^.axis=np1^.axis) then goto incAxis;//in FTM no successive moves with same axis
if np^.axis<=F then
if TurnAxis(Ord(np^.axis)+3)=np1^.axis then goto incAxis;//no D*U, L*R etc.
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
if sliceMode then
begin
if np^.axis<=F then
if TurnAxis(Ord(np^.axis)+6)=np1^.axis then goto incAxis;//no Us*U, Rs*R etc.
if np^.axis<=B then
if TurnAxis(Ord(np^.axis)+3)=np1^.axis then goto incAxis;//no Us*D, Rs*L etc.
//D*U, L*R already done
if np^.axis>=Us then
if TurnAxis(Ord(np^.axis)- 3)=np1^.axis then goto incAxis;//no D*Us, L*Rs etc.
if np^.axis>=Us then
if TurnAxis(Ord(np^.axis)- 6)=np1^.axis then goto incAxis;//no U*Us, R*Rs et.
end;
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
end;
// np^.power:=1;
// goto turn;
np^.power:=0;
goto incpower;
left:
if depth = r_depth then//depth is incremented
begin
nodeCount:=0;
if depth>=maxLength-1 then
begin
returnLength:=-1;
Result:=returnLength; //no more solution
goto ende
end;
Inc(depth);Inc(r_depth);
np^.axis:=U;
np^.power:=1;
goto turn;
end
else
begin
Dec(np);
Inc(r_depth);
if inValid>depth-r_depth then inValid:=depth-r_depth;
goto incPower;
end;
ende:
end;
{$ELSE}
function IDA.Next2PhaseSolution: Integer;
var np,np1,np2: ^Node;
x,r_depth: Integer;
m: Move;
label incPower,turn,right,incAxis,checkNeighbourAxis,left,ende,phase2;
begin
np:= @n[depth];
r_depth:=0;
incPower:
Inc(np^.power);
if (np^.power=2) then Inc(np^.power);
if (np^.power>3) then goto incAxis;
if (depth<>r_depth) then //sonst kein linker Nachbar
begin
np1:=np; Dec(np1);
if (np1^.axis=np^.axis)and ((np1^.power=3) or (np^.power=3)) then goto incAxis; //only X*X, not X*X',X'*X or X'*X'
if slicemode and (np1^.axis<=F) and (np^.axis=Turnaxis(Ord(np1^.axis)+3))
and (np1^.power + np^.power = 4) then goto incPower;//kein UD', U2D2, U'D etc wenn slices
end;
turn:
np1:=np;
Inc(np1);
Inc(NodeCount);
//np1^.parityEven:= not np^.parityEven;
m:= Move(3*Ord(np^.axis) + np^.power - 1);
// np1^.cenTwist:= CentOriMove[np^.cenTwist,m];
if isOriented then
begin
np1^.UDTwist:= TwistMove[np^.UDTwist,m];
np1^.UDCentRFLBMod2Twist:= CentOriRFLBMod2Move[np^.UDCentRFLBMod2Twist,m];
x:= flipSliceMove[np^.UDIdx,Ord(SymMove[np^.UDSym,m])];
np1^.UDSym:=SymMult[x and 15,np^.UDSym];
np1^.UDIdx:= x shr 4;
np1^.UDPrun:= GetPruningLength[np^.UDPrun,
GetPruningCentP((Int64(np1^.UDIdx)*16+
CentOriRFLBMod2Conjugate[np1^.UDCentRFLBMod2Twist,np1^.UDSym])*2187+
TwistConjugate[np1^.UDTwist,np1^.UDSym])];
end
else
begin
np1^.UDTwist:= TwistMove[np^.UDTwist,m];
x:= flipSliceMove[np^.UDIdx,Ord(SymMove[np^.UDSym,m])];
np1^.UDSym:=SymMult[x and 15,np^.UDSym];
np1^.UDIdx:= x shr 4;
np1^.UDPrun:= GetPruningLength[np^.UDPrun,GetPruningP(2187*np1^.UDIdx + TwistConjugate[np1^.UDTwist,np1^.UDSym])];
end;
if (np1^.UDPrun>r_depth+2) then goto incAxis;
if (np1^.UDPrun>r_depth) or (( np1^.UDPrun=0) and (r_depth>0)) then goto incPower;
//we deny phase2 states within phase1, so maybe we loose the best solution
//if (np1^.UDPrun>r_depth) then goto incPower; //Version, die auch Phase2 states erlaubt!
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//if sliceMode and (depth<>r_depth) then //else no left neighbour
// begin
// np2:=np;Dec(np2);
// if (np2^.axis<=F) and (np^.axis=Turnaxis(Ord(np2^.axis)+3))
// and (np2^.power + np^.power = 4) then
// goto incPower;// goto incAxis; //kein UD', U2D2, U'D etc wenn slices , WARUM NICHT im TURN TEIL Vorsicht mit inc/dec np1
// end;
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
if (r_depth=0) then
begin
phase2:
if Terminated then
begin
returnLength:=-2;
Result:=returnLength;
goto ende;
end;
//returnLength:=depth+1;Result:=depth+1; goto ende;
NextPhase2ID;
case depth2 of
-2: goto incPower;
-3: goto incAxis;
-4: goto left;
end;
if (depth=0) and (depth2=0) and (n[0].axis=U) and (n[0].power=1)
and (n[1].axis=U) and (n[1].power=3) then // in case of Id cube
begin
returnLength:=2;
Result:=2;
goto ende;
end;
// we do not allow UU,RR,FF,DU,DD,LL,LR,BB,BF between phase1 and 2
if depth2>=0 then
begin
// if n[depth].axis=n[depth+1].axis then goto incPower;
if Ord(n[depth].axis)= Ord(n[depth+1].axis)+3 then goto incPower;
end;
returnLength:=depth+depth2+2;
Result:=returnLength; //solution
goto ende;
end;
right:
Dec(r_depth);
Inc(np); np^.axis:= U;
np^.virtualmove:=false;//keine virtual moves in phase 1
goto checkNeighbourAxis;
incAxis:
// if np^.axis=B then goto left;
// Inc(np^.axis);
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
if (not sliceMode and (np^.axis=B)) or (np^.axis=Fs) then goto left;
Inc(np^.axis);
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
checkNeighbourAxis:
if depth<>r_depth then //else no left neighbour