Proc. of Int. Conf. on Advances in Computer Science, AETACS Modeled Solution of a Rubik s Cube and Implementation of Techniques to Simulate Human Solutions Sumedh Ghaisas and Harman Singh III rd Year Undergraduates, B.E. (Hons.) Computer Science BITS-Pilani K.K. Birla Goa Campus, Zuarinagar, India sumedhghaisas@gmail.com harman28@gmail.com Abstract The ZZ Method of solving the Rubik s Cube is mapped and modeled to mirror the simple decision-making process of a human mind. Using sequence detection as our learning model, we try to optimize the solution achieved through very basic cases. This is an alternate solution to the heuristic search method which uses a huge pattern database of cube permutations. Index Terms artificial intelligence, Rubik s cube, sequence detection I. INTRODUCTION The Rubik's Cube* is a mechanical puzzle invented by Erno Rubik. Although invented decades ago, the Rubik s Cube continues to break records of popularity. Since 2003, the World Cube Association has organized many events under the categories 3v3, 4v4, 5v5, 7v7 and other variations. The Rubik's Cube has been a major attraction to mathematicians and computer scientists who pursue interests in discrete mathematics. Games and Puzzles is an important domain of research under Artificial Intelligence. The puzzle of Rubik s Cube is being studied formally alongside the 15-puzzle, Sudoku under the category of heuristic search. Morwen B. Thistlethwaite [4] is a mathematician who devised a clever algorithm for solving the Rubik s Cube in remarkably few moves. It is a rather complicated method which cannot be memorised, and is thus only practical for computers and not for humans, although there is a variant which can be performed by humans known as the Human Thistlethwaite algorithm which is developed by Ryan Heise. The algorithm is important from a theoretical standpoint however, as it has long been the method with the fewest number of moves to a complete solution. Using nested groups this algorithm can solve any permutation of Rubik s Cube in no more than 52 moves. To keep the size of the lookup tables down [3], Thistlethwaite used simplifying preliminary moves which is the reason why the number of moves he needed is sometimes more than the best possible. Later research decreased this number to as small as 23. In 2010, using latest computational technologies and the huge storage space of Google s San Francisco headquarters, a group of scientists exhaustively proved that any permutation can be solved with no more than 20 moves [2]. The number of valid permutations of Rubik s Cube Elsevier, 2013
is approximately forty-three quintillion. It took 35 CPU years to prove the above result. But the puzzle of Rubik s Cube is not completely solved yet. We did prove that the so-called God Number for this puzzle is 20 but the God Algorithm is still a mystery. A deterministic algorithm to solve any permutation optimally is yet to be found. With current computation advances heuristics-based state space search may be capable of finding the solution. It is noted however that the grand majority of systems designed to solve the puzzle in the minimum number of ways function on the principle of balancing efficiency and optimisation with reducing time for look-up operations. Large look-up tables are necessary to produce the most efficient move sequences, as the computer must be prepared to handle an extremely large number of situations. In the method proposed in this paper however, we hope to find a way around that. The Rubik s Cube can be solved with deterministic algorithms which permute it through well-defined states to the end state or solved state. Many algorithms are used to solve the Rubik s cube, some of which use subgroups of the group of cube permutations as defined states. A lookup table must be generated for all the cases in a certain stage against the algorithm required to bring it to the next stage. Here, while generating the lookup table, one has to manually enter the algorithms for all the cases. This is highly inefficient however, as the number of cases in a particular stage can be quite large. This paper gives an alternative approach to lookup tables, by trying to generate the solution through simple rules and sequence detection, i.e. a small lookup table will first be used to create a simple, inefficient sequence of moves, and then a sequence reduction algorithm (here called F-elimination) will be used to eliminate moves from and shorten the sequence into a much shorter version, which is observed to often be the same efficient sequence generated by a large lookup table. This solution is based on the process of learning which a human mind follows rather than mapping. If we teach our brain algorithms to solve simple cases, we can learn to solve more complex cases through experience. Similarly, here we provide the machine with base cases and define rules of sequence detection as the learning process. In this paper, we shall be modeling the popular ZZ method of solving the cube, a modified version of the Human Thistlethwaite algorithm. II. TERMINOLOGY A. Moves and Cubies For the remainder of this paper we will use certain terms to refer to parts and pieces, as well as standard moves of the cube. These notations are not fixed with respect to colours, but they give us a way of depicting the orientation of the cube and allow us to choose our own colours according to a frame of reference. Fig 1. Face notations and respective clockwise movements There are 8 corner and 12 edge independently-rotating pieces on a cube. In total 12 moves are defined for a cube, each corresponding to one of the 6 faces. A clockwise move of a face is represented by the letter used for the face representation, whereas an anticlockwise move is accompanied by ' symbol (transpose operator). The clockwise and anticlockwise direction for each face is given by Figure 1. For example, F denotes a rotation of the Front face clockwise by 90 degrees, while F' denotes a rotation of the Front face anti-clockwise. Sometimes extended notations like 2F are used to represent a 180 degree rotation of the front face. To refer to an individual unit or piece of the cube (also referred to as a 'cubie') we will use single letter to refer to centre pieces, two letters to refer to an edge piece, and three letters to refer a corner piece. The convention is an intuitive one. UFR stands for Up Front Right, implying the piece common to the Up, Front, and Right faces of the cube. And similarly UFL (Up Front Left), DBL(Down Back Left), etc. are defined for each of the corners. For edge 449
references similar notations involving two letter groups like UB(Up Back), DL(Down Left), etc. are used. To differentiate between cubies and the space they fill we define cubicles. These are labeled the same way as cubies but define the physical space in which the cubie is located. If the Rubik's Cube is solved each cubie is in the cubicle of the same name, eg. UFR cubie is in UFR cubicle. All cubies (except the centre pieces) can be moved by valid cube rotations but all cubicles always stay in their position. III. THE ZZ METHOD [1] Thistlewaite used nested groups to solve the Rubik s cube stage by stage. The entire state space can be considered a group with cube moves <R, L, U, D, B, F> as generator and the concatenation of these moves as the operator. For example the sequence RURUUU will define one scrambled state of Rubik s cube and the result of any valid cube operations on any scrambled state will generate a valid scrambled state. Based on this theory Thistlewaite created nested groups to solve the Rubik s optimally. In a certain group, there is a restriction on the applicable cube rotations. This grouping allows us to restrict the cube rotations until the cube is completely solved. The ZZ method by Zbigniew Zborowski [1] is based on this nested group method of solving the Cube stage by stage. The different stages of this method are: Edge Orientation: All edge pieces are reoriented so as to simply their positioning in the later stages. EO Cross: The edge pieces on the bottom face (D) are correctly placed, with some restriction on moves to preserve edge orientation. F2L: Corner pieces of the D face and edge pieces of the second layer are correctly placed, thus completing the First 2 Layers (F2L). Last Layer: Last layer is corrected. We will use sequence detection and reduction algorithms on particular stages of the ZZ method and try to get optimum solution to each of the stages. In this paper, we will analyse the first two stages. IV. REPRESENTATION RULES A. Similarity of Colours Two colours are similar if they either represent the same face or opposite faces. For example, if we consider a Rubik's Cube which has colour White on its Front face and colour Yellow on its Back face then we say that White and Yellow are similar colours corresponding to that particular Rubik's Cube. Colours White-White and Yellow-Yellow will also be similar. The restrictions on any sound Rubik s Cube imply that: (1) any edge has to be associated with two dissimilar colours, and (2) any corner has to be associated with three dissimilar colours. B. Edge Representation Good Edge/Bad Edge The Good Edge/Bad Edge concept is used by many speedcubers to boost their solving time as it gives a quick idea of the orientation of any edge. Using this concept we can effectively represent any edge by just one number without compromising any time complexity in cube rotations. We define a Good Edge as any edge which can be solved (brought to its original position) using only the rotations/moves R, U, L, D. Any edge which cannot be solved with this regular expression is a Bad Edge. Effectively, F and B rotations of the cube convert edges on the same face (face of rotation) from Good to Bad or Bad to Good. This method belongs to strategies used to find optimized solutions for Rubik s cube. By restricting movements further we can classify the state of a certain cube and find optimized solution for it. C. Corner Representation To represent a corner piece efficiently we need to extend notations using our behavioral knowledge. Grouping each corner piece with the three different colours on it slows the entire model representation process. In place of three colours we can group each corner with three integers to enable us to generalize the idea of rotation for any corner. For this we define primary, secondary and tertiary colours of our cube, each referring to a pair of similar colours. i. Primary Colours - {U face colour on solved cube, D face colour on solved cube} ii. Secondary Colours - {F face colour on solved cube, B face colour on solved cube} 450
iii. Tertiary Colours - {R face colour on solved cube, L face colour on solved cube} Now the each corner can be defined with respect to primary, secondary and tertiary colours. If we represent primary by 1, secondary by 2, and tertiary 3, we have nine integer sequences which can uniquely identify the orientation of any corner at any moment. For that we need to define orientation with respect to our new notations. We define it as shown below. Consider the UFR corner in Figure 4. For this configuration Primary colours are blue and green, Secondary are white and yellow, and Tertiary are red and orange. We will write the orientation of this cubie, aligning it with its name U, F, R. Thus we will represent this orientation as Green-White-Red, corresponding to Up- Front-Right. In terms of groups it will be P-S-T (primary-secondary-tertiary). By this logic UFL corner will also decode to PST. If we observe every corner in its default configuration will be PST. In Figure 5 UFR orientation is STP. This is because the Secondary color of the piece (White) is on the corner's Up face and Tertiary color (Red) is on the corner's Front face. This leaves the Primary color (Green) on the Right face. Thus, White-Red-Green decodes to STP. By this representation every corner piece is represented only by {P, S, T} and the meaning changes as the corner moves, because the default configuration of every corner is its naming convention. The PST representation only represents piece's orientation and not the piece itself, so to represent each corner piece uniquely we need to assign each piece a unique number. Now we will look at the effect of valid cube rotations on corner orientation. We can easily see that the rotation R will swap Primary and Secondary colours of corners on face R and so does rotation R'. Rotation L and L' will also swap Primary and secondary colours of respective corners. Rotations U, U', D and D' will swap Secondary and Tertiary colours while F, F', B and B' will swap Primary and Tertiary colours. V. SOLVING THE CUBE The ZZ method of Rubik s Cube solving includes 3 or 4 stages depending on the variant. (A) Edge orientation, (B) EO-Cross, (C) F2L, and (D) solving the last layer are basic stages of this algorithm. A. Edge Orientation Edge orientation stage converts all the Bad Edges to Good Edges. The purpose of this stage is to orient the edges so as to restrict the cube rotations to <R, U, L, D, 2F, 2B>, thus reducing the number of cases that need to be taken care in later stages. As we know, Bad Edges can only appear in pairs so we can divide our cases further with regards to number of bad edges to be solved. Let us take our base case as only 2 bad edges. We know F, F, B or B rotation flips the orientation of all the 4 edges on the corresponding face. Take the case where both the bad edges are on the Front face. In this case F rotation will cause both bad edges to turn good while the remaining two good edges will turn bad. So we will not be able to achieve our goal. To solve this case we need to perform a setup move to get one bad edge out of the Front face (a setup move is defined as one that brings the cube to one of the base cases, but does not contain F or B rotation to ensure constant orientation of the edges in question). When both bad edges are not on the Front face one of them can be taken to front face through setup moves. This will reduce our base cases further. We define our base cases as cases in which one of the bad edges is in FL or in UF. Fig 2. Example A for Edge Orientation with two bad edges For example, let us take one base case, Example A as shown in Figure 2. Bad edges are FL and UR. We will perform FUF to turn both bad edges good. The F move turns one bad edge good while U replaces it with the remaining one and F finishes the task. The end F move can be replaced by F as both fulfil the same function. As the cube is symmetric the algorithm for FR and UR will be F UF. In total there will be 18 such base cases. The solutions for all these cases are intuitive and will not exceed 4 steps. o Note on n-replacement policies In the example explained above, only two edges were out of place, so a set to moves aimed at turning both 451
these edges into good edges would necessarily only replace one bad edge at a time. Put simply, the method involves correcting the orientation of one bad edge B1 with an F or F move (and displacing three other good edges on the face in the process), then replacing it with the other bad edge B2, and reversing the initial F or F to correct B2 (and the other three edges as well). This one-replacement method also works for cases involving four or more (even numbered) bad edges, but results in long drawn sets of moves which are largely unnecessary. On the other hand, a two-replacement policy is applied in a case where two bad edges (out of at least four) exist in the front face, in which case both can be corrected, replaced, and then their replacements can be corrected (the initial condition of two bad edges in the front face can be achieved using another set of setup moves ), thus achieving a shorter set of moves than that attained by the one-replacement policy. Such higher n-replacement policies are easy to apply for a human user, but cannot be arrived at by a computer without extremely costly lookup operations. For this reason, we will teach the machine only 1-replacement policies to arrive at the initial long algorithm, then apply various sequence reduction techniques to reduce the algorithm to its shortest possible form. This final form of the algorithm is often equivalent to the algorithm that a human might have arrived at using the higher n-replacement policy described earlier. Fig 3. Example B for Edge Orientation with four bad edges Let us now analyse the solution of a case involving four bad edges Example B in Figure 3. First we shall solve this using only base cases i.e. one-replacement policy. For the first replacement we have 4 options: UF- UR, UF-UL, DF-UR or DF-BL. We need to check for every case (every replacement as well as every set of setup moves) and compare the solutions after reduction to find the optimized solution. Let us first solve UF- UR. These can be corrected using FR F. Now there are two bad edges remaining namely DF and UL. These in turn can be corrected using FLLF. Concatenation will give us the complete solution: (FR F )(FLLF ). We know that F is inverse of F so FF will be an empty string. Thus the solution reduces to (FR LLF ). If we look closely, we can see that the solution is the same as that achieved using 2-replacement policy F corrects two edges, R and LL replace both of them, and F corrects the replacements. We need to check for all the combinations of 1 replacement algorithms possible to find the true optimal solution. o Further Sequence Reduction In case analyzed above, the solution can be even further optimized. In fact, a set of moves as short as R LLF could achieve correction for all four bad edges. Thus the first F has been eliminated. This is a special case in which all bad edges are simultaneously placed in the front face, which forms one of two exceptional cases, which we now attempt to explain. If it is ever possible to place (i) four bad edges on the front face, or (ii) half of the existing bad edges on the front face (for example, 3 in the case having 6 bad edges) then these cases provide the optimal solution to edge correction. Let us call these F B4 and F B3 states respectively. Of these two favourable states, we state that F B4 has higher priority than F B3. For example, in a case where there exist a total of six bad edges, placing half the edges on the front face (using setup moves), correcting them, then replacing them and correcting the replacements. In order for the machine to realise such reduction, we must define further rules. F-Elimination Let us take the previous example to demonstrate this technique, through which we are able to remove pairs of F-moves at a time. We shall use the algorithm as described in the pseudocode below in Code Block 1. For purposes of the algorithm, we shall use: o A copy of the present state cube, called cube_temp o A sequence, initially empty, L1 o A sequence, initially empty, L2 o A set G1 which contains {F,F } o A set G2 which contains all possible moves except for elements of G1. o Situation_full refers to an arrangement where 4 bad edges are present on the Front face. 452
o Situation_half refers to an arrangement where half of the total number of bad edges is present on the Front face. o Situation_null refers to an arrangement where neither of the above two situations are applicable. o An Isolated move is on that does not affect any bad edge on the front face of cube_temp. In this particular case, the sequence is we will be using and attempting to reduce is FR F FL2F. Code Block 1. Pseudocode for F-elimination So, in the first instance of the loop, temp=f, which belongs to G1. L1 is initially empty, to we add temp to it. L1 is now {F}. After reading the next move from the sequence, temp will become R', and is added to L1. R is isolated, so we add it to L2 and perform it on cube_temp. Now L1= {FR }, L2={R }. A check reveals that cube_temp now has three bad edges on the front face, which corresponds to Situation_null. So set temp=f, the next move and add it to L1. So L1={R F }, L2={R }. The count of bad edges is still not zero so we repeat the procedure. 453
Temp is now F, the next move in the sequence, which happens to belong to G1. Now temp is an inverse of L1_x, the last move in L1, as L1_x is F. So L1_x is removed and L1={R }. Temp is next set to L2, and is added to L1. Temp is also an isolated move, so it is added to L2, and performed on cube_temp. L1=L2={R LL}. A check reveals we are now in Situation_full. Set temp to the next move from the sequence, but add F to L2, and perform it on cube_temp. Equate L1=L2. A check reveals there are now no bad edges remaining and the algorithm is complete. The final value of L1, which is R LLF, gives us the optimized solution for this stage. Note that the last step of Situation_half, had it occurred, would have required us to once more generate a new sequence of moves, this time from the cube_temp. This new sequence would be achieved in a way similar to that described in the last section, i.e. using one-replacement policy. Analysis of F-Elimination The initial sequence used in our example was FR F FLLF. The subsequence F F can be removed to give us FR LLF. Careful examination of this new sequence shows us that it is in fact the implementation of a tworeplacement policy to solve edge orientation. After applying the F-elimination technique, we are left with the shortened sequence R LLF. Another examination shows us that, if applied on the original cube, this sequence seeks to first place all bad edge on the front face and then correct each of them using a single F move. This is thus an implementation of the four-replacement policy. This is remarkable, as the initial sequence of FR F FLLF is one generated by the machine when it was only programmed to do one-replacement techniques, but after application of the reduction algorithm, it simulates being able to perform a much high-replacement policy. The learning process here can be compared to that which any human might go through when first trying to solve edge orientation. Instead of using only one-replacement policy, a human might correctly identify the highest number of edges one can correct at a time, and use that. This is exactly what F-elimination achieves. B. EO-Cross The EO-Cross stage sets the initial cross-shape of edge pieces on the lowermost face of the cube, henceforth referred to as Layer #1. The purpose of this stage is to actually begin building the cube in its solved state, with the added restriction of never using the <F, F, B, B > operations, as any of these with affect the edge orientations that were set in the previous stage. Simply put, this step involves bringing all four edge pieces belonging to the D face to their correct places. This is achieved by placing each of them in the edge location common to the two faces whose colours they carry. A human mind would attempt to optimize these moves by simultaneously placing as many of the four relevant edges in their correct location as possible. However, similar to the technique we used in the Edge Orientation stage, here too we will create an algorithm to only correctly place one edge piece at a time, and run this algorithm for all four pieces to achieve in a long series of solving moves. Sequence reductions will then be applied to this series to optimize it and bring it as close as possible to the ideal one that the human solver might arrive at. Before we define the algorithm, some salient categorization of edge pieces should be noted: o A relevant edge piece may reside in the lowermost face D (Layer #1), the uppermost face U (Layer #3), or in the middle row of the cube (Layer #2). o Even though edge pieces are by nature common to two faces at any point of time, in this algorithm, whenever possible we shall say they belong to face L or face R, i.e. they are a part of, or currently reside in face L or R. o The only edge pieces that cannot be referred to in the manner described above are those in UF and DF, and UB and DB. These are said to belong to F and B respectively. With these statements in mind, we can now begin to elaborate the EO-Cross algorithm. The algorithm will give us, for each edge piece to be corrected, a series of moves which shall have the following form: Sequence = ABCA B Where A, B, and C are short adjustment moves and A and B are the inverses or reversals of A and B respectively. A, B and C can be obtained as follows. Move A For edge pieces residing in Layers #2 and #3, do nothing. Therefore, A= {}. For edge pieces residing in Layer #1, For edge pieces on X face, where X is F or B, A is given by A= {2X}, i.e. {2F} for DF and {2B} for DB. 454
For edge pieces on X face, where X is L or R, A is given by A= {X}, i.e. {L} for DL and {R} for DR. Move B Move B is given by the number of D moves required to shift an edge piece from the face it belongs to now to the face it should belong to in the solved state. For example, if an edge belongs to L now, but must belong to B eventually, then this is one D move away. Similarly, R is 2 D moves away (2D) and F is 3 D moves away (D ). All possible combinations are given in Table I. Here the row is given by the initial face the edge belongs to, while the column is given by the final face the edge must be brought to. TABLE I. B STAGE MOVES FOR EO CROSS From\To F L B R F - D 2D D L D - D 2D B 2D D - D R D 2D D - Move C For edge pieces residing in Layer #1, C= {}. For edge pieces residing in Layer #2, For FL, C= {L} For FR, C= {R } For BL, C= {L } For BR, C= {R} For edge pieces residing in Layer #3, C= {2X}, where X is the face that the edge currently belongs to. For example, for an edge piece at UL, C= {2L}, but for one at UB, C= {2B}. Having thus obtained all three moves A, B, and C, it is now possible to string together the complete sequence for solving a single edge piece with the equation, Sequence = ABCA B A series of sequences, one for each of the four edge pieces belonging to the D face, would then together solve the cross-shape on the D face and thus complete EO-Cross. However, the order of selection of the four edge pieces also matters. It can be seen that giving priority to edge pieces that can be solved with the fewest number of moves is help shorten the overall EO-Cross stage, as the early short sequences will displace the edge pieces having longer sequences and put in more favourable positions. For this purpose, it is noted that edge pieces initially residing in Layer #2 are most favourable, followed by those in Layer #3. This is because Layer #2 pieces have no move A, and move B of length 1. Layer #3 pieces also have no move A, but have a move B of length 2. Layer #1 pieces will generally have the longest correction sequences. As an example, let us take an edge piece existing in the UL position (Example A in Figure 4) and needing to be placed in the DL position, as shown. Fig 4. Example A for EO Cross with relevant edge at UL It is readily apparent to us that a simple 2L turn will place the edge piece in the correct location. Let us a see what sequence the algorithm produces. Firstly, we find A. A is empty for pieces in the 3 rd layer. Next, we find B. The piece belongs to L, and must travel to L itself, so Table I states that B is also empty. Finally, C is given by 2X, where X is the face, here L. Thus the produced sequence is ABCA B, where only C has a value=2l. Thus the sequence is 2L, which the same result we predicted. 455
Let us study a more complicated example (Example B in Figure 5), namely an edge piece located in DF, needing to be placed in the back face, i.e. DB. Fig 5. Example B for EO Cross with relevant edge at DF A naïve solution would be to perform 2D, but this would alter the correctly placed edge pieces on the D face. Instead, let us see if the algorithm can preserve the placing while relocating the piece in question. First, since the piece belongs to layer #1, A is 2X, or 2F. Next, B is given by a move from F to B, for which Table I supplies a value of 2D. Finally, C is empty for layer #1. The given sequence ABCA B is thus {2F2D2F 2D }, which correctly places the edge piece. Closer observation will show how the correctness of the edge pieces and the D cross is preserved. The two A groups contain 2F, which does not alter edge orientation as set in the first stage. The two B groups use 2D. This affects the edges on the bottom face, but the effect is reversed by the B move, which reinstalls the edges in their former positions, with the exception of the piece in question, which has be correctly placed at DB. VI. CONCLUSION It is thus observed that a machine can be made to simulate more complex and optimized solutions to extended problems, even though in the learning step it is only taught to put together the longest, most simple set of moves that solves the problem. By implementing certain reduction techniques, this extended sequence can be greatly shortened and optimized, such that the solution mimics that of a human who has learnt more than just the simplest techniques. In the four stages of the ZZ method of solving a Rubik s Cube, we have here dealt with only the first two stages. The greatest benefit can be seen in the F-elimination technique applied to the first Edge Orientation stage. A reduction algorithm has also been developed for the EO-Cross stage, to moderately reduce number of steps. Further work would include modeling the most basic solutions for the remaining two stages, F2L and Last Layer, and developing similarly efficient reduction algorithms to optimize the solutions. At the conclusion of this work on all layers, it will be possible to completely program a machine to mimic the same process that a human undertakes as he attempts to combine multiple sequences into fewer, shorter amalgamations of moves, by using the reduction algorithms to simulate thinking. This method of solving a Rubik s Cube using sequence detection and reduction is a suitable alternate to the heuristic search method, especially which regards to solving cube of higher degrees, as the huge pattern databases used by the latter method could pose a serious memory problem. REFERENCES [1] Zbigniew Zborowski, Expert Method of Solving the Rubik s Cube, 2006, http://www.zborowski.republika.pl/expert3x3x3method.html [2] Rubik's Cube, AI Technology in the Computer Game World, http://aitopics.org/topic/rubiks-cube [3] Richard E. Korf, "Finding Optimal Solutions to Rubik s Cube Using Pattern Databases", AAAI-97 Proceedings, 1997 [4] Thistlethwaite, M.B.: The 45-52 Move Strategy. London CL VIII (1981) 456