Resolver Rubik Usando Sat
Resolver Rubik Usando Sat
Resolver Rubik Usando Sat
Jingchao Chen
1 Introduction
SAT solvers have attained success in many fields, and have been used widely
for hardware design and verification, software verification, artificial intelligence,
cryptanalysis, equivalence checking, model checking, planning, scheduling etc.
However, there are still large real world instances that cannot be solved by SAT
solvers. Rubik’s Cube is such an example. It is a well-known planning problem,
which is originally called the “magic cube”. The puzzle game was invented in
2
1974[1] by Ernö Rubik. So far, It has been studied for a long time. Yet many
simple properties remain unknown. From the viewpoint of SAT applications, this
paper studies this puzzle.
With respect to this puzzle, one of the most natural questions is how many
moves are required to solve Rubik’s Cube in the worst case. This problem has
been studied for over 30 years. There has been great progress. In 1995, Reid
proved that the lower bound on the number of moves and the upper bound is
20 and 29, respectively [5,6,7]. Since then, the upper bound was unceasingly im-
proved. In 2006, the upper bound was reduced by Radu [8] to 27. In 2007, Kunkle
and Cooperman used computer search methods to refine it to 26. In 2008, Rokicki
[3,4] reduced further it from 25 to 22. In 2010, this open problem was settled.
Rokicki, Kociemba, Davidson and Dethridge [9,10] proved that God’s number
(i.e. the upper bound) for the Cube is exactly 20. They spent about 35 CPU-
years of idle computer donated by Google to solve all 43,252,003,274,489,856,000
positions of the Cube. Without a doubt, all the current approaches to proving
the upper bound are time-consuming and space-consuming. How to finish the
theoretical proof of the upper bound is yet a hard problem.
Apart from the approach to compute directly positions of the Cube, one
considered the other approaches. In 1985, Korf [11] noted that problems such
as Rubik’s Cube can be divided into subgoals that are of the property called
operator decomposability, and attempted to solve them by searching for macro-
operators. Korf’s approach succeeded for the 2 × 2 × 2 version of Rubik’s Cube,
but failed to find an optimal solution for the full 3×3×3 Rubik’s Cube, for which
the solution lengths are close to those of human strategies. Rubik’s Cube is also a
well-known planning problem. This puzzle can be encoded as a planning problem
in PDDL (Planning Domain Definition Language). Nevertheless, there is no
report on solving successfully it using a sat-based planner such as SATPLAN
[12].
The purpose of this paper is two-fold. The first purpose is to design an
effective SAT encoding of Rubik’s Cube. The second purpose is to improve the
existing SAT solvers to extend the range of SAT applications. To attain the first
purpose, we optimize the encoding of this puzzle in the following ways: encoding
the At-Most-One (AMO) constraint for minimizing the number of moves by the
2-product encoding [14] proposed recently, and replacing a naive approach of 6
Boolean variables to represent each color on each facelet with a new approach of
2 Boolean variables. The number of Boolean variables required by a cube state is
cut from 8 × 6 × 6 = 288 to 8 × 6 × 3 = 144 or 8 × 6 × 2 = 96. Encoding this puzzle
according to the idea of one-phase algorithm results in a very hard SAT problem.
Therefore, we encode this problem according to the idea of two-phase algorithm,
and consider the goal state of phase 1 as an additional constraint. In addition to
efficiently encoding this puzzle, we improve the existing SAT solvers, and develop
a new hybrid SAT solver based on PrecoSAT [13], though the new solver is suited
only for Rubik’s Cube. The new solver selects some decision variables according
to whether a variable occurs in a clause with the AMO constraint, and splits
the original problem into some subproblems with those decision variables. Each
3
2 Preliminaries
0 1 2
3 4 5
6 7 8
front left back right up down
here denotes a clockwise quarter turn of the “up” face, and similarly, D, L, R, F
and B denote “down”, “left”, “right”, “front” and “back”, respectively. A solu-
tion can be represented by a move sequence. As an example, the move sequence
F 2U 2B ′ U ′ B2D′ U 2F ′ U 2LDR2B2U 2F ′U 2F ′ U 2B2L2 is a solution to the state
shown in Figure 1. That is, performing in turn each move in this sequence can
restores that state to the home state. As defined in [3], we define S18 the set
of 18 moves mentioned above, and A10 = {U, U ′ , U 2, D, D′ , D2, L2, R2, F 2, B2},
which will be used in subsequent sections and the two-phase algorithm given
later.
A Rubik’s Cube consists of different cubies. By convention, the cubies are
classified into edge cubies (two visible facelets), corner cubies (three visible
facelets) and center cubies (one visible facelets, in the center of a side). Cor-
respondingly, according to the cubie where a facelet belongs, the facelets are
classified into edge facelets, corner facelets and center facelets.
One of goals of this paper is to translate the Rubik’s cube puzzle into a sat-
isfability (SAT) problem with CNF (conjunctive normal form). A propositional
logic formula is said to be in CNF if it is a conjunction (“and”) of clauses, each
clause being a disjunction (“ors”) of Boolean literals, where each literal is either
a variable or the negation of a variable.
The Rubik’s Cube puzzle may be described by the initial state, the move se-
quence, the map relation of each move and the solved state. Its SAT encoding
will consist of such ingredients. A Rubik’s Cube has a total of six colors. A naive
approach is that a color corresponds a Boolean variable. Thus, representing each
color on each facelet requires six Boolean variables. In fact, six colors contains
only log 6 ≈ 2.6 bit information. So the number of Boolean variables can be
reduced. Let b1 b2 b3 be the binary representation of k(0 ≤ k ≤ 5). The Boolean
variable representation of the k-th color is x1 (b1 ), x2 (b2 ), x3 (b3 ), where xi (bi ) is
xi if bi is 1, and xi otherwise. For example, the Boolean variable representa-
tion of the second color is x1 , x2 , x3 . Therefore, 3 Boolean variables suffice for
representing the color of each facelet. In our SAT encoding, states are divided
into two categories: general state and H-state. A state is said to be H-state
if it can be transformed into the solved state by a sequence of the moves in
A10 mentioned above. In the two-phase algorithm, each state in Phase two is
H-state. For general states, we represent each color on each facelet with three
Boolean variables. For H-states, we represent each color on each facelet with two
Boolean variables. In the 2-variable scheme, we represent the colors of the front,
left, back, right face in the solved state by 00, 01, 10 and 11, respectively, and
then re-use 00 and 01 to represent the colors of the other two (top and down)
faces. Notice, any move in in A10 cannot transform any facelet on top and down
faces to somewhere on the other four faces. H-states are allowed to use only
moves in A10 . Therefore, under H-states, the 2-variable scheme does not yield
any confusing.
5
Let c(i, j, m) be the color of the j-th facelet in the i-the face under the m-th
(m ≥ 1) state (hereafter, color of facelet (i, j, m), for short), c(i, 4, 1) the center
facelet color of the i-th face under the initial state. If the m-th state is the solved
state, this state may be represented by
V
c(i, j, m) = c(i, 4, 1)
1≤i≤6,0≤j≤8
Using 3-variable scheme, c(i, j, m) = c(i, 4, 1) is translated into c(i, j, m, 1) =
c(i, 4, 1, 1) ∧ c(i, j, m, 2) = c(i, 4, 1, 2) ∧ c(i, j, m, 3) = c(i, 4, 1, 3), where c(. . . 1),
c(. . . 2) and c(. . . 3) are literals that denote the 1st, 2nd and 3rd bit of a color.
Formula c(i, j, m, 1) = c(i, 4, 1, 1) can be translated into the following clauses:
(c(i, j, m, 1) ∨ ¬c(i, 4, 1, 1)) ∧ (¬c(i, j, m, 1) ∨ c(i, 4, 1, 1)).
An initial state of a cube is considered as State 1, which is interpreted as
V
B(c(i, j, 1, k))
1≤i≤6,0≤j≤8,k=1,2,3
where B(c(i, j, 1, k)) is defined as c(i, j, 1, k) if the value of the k-th bit color of
facelet(i, j, 1) is 1, and ¬c(i, j, 1, k) otherwise.
Assume we take at most n − 1 moves to solve Rubik’s Cube, and associate
a Boolean variable st with each state t(1 ≤ t ≤ n). “ st = true ” mean the t-th
state is the solved state. Then, this constraint can be represented by
V
(¬st ∨ c(i, j, t) = c(i, 4, 1))
1≤i≤6,0≤j≤8
This formula can be converted easily into clauses.
At any time, among S = {s1 , s2 , . . . , sn }, we must ensure that exactly one st
is true. The exactly-one constraint can be formalized by the at-least-one (ALO)
and at-most-one (AMO) constraint. That is, exactly-one(S) ≡ ALO (S)∧ AMO
(S). The standard SAT encodings of constraints ALO and AMO are the follow-
ing.
ALO(S) ≡ s1 ∨ s2 ∨ · · · ∨ sn
AMO(S) ≡ {si ∨ sj |si , sj ∈ S, i < j}
The ALO constraint ensures that a variable is true. And the AMO constraint
ensures that no more than one variable is true. The standard AMO encoding
requires much more clauses. To reduce the number of clauses, we can apply a
two-product AMO encoding [14], which is recursively defined as
V
1≤k≤n,k=(i−1)q+j
AMO(S) ≡ AMO(U ) ∧ AMO(V ) ((xk ∨ ui ) ∧ (xk ∨ vj ))
1≤i≤p,1≤j≤q
√
where p = ⌈ n⌉, q = ⌈ np ⌉, U = {u1 , u2 , . . . , up }, V = {v1 , v2 , . . . , vq }, each ele-
ment ui in U and each element vj in V are auxiliary variables. Here, AMO(U )
and AMO(V ) apply the standard AMO encoding. The number of clauses and
auxiliary variables required by this encoding are 2n + p(p − 1)/2 + q(q − 1)/2
and p + q, respectively. When n = 20, the number of clauses required is 40 +
4(4 − 1)/2 + 5(5 − 1)/2 = 56. For the n, the standard AMO encoding requires
20(20-1)/2=190 clauses.
To encode efficiently the constraints on the turns, we classify the turns of
Rubik’s Cube into six classes: u, d, l, r, f and b. Let uk (1 ≤ k ≤ n) be a Boolean
variable that is associated with the up turn of step k. We perform either U -, or
U ′ - or U 2-type up turn at step k when uk is true, and do the other turn otherwise.
6
Some two-move sequences will yield the same result. For example, two-moves
U D and DU have the same result states. To speed up the search, we remove
the search on two-move sequences such as DU . The removing of such a search
can be done by adding the following constraint clauses to the SAT encoding of
Rubik’s
V Cube.
((uk ∨ dk+1 ) ∧ (lk ∨ rk+1 ) ∧ (fk ∨ bk+1 ))
1≤k≤n
Kociemba’s Algorithm
d ← 0, t ← ∞
while d < t do
d
for s ∈ S18 , ps ∈ H do
if d + D(ps) < t then
find a better solution, using moves in A10
t ← d + D(ps)
end if
end for
d←d+1
end while
This algorithm assumes that the original state is p, and applies some move
d
sequence s ∈ S18 (for the definition of S18 , see Section 2) of length d to the
original cube yielding ps which lies in H. This search process is called phase
one. Here H is a subset of states that is composed of all patterns with following
characteristics:
These characteristics are preserved by moves in the set A10 (which is defined
in Section 2). The search process from the new state ps to the fully solved state is
called Phase two. In this phase, each move is in A10 . D(ps) returns the distance
from the state ps to the solved state using moves in A10 . To efficiently complete
this computation, it is usually done by a lookup table. In fact, Phase one is
usually also done by a lookup table.
8
solver based on PrecoSAT. The basic framework of this new solver is similar to
MoRsat [15], but replaces the lookahead solving strategy with an ALO (at-least-
one) solving strategy. Let the notation F (x) denotes the resulting formula after
assigning literal x true and performing iterative unit propagation. The basic
idea of the ALO solving strategy is to decompose the original problem F into
subproblems F (xi )(1 ≤ i ≤ n) if a clause C in F is x1 ∨ x2 ∨ · · · ∨ xn . If no sub-
problem is satisfiable, the original problem is unsatisfiable. Each subproblem can
be solved in a recursive way. Once the recursive depth reaches some constant,
say 4, we use PrecoSAT to solve that subproblem. This ALO solving strategy
has been applied successfully to MPhaseSAT [17]. In the new solver used for
Rubik’s Cube, the size of the clause C used to decompose the original problem
is specified to 6. And the recursive depth of the ALO solving strategy is limited
to 4. Under this assumption, this new solver may be described in a recursive way
as follows.
6 Experimental Studies
We solved Rubik’s Cube with the SAT solver described in previous section on
a 2.40GHz machine with Intel Core 2 Quad Q6600 CPU. Without adding the
constraint conditions of two-phase algorithm given in Section 4, we found that
determining whether a cube has an optimal solution with the maneuver (a move
sequence is called a maneuver) length of 13 took about 7 hours in the worst
case by our SAT solver. If the maneuver length of a solution is greater than 13,
in general, no modern SAT Solver cannot find efficiently an optimal solution.
However, if adding the constraint condition of two-phase algorithm, it is easy
10
The time to solve a cube greatly depends on the given states. The cube state
shown in Figure 1 is an easy example. We found the following six solutions to
this example by setting different faces to the U - and D-face, and using different
encoding strategies, based on two-phase algorithm.
1. F 2RDF ′ R′ D2U L′ DF L2U R2D2L2U ′L2B2F 2R2
2. R′ F RBU ′ B ′ D′ LU ′ F ′ U ′ R2B2U ′ B2L2U 2L2D2F 2
3. F ′ L′ F ′ U 2BU ′ F ′ DF 2LB2R2D2F 2R2F D2B ′F 2U 2
4. U F ′ RL′ B2F 2LBD′ B ′ RB2LB2LD2B2L′B2U 2
5. U F ′ D2U 2R′ L2F U ′ F ′ RF 2R′ L2D2LF 2D2R′B2U 2
6. U F R2LB2F 2L′B ′ U ′ B ′ RD2B2D2B2U 2L2B2R′L′
Each solution took about 200 seconds. These solutions have a common char-
acteristic: both the length of the phase 1 maneuvers and the length of the phase
2 maneuvers are 10, and the total length is 20. These solutions are not the short-
est. Finding a shorter solution will take much more time, since the length of the
phase 1 maneuvers increases with the shortening of solutions.
A special cube state which flips all 12 edges, called superflip, is a hard example
to our SAT solver. It has been proven to have a shortest maneuver length of
20 moves to be solved. Finding a shortest solution to the superflip is time-
consuming. However, it is easy to find a near-optimal solution. Actually, we
took 532 seconds to find a solution with the length of 21 moves as follows.
BF ′ L′ U 2F 2LD′U ′ F ′ R′ LF 2U 2R2B2U R2D′B2U ′ R2
If the length of the phase 1 maneuvers is known to be 13 in advance, for the
superflip, we can find easily a shortest solution of length 20 as follows.
BF U 2R′ D′ U L2B2R2B ′U 2R′ L′ U ′ L2U ′ B2D′ L2U ′
It took about 310 seconds. Note that this solution is different from that given
by Cube Explorer [16] that implements two-phase algorithm using lookup tables.
11
To test the generalized case, we generated 10 randomly cube states. For each
state, the time required by our SAT solver to search for a solution of length 20
or less is shown in Table 2.
The upper bound on the length of the phase 1 maneuvers has been shown
to 12 [7]. Our experiments verified that this fact is true. Furthermore, within
length 12 of the phase 1 maneuvers, we found always a solution of length 20
or less. As the length of the phase 1 maneuvers increases, the time to solve a
cube increases sharply. In most of the cases, we can find a solution within 7000
seconds. If the length of solutions is allowed to be 21, the time to solve a cube
by our SAT solver never exceeds 1500 seconds.
Table 2. Runtime took by our SAT solver to solve 10 random cube states
cube state solution length time (seconds) solution length time (seconds)
(non-optimal) (non-optimal) (optimal) (optimal)
1 19 0.3 19 9125
2 19 4.5 18 531
3 18 1.5 18 580
4 16 7.3 16 23
5 19 0.3 18 3243
6 18 1.5 17 315
7 17 2.1 17 25
8 19 0.1 18 4216
9 19 1.1 18 2219
10 19 0.1 18 1452
The test platform in Table 3 are the same as that in Table 2. Compared with
the SAT solver, Cube Explorer using lookup tables is much faster. As shown in
Table 3, Cube Explorer took at most 8 seconds to find a non-optimal solution
12
whose length is at most 20. In most of the cases, it found a solution immediately.
However, to find an optimal solution, it is also time-consuming in some cases.
For example, finding an optimal solution of cube state 1 took 9125 seconds. If
information on lookup tables can be encoded in CNF, it is possible that the SAT
solver can outperform Cube Explorer.
Our encoding follows almost the idea of brute force enumeration. However,
it is impossible for brute force enumeration to find a solution of length 20 with
length 12 of the phase 1 maneuvers on a modern PC in a reasonable time, since
brute force enumeration has to check about 1512 × 910 ≈ 1023.7 states (note each
state consists of 48 movable facclets) in the worst case. Hence, the SAT solver
has its advantage over generalized approaches such as brute force enumeration.
The main advantage of solving a cube with SAT solvers is that it does not
need a huge lookup table indicating the distance from the home state, and the
memory requirement is very less. 10 MB RAM memory is enough.
7 Conclusions
This paper is the first to solve Rubik’s Cube using a SAT solver. The experimen-
tal results reveal that our SAT encoding of Rubik’s Cube and the improvement
on the existing SAT solver are effective. Using the improved SAT solver, Rubik’s
Cube can be solved in a reasonable time. We believe that the encoding ap-
proaches and the ALO solving technique should be useful beyond the planning
domain such as Rubik’s Cube.
Here many open problem remains. For example, what is the optimal SAT
encoding of Rubik’s Cube? The heuristic approach is frequently used to speed
up SAT solvers. Can the heuristics information be encoded into a SAT formula?
Indeed, in this paper, we implemented the SAT encoding of partial heuristic
information such as the goal state information of phase 1 in two-phase algo-
rithm. However, we cannot still encode the information on lookup tables (pat-
tern database of Rubik’s Cube) in Kociemba’s algorithm, which is used to prune
the superfluous search space. When a SAT formula is given, can we exploit logic
structures on the heuristic information? What two-phase algorithm uses is a
depth-first search technique. How to encode the depth-first search technique in
CNF is also a challenge. Although one now has proved that God’s number or the
upper bound on the number of moves for Rubik’s Cube is exactly 20, We cannot
encode yet such a problem into a SAT problem. Nevertheless, in near future,
we believe that it is possible to perform the SAT encoding of the upper bound
of Rubik’s Cube by extending the current SAT encoding technique of Rubik’s
Cube.
References
2. Kunkle, D., Cooperman, G.: Twenty-Six Moves Suffice for Rubik’s Cube, Pro-
ceedings of the International Symposium on Symbolic and Algebraic Computa-
tion (ISSAC ’07), 2007, http://www.ccs.neu.edu/home/gene/papers/rubik.pdf
3. Rokicki, T.: Twenty-Five Moves Suffice for Rubik’s Cube,
http://arxiv.org/abs/0803.3435.
4. Rokicki, T.: Twenty-Two Moves Suffice, 2008,
http://cubezzz.homelinux.org/drupal/?q=node/view/121
5. Rokicki, T.: In search of: 21f*s and 20f*s; a four month odyssey, 2006,
http://cubezzz.homelinux.org/drupal/?q=node/view/56
6. Reid, M.: Superflip requires 20 face turns, 1995,
http://www.math.rwth-aachen.de/ Martin.Schoenert/Cube-Lovers/
michael re%id superflip requires 20 face turns.html
7. Reid, M.: New upper bounds, 1995,
http://www.math.rwth-aachen.de/ Martin.Schoenert/Cube-Lovers/
michael re%id new upper bounds.html
8. Radu, S.: Rubik can be solved in 27f, 2006,
http://cubezzz.homelinux.org/drupal/?q=node/view/53.
9. Flatley, J.F.: Rubik’s Cube solved in twenty moves, 35 years of CPU time,
Engadget, 2010.
10. Rokicki,T., Kociemba, H., Davidson, M., Dethridge, J.: God’s Number is 20,
2010, www.cube20.org.
11. Korf R. E.: Macro-Operators: A Weak Method for Learning, Artificial Intelli-
gence 26, 35-77 (1985)
12. Kautz, H.A., Selman B.: Planning as Satisfiability, European Conference on
Artificial Intelligence (ECAI’92), 359–363 (1992)
13. Biere, A.: Lingeling, Plingeling, PicoSAT and PrecoSAT at SAT Race 2010,
http://baldur.iti.uka.de/sat-race-2010/descriptions/solver 1+2+3+6.pdf
14. Chen, J.C.: A new SAT encoding of the at-most-one constraint, Proc. of the
Tenth Int. Workshop of Constraint Modelling and Reformulation, St. Andrews,
Scotland, UK, 2010.
15. Chen, J.C.: Building a Hybrid SAT Solver via Conflict-driven, Look-ahead and
XOR Reasoning Techniques, SAT 2009, LNCS 5584, 298-311 (2009)
16. Kociemba, H.:Cube Explorer (Windows program),
http://kociemba.org/cube.htm
17. Chen, J.C.: The SAT solver, MPhaseSAT, submitted to SAT 2011 Competition.
18. Heule, M., Van Maaren, H.: Effective Incorporation of double look-ahead pro-
cedures, SAT 2007, LNCS 4501, 258–271 (2007)