-
Notifications
You must be signed in to change notification settings - Fork 1
/
yruru.cpp
1849 lines (1519 loc) · 60.6 KB
/
yruru.cpp
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
/* This file is part of "GR Cube"
Copyright (C) 2022 German Ramos Rodriguez
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
German Ramos Rodriguez
Vigo, Spain
grvigo@hotmail.com
*/
#include "yruru.h"
#include "roux.h"
#include "cfop.h"
#include "collection.h"
#include <chrono>
#include <algorithm>
namespace grcube3
{
// Reset the search results
void YruRU::Reset()
{
for (int i = 0; i < 24; i++)
{
Inspections[i].clear();
Lines[i].clear();
CPLines[i].clear();
pEO[i].clear();
EO[i].clear();
EOBF[i].clear();
F2L[i].clear();
Alg2GLL[i].clear();
Cases2GLL[i].clear();
}
MaxDepthCPLines = MaxDepthpEO = MaxDepthEO = MaxDepthEOBF = MaxDepthF2L = 0u;
TimeLines = TimeCPLines = TimepEO = TimeEO = TimeEOBF = TimeF2L = Time2GLL = 0.0;
SearchSpins.clear();
for (int s = 0; s < 24; s++) SearchSpins.push_back(static_cast<Spn>(s));
Metric = Metrics::Movements; // Default metric
}
// Search the best Lines solve algorithms with the given search depth - CP solves discarded
// Return false if no Lines found
bool YruRU::SearchLines(const uint MaxDepth, const uint MaxSolves)
{
const auto time_lines_start = std::chrono::system_clock::now();
MaxDepthCPLines = (MaxDepth < 4u ? 4u : MaxDepth);
DeepSearch DSLines(Scramble); // Deep search for Lines
DSLines.AddToOptionalPieces(Pgr::CPLINE_UF); // CPLINE_RB
DSLines.AddToOptionalPieces(Pgr::CPLINE_UB); // CPLINE_LF
DSLines.AddToOptionalPieces(Pgr::CPLINE_UR); // CPLINE_BL
DSLines.AddToOptionalPieces(Pgr::CPLINE_UL); // CPLINE_FR
DSLines.AddToOptionalPieces(Pgr::CPLINE_DF); // CPLINE_LB
DSLines.AddToOptionalPieces(Pgr::CPLINE_DB); // CPLINE_RF
DSLines.AddToOptionalPieces(Pgr::CPLINE_DR); // CPLINE_FL
DSLines.AddToOptionalPieces(Pgr::CPLINE_DL); // CPLINE_BR
DSLines.AddToOptionalPieces(Pgr::CPLINE_FU); // CPLINE_LD
DSLines.AddToOptionalPieces(Pgr::CPLINE_FD); // CPLINE_RU
DSLines.AddToOptionalPieces(Pgr::CPLINE_BU); // CPLINE_RD
DSLines.AddToOptionalPieces(Pgr::CPLINE_BD); // CPLINE_LU
// First level is extended in the search to improve the multithreading - first level will not be checked
// (it's supose that the first block not will be solved in a single movement)
const SearchUnit URoot(SequenceType::DOUBLE);
const SearchUnit U(SequenceType::SINGLE);
SearchLevel L_Root(SearchCheck::NO_CHECK);
L_Root.Add(URoot);
SearchLevel L_Check(SearchCheck::CHECK);
L_Check.Add(U);
SearchLevel L_NoCheck(SearchCheck::NO_CHECK);
L_NoCheck.Add(U);
DSLines.AddSearchLevel(L_Root); // Level 1 (two steps -DOUBLE- root algorithms)
DSLines.AddSearchLevel(L_Check); // Level 2
DSLines.AddSearchLevel(L_Check); // Level 3
for (uint l = 4u; l < MaxDepthCPLines; l++) DSLines.AddSearchLevel(L_Check); // Levels 4 to MaxDepth
DSLines.UpdateRootData();
// DSLines.SetMinDeep(DSLine.GetMaxDepth() - 2u);
DSLines.Run(Cores);
Cores = DSLines.GetCoresUsed(); // Update to the real number of cores used
EvaluateLines(DSLines.Solves, MaxSolves);
const std::chrono::duration<double> lines_elapsed_seconds = std::chrono::system_clock::now() - time_lines_start;
TimeLines = lines_elapsed_seconds.count();
return !DSLines.Solves.empty();
}
// Search the best CP solve algorithms
void YruRU::SearchCP()
{
const auto time_cp_start = std::chrono::system_clock::now();
// First level is extended in the search to improve the multithreading - first level will not be checked
// (it's supose that the first block not will be solved in a single movement)
const SearchUnit URoot(SequenceType::DOUBLE);
const SearchUnit U(SequenceType::SINGLE);
SearchLevel L_Root(SearchCheck::NO_CHECK);
L_Root.Add(URoot);
SearchLevel L_Check(SearchCheck::CHECK);
L_Check.Add(U);
SearchLevel L_NoCheck(SearchCheck::NO_CHECK);
L_NoCheck.Add(U);
for (const auto spin : SearchSpins)
{
int sp = static_cast<int>(spin);
CPLines[sp].clear();
Pgr LINE;
switch (spin)
{
case Spn::UF: LINE = Pgr::CPLINE_UF; break;
case Spn::UB: LINE = Pgr::CPLINE_UB; break;
case Spn::UR: LINE = Pgr::CPLINE_UR; break;
case Spn::UL: LINE = Pgr::CPLINE_UL; break;
case Spn::DF: LINE = Pgr::CPLINE_DF; break;
case Spn::DB: LINE = Pgr::CPLINE_DB; break;
case Spn::DR: LINE = Pgr::CPLINE_DR; break;
case Spn::DL: LINE = Pgr::CPLINE_DL; break;
case Spn::FU: LINE = Pgr::CPLINE_FU; break;
case Spn::FD: LINE = Pgr::CPLINE_FD; break;
case Spn::FR: LINE = Pgr::CPLINE_FR; break;
case Spn::FL: LINE = Pgr::CPLINE_FL; break;
case Spn::BU: LINE = Pgr::CPLINE_BU; break;
case Spn::BD: LINE = Pgr::CPLINE_BD; break;
case Spn::BR: LINE = Pgr::CPLINE_BR; break;
case Spn::BL: LINE = Pgr::CPLINE_BL; break;
case Spn::RU: LINE = Pgr::CPLINE_RU; break;
case Spn::RD: LINE = Pgr::CPLINE_RD; break;
case Spn::RF: LINE = Pgr::CPLINE_RF; break;
case Spn::RB: LINE = Pgr::CPLINE_RB; break;
case Spn::LU: LINE = Pgr::CPLINE_LU; break;
case Spn::LD: LINE = Pgr::CPLINE_LD; break;
case Spn::LF: LINE = Pgr::CPLINE_LF; break;
case Spn::LB: LINE = Pgr::CPLINE_LB; break;
default: return; // Should not happend
}
for (uint n = 0u; n < Lines[sp].size(); n++)
{
Algorithm AlgStart = Scramble;
AlgStart.Append(Inspections[sp][n]);
AlgStart.Append(Lines[sp][n]);
Cube CubeYruRU(AlgStart);
if (!IsLineBuilt(CubeYruRU, spin))
{
CPLines[sp].push_back(Algorithm(""));
continue;
}
DeepSearch DSCP(AlgStart); // Deep search for CP
DSCP.AddToMandatoryPieces(LINE);
DSCP.AddSearchLevel(L_Root);
for (uint l = 2u; l < 4u; l++) DSCP.AddSearchLevel(L_Check); // Add needed search levels
DSCP.UpdateRootData();
DSCP.Run(Cores);
Cores = DSCP.GetCoresUsed(); // Update to the real number of cores used
std::vector<Algorithm> Solves;
EvaluateCPLinesResult(Solves, 1u, DSCP.Solves, CubeYruRU, spin);
if (Solves.empty()) CPLines[sp].push_back("");
else CPLines[sp].push_back(Solves[0]);
}
}
const std::chrono::duration<double> cp_elapsed_seconds = std::chrono::system_clock::now() - time_cp_start;
TimeCPLines = cp_elapsed_seconds.count();
}
// Search the best CP-Lines solve algorithms with the given search depth
// Return false if no CP-Lines found
bool YruRU::SearchCPLines(const uint MaxDepth, const uint MaxSolves)
{
const auto time_cplines_start = std::chrono::system_clock::now();
MaxDepthCPLines = (MaxDepth < 4u ? 4u : MaxDepth);
DeepSearch DSCPLines(Scramble); // Deep search for CP-Line
DSCPLines.AddToOptionalPieces(Pgr::CPLINE_UF); // CPLINE_RB
DSCPLines.AddToOptionalPieces(Pgr::CPLINE_UB); // CPLINE_LF
DSCPLines.AddToOptionalPieces(Pgr::CPLINE_UR); // CPLINE_BL
DSCPLines.AddToOptionalPieces(Pgr::CPLINE_UL); // CPLINE_FR
DSCPLines.AddToOptionalPieces(Pgr::CPLINE_DF); // CPLINE_LB
DSCPLines.AddToOptionalPieces(Pgr::CPLINE_DB); // CPLINE_RF
DSCPLines.AddToOptionalPieces(Pgr::CPLINE_DR); // CPLINE_FL
DSCPLines.AddToOptionalPieces(Pgr::CPLINE_DL); // CPLINE_BR
DSCPLines.AddToOptionalPieces(Pgr::CPLINE_FU); // CPLINE_LD
DSCPLines.AddToOptionalPieces(Pgr::CPLINE_FD); // CPLINE_RU
DSCPLines.AddToOptionalPieces(Pgr::CPLINE_BU); // CPLINE_RD
DSCPLines.AddToOptionalPieces(Pgr::CPLINE_BD); // CPLINE_LU
// First level is extended in the search to improve the multithreading - first level will not be checked
// (it's supose that the first block not will be solved in a single movement)
const SearchUnit URoot(SequenceType::DOUBLE);
const SearchUnit U(SequenceType::SINGLE);
SearchLevel L_Root(SearchCheck::NO_CHECK);
L_Root.Add(URoot);
SearchLevel L_Check(SearchCheck::CHECK);
L_Check.Add(U);
SearchLevel L_NoCheck(SearchCheck::NO_CHECK);
L_NoCheck.Add(U);
DSCPLines.AddSearchLevel(L_Root); // Level 1 (two steps -DOUBLE- root algorithms)
DSCPLines.AddSearchLevel(L_Check); // Level 2
DSCPLines.AddSearchLevel(L_Check); // Level 3
for (uint l = 4u; l < MaxDepthCPLines; l++) DSCPLines.AddSearchLevel(L_Check); // Levels 4 to MaxDepth
DSCPLines.UpdateRootData();
// DSCPLines.SetMinDeep(DSCPLine.GetMaxDepth() - 2u);
DSCPLines.Run(Cores);
Cores = DSCPLines.GetCoresUsed(); // Update to the real number of cores used
EvaluateCPLines(DSCPLines.Solves, MaxSolves);
const std::chrono::duration<double> cplines_elapsed_seconds = std::chrono::system_clock::now() - time_cplines_start;
TimeCPLines = cplines_elapsed_seconds.count();
return !DSCPLines.Solves.empty();
}
// Search the best lines solve algorithms from an algorithms vector
void YruRU::EvaluateLines(const std::vector<Algorithm>& Solves, const uint MaxSolves)
{
// Best line solve algorithms adapted to spin
for (const auto spin : SearchSpins)
{
int sp = static_cast<int>(spin);
EvaluateLinesResult(Lines[sp], MaxSolves, Solves, CubeBase, spin);
}
for (int sp = 0; sp < 24; sp++)
{
Spn spin = static_cast<Spn>(sp);
for (auto& line : Lines[sp])
{
Stp T1, T2;
Cube::GetSpinsSteps(CubeBase.GetSpin(), spin, T1, T2);
Algorithm Insp;
if (T1 != Stp::NONE) { Insp.Append(T1); line.TransformTurn(T1); }
if (T2 != Stp::NONE) { Insp.Append(T2); line.TransformTurn(T2); }
Inspections[sp].push_back(Insp);
}
}
}
// Search the best CP-Lines solve algorithms from an algorithms vector
void YruRU::EvaluateCPLines(const std::vector<Algorithm>& Solves, const uint MaxSolves)
{
// Best CP-Line solve algorithms adapted to spin
for (const auto spin : SearchSpins)
{
int sp = static_cast<int>(spin);
EvaluateCPLinesResult(CPLines[sp], MaxSolves, Solves, CubeBase, spin);
}
for (int sp = 0; sp < 24; sp++)
{
Spn spin = static_cast<Spn>(sp);
for (auto& cp : CPLines[sp])
{
Stp T1, T2;
Cube::GetSpinsSteps(CubeBase.GetSpin(), spin, T1, T2);
Algorithm Insp;
if (T1 != Stp::NONE) { Insp.Append(T1); cp.TransformTurn(T1); }
if (T2 != Stp::NONE) { Insp.Append(T2); cp.TransformTurn(T2); }
Inspections[sp].push_back(Insp);
}
}
}
// Search the best pEO extension solve algorithm
void YruRU::SearchpEO(const uint MaxDepth)
{
const auto time_peo_start = std::chrono::system_clock::now();
MaxDepthpEO = (MaxDepth <= 4u ? 4u : MaxDepth);
const SearchUnit URoot(SequenceType::DOUBLE, Sst::YRURU_urUR);
const SearchUnit U_pEO(SequenceType::SINGLE, Sst::YRURU_urUR);
SearchLevel L_Root(SearchCheck::NO_CHECK);
L_Root.Add(URoot);
SearchLevel L_Check(SearchCheck::CHECK);
L_Check.Add(U_pEO);
for (const auto spin : SearchSpins)
{
int sp = static_cast<int>(spin);
pEO[sp].clear();
Pgr B1;
switch (spin)
{
case Spn::UF: B1 = Pgr::UF_B1; break;
case Spn::UB: B1 = Pgr::UB_B1; break;
case Spn::UR: B1 = Pgr::UR_B1; break;
case Spn::UL: B1 = Pgr::UL_B1; break;
case Spn::DF: B1 = Pgr::DF_B1; break;
case Spn::DB: B1 = Pgr::DB_B1; break;
case Spn::DR: B1 = Pgr::DR_B1; break;
case Spn::DL: B1 = Pgr::DL_B1; break;
case Spn::FU: B1 = Pgr::FU_B1; break;
case Spn::FD: B1 = Pgr::FD_B1; break;
case Spn::FR: B1 = Pgr::FR_B1; break;
case Spn::FL: B1 = Pgr::FL_B1; break;
case Spn::BU: B1 = Pgr::BU_B1; break;
case Spn::BD: B1 = Pgr::BD_B1; break;
case Spn::BR: B1 = Pgr::BR_B1; break;
case Spn::BL: B1 = Pgr::BL_B1; break;
case Spn::RU: B1 = Pgr::RU_B1; break;
case Spn::RD: B1 = Pgr::RD_B1; break;
case Spn::RF: B1 = Pgr::RF_B1; break;
case Spn::RB: B1 = Pgr::RB_B1; break;
case Spn::LU: B1 = Pgr::LU_B1; break;
case Spn::LD: B1 = Pgr::LD_B1; break;
case Spn::LF: B1 = Pgr::LF_B1; break;
case Spn::LB: B1 = Pgr::LB_B1; break;
default: return; // Should not happend
}
for (uint n = 0u; n < CPLines[sp].size(); n++)
{
Algorithm AlgStart = Scramble;
AlgStart.Append(Inspections[sp][n]);
if (!Lines[sp].empty()) AlgStart.Append(Lines[sp][n]);
AlgStart.Append(CPLines[sp][n]);
Cube CubeYruRU(AlgStart);
if (!IsCPBuilt(CubeYruRU))
{
pEO[sp].push_back(Algorithm(""));
continue;
}
DeepSearch DSpEO(AlgStart, Plc::SHORT); // Deep search for pEO extension
DSpEO.AddToMandatoryPieces(B1);
DSpEO.AddSearchLevel(L_Root);
for (uint l = 2u; l < MaxDepthpEO; l++) DSpEO.AddSearchLevel(L_Check); // Add needed search levels
DSpEO.UpdateRootData();
DSpEO.Run(Cores);
Cores = DSpEO.GetCoresUsed(); // Update to the real number of cores used
Algorithm Solve;
EvaluatepEOResult(Solve, DSpEO.Solves, CubeYruRU, spin);
pEO[sp].push_back(Solve);
}
}
const std::chrono::duration<double> peo_elapsed_seconds = std::chrono::system_clock::now() - time_peo_start;
TimepEO = peo_elapsed_seconds.count();
}
// Complete the EO-BF in two steps - EO first
void YruRU::SearchEO(const uint MaxDepth)
{
auto time_eo_start = std::chrono::system_clock::now();
MaxDepthEO = (MaxDepth <= 4u ? 4u : MaxDepth);
const SearchUnit URoot(SequenceType::DOUBLE, Sst::YRURU_rUR);
const SearchUnit U_U(SequenceType::SINGLE, Sst::YRURU_rUR);
SearchLevel L_Root(SearchCheck::NO_CHECK);
L_Root.Add(URoot);
SearchLevel L_EO_Check(SearchCheck::CHECK);
L_EO_Check.Add(U_U);
for (const auto spin : SearchSpins)
{
int sp = static_cast<int>(spin);
EO[sp].clear();
Pgr B1;
switch (spin)
{
case Spn::UF: B1 = Pgr::UF_B1; break;
case Spn::UB: B1 = Pgr::UB_B1; break;
case Spn::UR: B1 = Pgr::UR_B1; break;
case Spn::UL: B1 = Pgr::UL_B1; break;
case Spn::DF: B1 = Pgr::DF_B1; break;
case Spn::DB: B1 = Pgr::DB_B1; break;
case Spn::DR: B1 = Pgr::DR_B1; break;
case Spn::DL: B1 = Pgr::DL_B1; break;
case Spn::FU: B1 = Pgr::FU_B1; break;
case Spn::FD: B1 = Pgr::FD_B1; break;
case Spn::FR: B1 = Pgr::FR_B1; break;
case Spn::FL: B1 = Pgr::FL_B1; break;
case Spn::BU: B1 = Pgr::BU_B1; break;
case Spn::BD: B1 = Pgr::BD_B1; break;
case Spn::BR: B1 = Pgr::BR_B1; break;
case Spn::BL: B1 = Pgr::BL_B1; break;
case Spn::RU: B1 = Pgr::RU_B1; break;
case Spn::RD: B1 = Pgr::RD_B1; break;
case Spn::RF: B1 = Pgr::RF_B1; break;
case Spn::RB: B1 = Pgr::RB_B1; break;
case Spn::LU: B1 = Pgr::LU_B1; break;
case Spn::LD: B1 = Pgr::LD_B1; break;
case Spn::LF: B1 = Pgr::LF_B1; break;
case Spn::LB: B1 = Pgr::LB_B1; break;
default: return; // Should not happend
}
for (uint n = 0u; n < CPLines[sp].size(); n++)
{
Algorithm AlgStart = Scramble;
AlgStart.Append(Inspections[sp][n]);
if (!Lines[sp].empty()) AlgStart.Append(Lines[sp][n]);
AlgStart.Append(CPLines[sp][n]);
AlgStart.Append(pEO[sp][n]);
Cube CubeYruRU(AlgStart);
if (!Roux::IsFBBuilt(CubeYruRU))
{
EO[sp].push_back(Algorithm(""));
continue;
}
DeepSearch DSEO(AlgStart, Plc::SHORT); // Deep search for EO
DSEO.AddToMandatoryPieces(B1);
DSEO.AddToMandatoryOrientations(Pgr::ALL_EDGES);
DSEO.AddSearchLevel(L_Root);
for (uint l = 2u; l < MaxDepthEO; l++) DSEO.AddSearchLevel(L_EO_Check); // Add needed search levels
DSEO.UpdateRootData();
DSEO.Run(Cores);
Cores = DSEO.GetCoresUsed(); // Update to the real number of cores used
Algorithm Solve;
DSEO.EvaluateShortestResult(Solve, true);
CubeYruRU.ApplyAlgorithm(Solve);
if (CubeYruRU.CheckOrientation(Pgr::ALL_EDGES)) EO[sp].push_back(Solve);
else EO[sp].push_back(Algorithm(""));
}
}
const std::chrono::duration<double> eo_elapsed_seconds = std::chrono::system_clock::now() - time_eo_start;
TimeEO = eo_elapsed_seconds.count();
}
// Complete the EO-BF in a single step or two steps as second step
void YruRU::SearchEOBF(const uint MaxDepth)
{
auto time_eobf_start = std::chrono::system_clock::now();
MaxDepthEOBF = (MaxDepth <= 4u ? 4u : MaxDepth);
const SearchUnit URoot(SequenceType::DOUBLE, Sst::YRURU_rUR);
const SearchUnit U_U(SequenceType::SINGLE, Sst::YRURU_rUR);
SearchLevel L_Root(SearchCheck::NO_CHECK);
L_Root.Add(URoot);
SearchLevel L_EOBF_Check(SearchCheck::CHECK);
L_EOBF_Check.Add(U_U);
for (const auto spin : SearchSpins)
{
int sp = static_cast<int>(spin);
EOBF[sp].clear();
Pgr B1, BF;
switch (spin)
{
case Spn::UF: B1 = Pgr::UF_B1; BF = Pgr::EOLINE_UF; break;
case Spn::UB: B1 = Pgr::UB_B1; BF = Pgr::EOLINE_UB; break;
case Spn::UR: B1 = Pgr::UR_B1; BF = Pgr::EOLINE_UR; break;
case Spn::UL: B1 = Pgr::UL_B1; BF = Pgr::EOLINE_UL; break;
case Spn::DF: B1 = Pgr::DF_B1; BF = Pgr::EOLINE_DF; break;
case Spn::DB: B1 = Pgr::DB_B1; BF = Pgr::EOLINE_DB; break;
case Spn::DR: B1 = Pgr::DR_B1; BF = Pgr::EOLINE_DR; break;
case Spn::DL: B1 = Pgr::DL_B1; BF = Pgr::EOLINE_DL; break;
case Spn::FU: B1 = Pgr::FU_B1; BF = Pgr::EOLINE_FU; break;
case Spn::FD: B1 = Pgr::FD_B1; BF = Pgr::EOLINE_FD; break;
case Spn::FR: B1 = Pgr::FR_B1; BF = Pgr::EOLINE_FR; break;
case Spn::FL: B1 = Pgr::FL_B1; BF = Pgr::EOLINE_FL; break;
case Spn::BU: B1 = Pgr::BU_B1; BF = Pgr::EOLINE_BU; break;
case Spn::BD: B1 = Pgr::BD_B1; BF = Pgr::EOLINE_BD; break;
case Spn::BR: B1 = Pgr::BR_B1; BF = Pgr::EOLINE_BR; break;
case Spn::BL: B1 = Pgr::BL_B1; BF = Pgr::EOLINE_BL; break;
case Spn::RU: B1 = Pgr::RU_B1; BF = Pgr::EOLINE_RU; break;
case Spn::RD: B1 = Pgr::RD_B1; BF = Pgr::EOLINE_RD; break;
case Spn::RF: B1 = Pgr::RF_B1; BF = Pgr::EOLINE_RF; break;
case Spn::RB: B1 = Pgr::RB_B1; BF = Pgr::EOLINE_RB; break;
case Spn::LU: B1 = Pgr::LU_B1; BF = Pgr::EOLINE_LU; break;
case Spn::LD: B1 = Pgr::LD_B1; BF = Pgr::EOLINE_LD; break;
case Spn::LF: B1 = Pgr::LF_B1; BF = Pgr::EOLINE_LF; break;
case Spn::LB: B1 = Pgr::LB_B1; BF = Pgr::EOLINE_LB; break;
default: return; // Should not happend
}
for (uint n = 0u; n < CPLines[sp].size(); n++)
{
Algorithm AlgStart = Scramble;
AlgStart.Append(Inspections[sp][n]);
if (!Lines[sp].empty()) AlgStart.Append(Lines[sp][n]);
AlgStart.Append(CPLines[sp][n]);
AlgStart.Append(pEO[sp][n]);
if (!EO[sp].empty()) AlgStart.Append(EO[sp][n]);
Cube CubeYruRU(AlgStart);
if (!Roux::IsFBBuilt(CubeYruRU) || (!EO[sp].empty() && !CubeYruRU.CheckOrientation(Pgr::ALL_EDGES)))
{
EOBF[sp].push_back(Algorithm(""));
continue;
}
DeepSearch DSEOBF(AlgStart, Plc::SHORT); // Deep search for EO-BF
DSEOBF.AddToMandatoryPieces(B1);
DSEOBF.AddToMandatoryPieces(BF);
DSEOBF.AddToMandatoryOrientations(Pgr::ALL_EDGES);
DSEOBF.AddSearchLevel(L_Root);
for (uint l = 2u; l < MaxDepthEOBF; l++) DSEOBF.AddSearchLevel(L_EOBF_Check); // Add needed search levels
DSEOBF.UpdateRootData();
DSEOBF.Run(Cores);
Cores = DSEOBF.GetCoresUsed(); // Update to the real number of cores used
std::vector<Algorithm> Solves;
CFOP::EvaluateF2LResult(Solves, 1u, DSEOBF.Solves, CubeYruRU, Cube::GetDownSliceLayer(spin), Plc::BEST_SOLVES);
if (Solves.empty()) EOBF[sp].push_back(Algorithm(""));
else
{
CubeYruRU.ApplyAlgorithm(Solves[0]);
if (CubeYruRU.CheckOrientation(Pgr::ALL_EDGES)) EOBF[sp].push_back(Solves[0]);
else EOBF[sp].push_back(Algorithm(""));
}
}
}
const std::chrono::duration<double> eobf_elapsed_seconds = std::chrono::system_clock::now() - time_eobf_start;
TimeEOBF = eobf_elapsed_seconds.count();
}
// F2L search
void YruRU::SearchF2L(const uint MaxDepth)
{
const auto time_F2L_start = std::chrono::system_clock::now();
MaxDepthF2L = (MaxDepth <= 4u ? 4u : MaxDepth);
const SearchUnit U_Root(SequenceType::DOUBLE, Sst::YRURU_UR);
const SearchUnit U_UR(SequenceType::SINGLE, Sst::YRURU_UR);
SearchLevel L_Root(SearchCheck::NO_CHECK);
L_Root.Add(U_Root);
SearchLevel L_Check(SearchCheck::CHECK);
L_Check.Add(U_UR);
for (const auto spin : SearchSpins)
{
int sp = static_cast<int>(spin);
const Lyr DownLayer = Cube::GetDownSliceLayer(spin);
const Lyr MiddleLayer = Cube::AdjacentLayer(DownLayer);
Pgr LDOWN, LMID, CUP;
switch (DownLayer)
{
case Lyr::U: LDOWN = Pgr::LAYER_U; LMID = Pgr::LAYER_E; CUP = Pgr::CROSS_D; break;
case Lyr::D: LDOWN = Pgr::LAYER_D; LMID = Pgr::LAYER_E; CUP = Pgr::CROSS_U; break;
case Lyr::F: LDOWN = Pgr::LAYER_F; LMID = Pgr::LAYER_S; CUP = Pgr::CROSS_B; break;
case Lyr::B: LDOWN = Pgr::LAYER_B; LMID = Pgr::LAYER_S; CUP = Pgr::CROSS_F; break;
case Lyr::R: LDOWN = Pgr::LAYER_R; LMID = Pgr::LAYER_M; CUP = Pgr::CROSS_L; break;
case Lyr::L: LDOWN = Pgr::LAYER_L; LMID = Pgr::LAYER_M; CUP = Pgr::CROSS_R; break;
default: continue; // Should not happend
}
for (uint n = 0u; n < CPLines[sp].size(); n++)
{
Algorithm AlgStart = Scramble;
AlgStart.Append(Inspections[sp][n]);
if (!Lines[sp].empty()) AlgStart.Append(Lines[sp][n]);
AlgStart.Append(CPLines[sp][n]);
AlgStart.Append(pEO[sp][n]);
if (!EO[sp].empty()) AlgStart.Append(EO[sp][n]);
AlgStart.Append(EOBF[sp][n]);
const Cube CubeYruRU(AlgStart);
if (!Roux::IsFBBuilt(CubeYruRU) || !CubeYruRU.CheckOrientation(Pgr::ALL_EDGES))
{
F2L[sp].push_back(Algorithm(""));
continue;
}
if (CubeYruRU.IsSolved(DownLayer) && CubeYruRU.IsSolved(MiddleLayer) && CubeYruRU.CheckOrientation(Pgr::ALL_EDGES))
{
F2L[sp].push_back(Algorithm(""));
continue; // F2L already solved
}
DeepSearch DSF2L(AlgStart, Plc::SHORT); // Deep search for F2L
DSF2L.AddToMandatoryPieces(LDOWN);
DSF2L.AddToMandatoryPieces(LMID);
DSF2L.AddToMandatoryOrientations(CUP);
DSF2L.AddSearchLevel(L_Root);
for (uint l = 2; l < MaxDepthF2L; l++) DSF2L.AddSearchLevel(L_Check); // Add needed search levels
DSF2L.UpdateRootData();
DSF2L.Run(Cores);
F2L[sp].push_back(Algorithm(""));
DSF2L.EvaluateShortestResult(F2L[sp][n], true);
}
}
const std::chrono::duration<double> F2L_elapsed_seconds = std::chrono::system_clock::now() - time_F2L_start;
TimeF2L = F2L_elapsed_seconds.count();
}
// 2GLL search
void YruRU::Search2GLL()
{
const auto time_2GLL_start = std::chrono::system_clock::now();
for (const auto spin : SearchSpins)
{
int sp = static_cast<int>(spin);
Alg2GLL[sp].clear();
Cases2GLL[sp].clear();
for (uint n = 0u; n < CPLines[sp].size(); n++)
{
Alg2GLL[sp].push_back(Algorithm(""));
Cases2GLL[sp].push_back("");
Algorithm Alg = Inspections[sp][n];
if (!Lines[sp].empty()) Alg += Lines[sp][n];
Alg += CPLines[sp][n];
Alg += pEO[sp][n];
if (!EO[sp].empty()) Alg.Append(EO[sp][n]);
Alg += EOBF[sp][n];
Alg += F2L[sp][n];
Cube CubeYruRU = CubeBase;
CubeYruRU.ApplyAlgorithm(Alg);
const Lyr DownLayer = Cube::GetDownSliceLayer(spin);
if (!CubeYruRU.IsSolved(DownLayer) ||
!CubeYruRU.IsSolved(Cube::AdjacentLayer(DownLayer)) ||
!CubeYruRU.CheckOrientation(Pgr::ALL_EDGES)) continue;
Stp AUFStep;
Collection::SolveLL(Alg2GLL[sp][n], Cases2GLL[sp][n], AUFStep, AlgSets::_2GLL, CubeYruRU);
Alg2GLL[sp][n].Append(AUFStep);
}
}
const std::chrono::duration<double> _2GLL_elapsed_seconds = std::chrono::system_clock::now() - time_2GLL_start;
Time2GLL = _2GLL_elapsed_seconds.count();
}
// Set regrips
void YruRU::SetRegrips()
{
for (const auto spin : SearchSpins)
{
int sp = static_cast<int>(spin);
if (!CheckSolveConsistency(spin)) continue;
for (uint n = 0u; n < Inspections[sp].size(); n++)
{
if (Lines[sp].empty())
{
CPLines[sp][n] = CPLines[sp][n].GetRegrip();
if (Algorithm::IsTurn(CPLines[sp][n].First()))
{
Inspections[sp][n].AppendShrink(CPLines[sp][n].First());
if (Inspections[sp][n].GetSize() == 3u) Inspections[sp][n] = Inspections[sp][n].GetCancellations();
CPLines[sp][n].EraseFirst();
}
}
else
{
Lines[sp][n] = Lines[sp][n].GetRegrip();
// CPLines[sp][n] = CPLines[sp][n].GetRegrip();
if (Algorithm::IsTurn(Lines[sp][n].First()))
{
Inspections[sp][n].AppendShrink(Lines[sp][n].First());
if (Inspections[sp][n].GetSize() == 3u) Inspections[sp][n] = Inspections[sp][n].GetCancellations();
Lines[sp][n].EraseFirst();
}
if (Algorithm::IsTurn(Lines[sp][n].Last()))
{
CPLines[sp][n].Insert(0u, Lines[sp][n].Last());
while (CPLines[sp][n].Shrink());
Lines[sp][n].EraseLast();
}
}
}
}
}
// Check if the CP-Line is built (only line)
bool YruRU::IsLineBuilt(const Cube& C)
{
switch (C.GetSpin())
{
case Spn::UF: case Spn::RB: return C.IsSolved(Pgr::CPLINE_UF); // == CPLINE_RB
case Spn::UB: case Spn::LF: return C.IsSolved(Pgr::CPLINE_UB); // == CPLINE_LF
case Spn::UR: case Spn::BL: return C.IsSolved(Pgr::CPLINE_UR); // == CPLINE_BL
case Spn::UL: case Spn::FR: return C.IsSolved(Pgr::CPLINE_UL); // == CPLINE_FR
case Spn::DF: case Spn::LB: return C.IsSolved(Pgr::CPLINE_DF); // == CPLINE_LB
case Spn::DB: case Spn::RF: return C.IsSolved(Pgr::CPLINE_DB); // == CPLINE_RF
case Spn::DR: case Spn::FL: return C.IsSolved(Pgr::CPLINE_DR); // == CPLINE_FL
case Spn::DL: case Spn::BR: return C.IsSolved(Pgr::CPLINE_DL); // == CPLINE_BR
case Spn::FU: case Spn::LD: return C.IsSolved(Pgr::CPLINE_FU); // == CPLINE_LD
case Spn::FD: case Spn::RU: return C.IsSolved(Pgr::CPLINE_FD); // == CPLINE_RU
case Spn::BU: case Spn::RD: return C.IsSolved(Pgr::CPLINE_BU); // == CPLINE_RD
case Spn::BD: case Spn::LU: return C.IsSolved(Pgr::CPLINE_BD); // == CPLINE_LU
default: return false; // Should not happend
}
}
// Check if the CP-Line is built (only line)
bool YruRU::IsLineBuilt(const Cube& C, const Spn sp)
{
Cube CAux = C;
CAux.SetSpin(sp);
return IsLineBuilt(CAux);
}
// Corners permutation (begginers version)
bool YruRU::IsCPBuilt(const Cube& C)
{
if (!IsLineBuilt(C)) return false; // Line for CP (1x1x3 block) must be solved
// Get the corner positions
const Pcp Place1 = Cube::FromAbsPosition(App::UFL, C.GetSpin()), // Odd place / Couple 1
Place2 = Cube::FromAbsPosition(App::UBL, C.GetSpin()), // Even place / Couple 1
Place3 = Cube::FromAbsPosition(App::UBR, C.GetSpin()), // Odd place / Couple 2
Place4 = Cube::FromAbsPosition(App::UFR, C.GetSpin()), // Even place / Couple 2
Place5 = Cube::FromAbsPosition(App::DFR, C.GetSpin()), // Odd place / Couple 3
Place6 = Cube::FromAbsPosition(App::DBR, C.GetSpin()); // Even place / Couple 3
// Get the corners
const Pce Corner1 = static_cast<Pce>(Place1),
Corner2 = static_cast<Pce>(Place2),
Corner3 = static_cast<Pce>(Place3),
Corner4 = static_cast<Pce>(Place4),
Corner5 = static_cast<Pce>(Place5),
Corner6 = static_cast<Pce>(Place6);
// Get the positions for each corner
const Pcp PlaceForCorner1 = C.GetPiecePosition(Corner1),
PlaceForCorner2 = C.GetPiecePosition(Corner2),
PlaceForCorner3 = C.GetPiecePosition(Corner3),
PlaceForCorner4 = C.GetPiecePosition(Corner4),
PlaceForCorner5 = C.GetPiecePosition(Corner5),
PlaceForCorner6 = C.GetPiecePosition(Corner6);
// Check if the corner 5 is in an odd place
bool Corner5OddLocation = PlaceForCorner5 == Place1 || PlaceForCorner5 == Place3 || PlaceForCorner5 == Place5;
// Check if the corners 5 & 6 are in the same couple
bool Corners5_6SameCouple = (PlaceForCorner5 == Place1 && PlaceForCorner6 == Place2) ||
(PlaceForCorner5 == Place2 && PlaceForCorner6 == Place1) ||
(PlaceForCorner5 == Place3 && PlaceForCorner6 == Place4) ||
(PlaceForCorner5 == Place4 && PlaceForCorner6 == Place3) ||
(PlaceForCorner5 == Place5 && PlaceForCorner6 == Place6) ||
(PlaceForCorner5 == Place6 && PlaceForCorner6 == Place5);
// Check the parity for each corner (true = odd)
const bool ParityCorner1 = PlaceForCorner1 == Place1 || PlaceForCorner1 == Place3 || PlaceForCorner1 == Place5,
ParityCorner2 = PlaceForCorner2 == Place1 || PlaceForCorner2 == Place3 || PlaceForCorner2 == Place5,
ParityCorner3 = PlaceForCorner3 == Place1 || PlaceForCorner3 == Place3 || PlaceForCorner3 == Place5,
ParityCorner4 = PlaceForCorner4 == Place1 || PlaceForCorner4 == Place3 || PlaceForCorner4 == Place5,
ParityCorner5 = PlaceForCorner5 == Place1 || PlaceForCorner5 == Place3 || PlaceForCorner5 == Place5,
ParityCorner6 = PlaceForCorner6 == Place1 || PlaceForCorner6 == Place3 || PlaceForCorner6 == Place5;
// Read the sequence or corners
std::vector<Pce> Sequence;
if (Corner5OddLocation) // If Corner 5 is in an odd location, we will "read" the thread from UFL to DBR
{
Sequence.push_back(C.GetPiece(Place1));
Sequence.push_back(C.GetPiece(Place2));
Sequence.push_back(C.GetPiece(Place3));
Sequence.push_back(C.GetPiece(Place4));
Sequence.push_back(C.GetPiece(Place5));
Sequence.push_back(C.GetPiece(Place6));
}
else // If corner 5 is in an even location, we will read from DBR to UFL
{
Sequence.push_back(C.GetPiece(Place6));
Sequence.push_back(C.GetPiece(Place5));
Sequence.push_back(C.GetPiece(Place4));
Sequence.push_back(C.GetPiece(Place3));
Sequence.push_back(C.GetPiece(Place2));
Sequence.push_back(C.GetPiece(Place1));
}
if (!Corners5_6SameCouple) // If corner 6 is in the couple of location of 5, do nothing.
{
int PlaceCorner6Index = -1, PlaceForCorner6FriendIndex = -1, PlaceCorner5Index = -1, PlaceForCorner5FriendIndex = -1;
// Get the index for corners 5 & 6
for (int Index = 0; Index < static_cast<int>(Sequence.size()); Index++)
{
if (Sequence[Index] == Corner5) PlaceCorner5Index = Index;
else if (Sequence[Index] == Corner6) PlaceCorner6Index = Index;
}
// Get the index for corner 5 friend
if (PlaceCorner5Index % 2 == 0) PlaceForCorner5FriendIndex = PlaceCorner5Index + 1;
else PlaceForCorner5FriendIndex = PlaceCorner5Index - 1;
// Get the index for corner 6 friend
if (PlaceCorner6Index % 2 == 0) PlaceForCorner6FriendIndex = PlaceCorner6Index + 1;
else PlaceForCorner6FriendIndex = PlaceCorner6Index - 1;
// The first swap is always done between "6" and "the friend of 5", so that now 5 and 6 are friends
Pce Buffer = Sequence[PlaceCorner6Index];
Sequence[PlaceCorner6Index] = Sequence[PlaceForCorner5FriendIndex];
Sequence[PlaceForCorner5FriendIndex] = Buffer;
// Second swap
if (ParityCorner5 == ParityCorner6)
{
// Search the third corner with the same parity than corners 5 & 6
int ThirdCornerSameParityIndex = -1;
if (ParityCorner1 == ParityCorner5)
{
for (int Index = 0; Index < static_cast<int>(Sequence.size()); Index++)
{
if (Sequence[Index] == Corner1)
{
ThirdCornerSameParityIndex = Index;
break;
}
}
}
else if (ParityCorner2 == ParityCorner5)
{
for (int Index = 0; Index < static_cast<int>(Sequence.size()); Index++)
{
if (Sequence[Index] == Corner2)
{
ThirdCornerSameParityIndex = Index;
break;
}
}
}
else if (ParityCorner3 == ParityCorner5)
{
for (int Index = 0; Index < static_cast<int>(Sequence.size()); Index++)
{
if (Sequence[Index] == Corner3)
{
ThirdCornerSameParityIndex = Index;
break;
}
}
}
else if (ParityCorner4 == ParityCorner5)
{
for (int Index = 0; Index < static_cast<int>(Sequence.size()); Index++)
{
if (Sequence[Index] == Corner4)
{
ThirdCornerSameParityIndex = Index;
break;
}
}
}
else return false; // Should not happend
// If 5 and 6 initially have the same parity, swap the third corner with the same parity with the friend of 6
Pce Buffer = Sequence[PlaceForCorner6FriendIndex];
Sequence[PlaceForCorner6FriendIndex] = Sequence[ThirdCornerSameParityIndex];
Sequence[ThirdCornerSameParityIndex] = Buffer;
}
else // ParityCorner5 != ParityCorner6
{
// Get the indexes for the third couple or corners
int ThirdCoupleCorner1Index = -1, ThirdCoupleCorner2Index = -1;
for (int Index = 0; Index < static_cast<int>(Sequence.size()); Index++)
{
if (Index != PlaceCorner5Index && Index != PlaceForCorner5FriendIndex &&
Index != PlaceCorner6Index && Index != PlaceForCorner6FriendIndex)
{