private void SearchPhase2(int cp, int equatorPermutation, int udEdgeOrder, int depth, int remainingMoves, int phase1Length) { if (IsTerminated.Value) { return; } if (remainingMoves == 0 && equatorPermutation == 0) //check if solved { Alg solution = Alg.FromEnumerable(_currentPhase1Solution.Take(phase1Length).Concat(_currentPhase2Solution.Take(depth)).Cast <Move>()); for (int i = 0; i < _rotation; i++) { solution = solution.Rotate(Rotation.y3).Rotate(Rotation.x3); } if (_inversed) { solution = solution.Inverse(); } lock (_lockObject) { if (solution.Length < _shortestSolutionLength.Value) { _shortestSolutionIndex.Value = _solutions.Count; _shortestSolutionLength.Value = solution.Length; } _solutions.Add(solution); } if (solution.Length <= _returnLength) { IsTerminated.Value = true; } return; } //increase depth foreach (int move in TwoPhaseConstants.Phase2Moves) { //prevent two consecutive moves on the same face or two //consecutive moves on the same axis in the wrong order if (depth == 0) { if (phase1Length > 0) { int relation = move / 3 - _currentPhase1Solution[phase1Length - 1] / 3; if (relation == SameFace || relation == SameAxisInWrongOrder) { continue; } } } else { int relation = move / 3 - _currentPhase2Solution[depth - 1] / 3; if (relation == SameFace || relation == SameAxisInWrongOrder) { continue; } } int newCp = TableController.CornerPermutationMoveTable[cp, move]; int newEquatorPermutation = TableController.EquatorPermutationMoveTable[equatorPermutation, move]; int newUdEdgePermutation = TableController.UdEdgeOrderMoveTable[udEdgeOrder, MoveTables.Phase1IndexToPhase2Index[move]]; //prune int cornerUdPruningIndex = PruningTables.GetPhase2CornerUdPruningIndex(newUdEdgePermutation, newCp); int cornerUdPruningValue = TableController.Phase2CornerUdPruningTable[cornerUdPruningIndex]; int cornerEquatorPruningIndex = Coordinates.NumEquatorOrders * newCp + newEquatorPermutation; int cornerEquatorPruningValue = TableController.Phase2CornerEquatorPruningTable[cornerEquatorPruningIndex]; if (Math.Max(cornerUdPruningValue, cornerEquatorPruningValue) > remainingMoves - 1) { continue; } _currentPhase2Solution[depth] = move; SearchPhase2(newCp, newEquatorPermutation, newUdEdgePermutation, depth + 1, remainingMoves - 1, phase1Length); } }
private void SearchPhase1(int eo, int co, int equator, int depth, int remainingMoves, int previousPruningValue) { if (IsTerminated.Value) { return; } if (remainingMoves == 0) //check if solved { lock (_lockObject) //manage timeout if (_timePassed.Elapsed > _timeout && (_requiredLength < 0 || (_solutions.Count > 0 && _shortestSolutionLength.Value <= _requiredLength))) { IsTerminated.Value = true; } int cp = _cp; int uEdges = _uEdges; int dEdges = _dEdges; int equatorPermutation = equator; //TEST improvement for (int moveIndex = 0; moveIndex < depth; moveIndex++) { int move = _currentPhase1Solution[moveIndex]; cp = TableController.CornerPermutationMoveTable[cp, move]; } int cornerEquatorPruningIndex = Coordinates.NumEquatorOrders * cp + equator; int cornerEquatorPruningValue = TableController.Phase2CornerEquatorPruningTable[cornerEquatorPruningIndex]; if (cornerEquatorPruningValue > MaxPhase2Length) { return; } for (int moveIndex = 0; moveIndex < depth; moveIndex++) { int move = _currentPhase1Solution[moveIndex]; uEdges = TableController.UEdgePermutationMoveTable[uEdges, move]; dEdges = TableController.DEdgePermutationMoveTable[dEdges, move]; } int udEdgeOrder = Coordinates.CombineUEdgePermutationAndDEdgeOrder(uEdges, dEdges % Coordinates.NumDEdgeOrders); //prune int cornerUdPruningIndex = PruningTables.GetPhase2CornerUdPruningIndex(udEdgeOrder, cp); int minMoves = TableController.Phase2CornerUdPruningTable[cornerUdPruningIndex]; int maxMoves = Math.Min(_shortestSolutionLength.Value - depth - 1, MaxPhase2Length); for (int length = minMoves; length <= maxMoves; length++) { SearchPhase2(cp, equatorPermutation, udEdgeOrder, depth: 0, remainingMoves: length, phase1Length: depth); } return; } //increase depth for (int move = 0; move < NumMoves; move++) { //If the cube is already in the subgroup H and there are less //than 5 moves left it is only possible to stay in the subgroup //if exclusivly phase 2 moves are used, which means that this //solution can also be generated in phase 2. if (previousPruningValue == 0 && remainingMoves < 5 && !TwoPhaseConstants.Phase2Moves.Contains((Move)move)) { continue; } //prevent two consecutive moves on the same face or two //consecutive moves on the same axis in the wrong order if (depth > 0) { int relation = move / 3 - _currentPhase1Solution[depth - 1] / 3; if (relation == SameFace || relation == SameAxisInWrongOrder) { continue; } } int newEo = TableController.EdgeOrientationMoveTable[eo, move]; int newCo = TableController.CornerOrientationMoveTable[co, move]; int newEquator = TableController.EquatorPermutationMoveTable[equator, move]; //prune int pruningCoord = PruningTables.GetPhase1PruningIndex(newCo, newEo, newEquator / Coordinates.NumEquatorOrders); int pruningValue = TableController.Phase1PruningTable[pruningCoord]; if (pruningValue > remainingMoves - 1) { continue; } _currentPhase1Solution[depth] = move; SearchPhase1(newEo, newCo, newEquator, depth + 1, remainingMoves - 1, pruningValue); } }