Interface to several Rubik’s cube solvers.

The first is by Michael Reid, and tries to find an optimal solution given the cube’s state, and may take a long time. See http://www.math.ucf.edu/~reid/Rubik/optimal_solver.html

The second is by Eric Dietz, and uses a standard (?) algorithm to solve the cube one level at a time. It is extremely fast, but often returns a far from optimal solution. See https://web.archive.org/web/20121212175710/http://www.wrongway.org/?rubiksource

The third is by Dik Winter and implements Kociemba’s algorithm which finds reasonable solutions relatively quickly, and if it is kept running will eventually find the optimal solution.

AUTHOR:

– Optimal was written by Michael Reid <reid@math.ucf.edu> (2004) – Cubex was written by Eric Dietz <root@wrongway.org> (2003) – Kociemba was written by Dik T. Winter <dik.winter@cwi.nl> (1993) – Initial interface by Robert Bradshaw (2007-08)

class sage.interfaces.rubik.CubexSolver[source]

Bases: object

format_cube(facets)[source]
solve(facets)[source]

EXAMPLES:

sage: # optional - rubiks
sage: from sage.interfaces.rubik import *
sage: C = RubiksCube("R U")
sage: CubexSolver().solve(C.facets())
'R U'
sage: C = RubiksCube("R U F L B D")
sage: sol = CubexSolver().solve(C.facets()); sol
"U' L' L' U L U' L U D L L D' L' D L' D' L D L' U' L D' L' U L' B' U' L' U B L D L D' U' L' U L B L B' L' U L U' L' F' L' F L' F L F' L' D' L' D D L D' B L B' L B' L B F' L F F B' L F' B D' D' L D B' B' L' D' B U' U' L' B' D' F' F' L D F'"
sage: RubiksCube(sol) == C
True
sage: C = RubiksCube("R2 F'")
sage: CubexSolver().solve(C.facets())
"R' R' F'"
sage: C = RubiksCube().scramble()
sage: sol = CubexSolver().solve(C.facets())
sage: C == RubiksCube(sol)
True
>>> from sage.all import *
>>> # optional - rubiks
>>> from sage.interfaces.rubik import *
>>> C = RubiksCube("R U")
>>> CubexSolver().solve(C.facets())
'R U'
>>> C = RubiksCube("R U F L B D")
>>> sol = CubexSolver().solve(C.facets()); sol
"U' L' L' U L U' L U D L L D' L' D L' D' L D L' U' L D' L' U L' B' U' L' U B L D L D' U' L' U L B L B' L' U L U' L' F' L' F L' F L F' L' D' L' D D L D' B L B' L B' L B F' L F F B' L F' B D' D' L D B' B' L' D' B U' U' L' B' D' F' F' L D F'"
>>> RubiksCube(sol) == C
True
>>> C = RubiksCube("R2 F'")
>>> CubexSolver().solve(C.facets())
"R' R' F'"
>>> C = RubiksCube().scramble()
>>> sol = CubexSolver().solve(C.facets())
>>> C == RubiksCube(sol)
True
# optional - rubiks
from sage.interfaces.rubik import *
C = RubiksCube("R U")
CubexSolver().solve(C.facets())
C = RubiksCube("R U F L B D")
sol = CubexSolver().solve(C.facets()); sol
RubiksCube(sol) == C
C = RubiksCube("R2 F'")
CubexSolver().solve(C.facets())
C = RubiksCube().scramble()
sol = CubexSolver().solve(C.facets())
C == RubiksCube(sol)
class sage.interfaces.rubik.DikSolver[source]

Bases: object

facet_map = [1, 2, 3, 4, 0, 5, 6, 7, 8, 9, 10, 11, 17, 18, 19, 25, 26, 27, 33, 34, 35, 12, 0, 13, 20, 0, 21, 28, 0, 29, 36, 0, 37, 14, 15, 16, 22, 23, 24, 30, 31, 32, 38, 39, 40, 41, 42, 43, 44, 0, 45, 46, 47, 48]
format_cube(facets)[source]
rot_map = {'B': 'U', 'D': 'B', 'F': 'D', 'L': 'L', 'R': 'R', 'U': 'F'}
solve(facets, timeout=10, extra_time=2)[source]

EXAMPLES:

sage: # optional - rubiks
sage: from sage.interfaces.rubik import *
sage: C = RubiksCube().move("R U")
sage: DikSolver().solve(C.facets())
'R U'
sage: C = RubiksCube().move("R U F L B D")
sage: DikSolver().solve(C.facets())
'R U F L B D'
sage: C = RubiksCube().move("R2 F'")
sage: DikSolver().solve(C.facets())
"R2 F'"
>>> from sage.all import *
>>> # optional - rubiks
>>> from sage.interfaces.rubik import *
>>> C = RubiksCube().move("R U")
>>> DikSolver().solve(C.facets())
'R U'
>>> C = RubiksCube().move("R U F L B D")
>>> DikSolver().solve(C.facets())
'R U F L B D'
>>> C = RubiksCube().move("R2 F'")
>>> DikSolver().solve(C.facets())
"R2 F'"
# optional - rubiks
from sage.interfaces.rubik import *
C = RubiksCube().move("R U")
DikSolver().solve(C.facets())
C = RubiksCube().move("R U F L B D")
DikSolver().solve(C.facets())
C = RubiksCube().move("R2 F'")
DikSolver().solve(C.facets())
class sage.interfaces.rubik.OptimalSolver(verbose=False, wait=True)[source]

Bases: object

Interface to Michael Reid’s optimal Rubik’s Cube solver.

format_cube(facets)[source]
ready()[source]
solve(facets)[source]

The initial startup and precomputation are substantial…

Todo

Let it keep searching once it found a solution?

EXAMPLES:

sage: # optional - rubiks
sage: from sage.interfaces.rubik import *
sage: solver = DikSolver()
sage: solver = OptimalSolver()      # long time (28s on sage.math, 2012)
Initializing tables...
Done.
sage: C = RubiksCube("R U")
sage: solver.solve(C.facets())
'R  U'
sage: C = RubiksCube("R U F L B D")
sage: solver.solve(C.facets())
'R  U  F  L  B  D'
sage: C = RubiksCube("R2 D2")
sage: solver.solve(C.facets())
'R2 D2'
>>> from sage.all import *
>>> # optional - rubiks
>>> from sage.interfaces.rubik import *
>>> solver = DikSolver()
>>> solver = OptimalSolver()      # long time (28s on sage.math, 2012)
Initializing tables...
Done.
>>> C = RubiksCube("R U")
>>> solver.solve(C.facets())
'R  U'
>>> C = RubiksCube("R U F L B D")
>>> solver.solve(C.facets())
'R  U  F  L  B  D'
>>> C = RubiksCube("R2 D2")
>>> solver.solve(C.facets())
'R2 D2'
# optional - rubiks
from sage.interfaces.rubik import *
solver = DikSolver()
solver = OptimalSolver()      # long time (28s on sage.math, 2012)
C = RubiksCube("R U")
solver.solve(C.facets())
C = RubiksCube("R U F L B D")
solver.solve(C.facets())
C = RubiksCube("R2 D2")
solver.solve(C.facets())
start()[source]
stop()[source]
class sage.interfaces.rubik.SingNot(s)[source]

Bases: object

This class is to resolve difference between various Singmaster notation.

Case is ignored, and the second and third letters may be swapped.

EXAMPLES:

sage: from sage.interfaces.rubik import SingNot
sage: SingNot("acb") == SingNot("ACB")
True
sage: SingNot("acb") == SingNot("bca")
False
>>> from sage.all import *
>>> from sage.interfaces.rubik import SingNot
>>> SingNot("acb") == SingNot("ACB")
True
>>> SingNot("acb") == SingNot("bca")
False
from sage.interfaces.rubik import SingNot
SingNot("acb") == SingNot("ACB")
SingNot("acb") == SingNot("bca")