Upload teacher_dwalton.py with huggingface_hub
Browse files- teacher_dwalton.py +23 -11
teacher_dwalton.py
CHANGED
|
@@ -50,25 +50,37 @@ def _parse_basic_step(step: str) -> Move:
|
|
| 50 |
return Move(face=face, depth=1, width=1, turns=turns)
|
| 51 |
|
| 52 |
|
| 53 |
-
def
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
# Short-circuit: already solved cubes hang the dwalton solver
|
| 58 |
-
if cube.has_uniform_faces():
|
| 59 |
return ()
|
| 60 |
|
| 61 |
_ensure_solver_importable()
|
| 62 |
-
from rubikscubennnsolver.RubiksCube222 import RubiksCube222
|
| 63 |
|
| 64 |
state = cube.to_kociemba_string()
|
| 65 |
-
solver_cube =
|
| 66 |
solver_cube.solve([])
|
| 67 |
-
|
|
|
|
| 68 |
|
| 69 |
-
# Verify the move trace solves our simulator state before using it as a label.
|
| 70 |
check_cube = cube.copy()
|
| 71 |
check_cube.apply_moves(solution)
|
| 72 |
-
if not
|
| 73 |
raise RuntimeError("Teacher solution did not solve cube in local simulator")
|
| 74 |
return solution
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
return Move(face=face, depth=1, width=1, turns=turns)
|
| 51 |
|
| 52 |
|
| 53 |
+
def _solve_cube(cube: Cube, solver_class, goal_check) -> tuple[Move, ...]:
|
| 54 |
+
"""Generic solver: instantiate solver_class, solve, parse, verify."""
|
| 55 |
+
if goal_check(cube):
|
|
|
|
|
|
|
|
|
|
| 56 |
return ()
|
| 57 |
|
| 58 |
_ensure_solver_importable()
|
|
|
|
| 59 |
|
| 60 |
state = cube.to_kociemba_string()
|
| 61 |
+
solver_cube = solver_class(state, "URFDLB", None)
|
| 62 |
solver_cube.solve([])
|
| 63 |
+
raw_steps = [s for s in solver_cube.solution if not s.startswith("COMMENT")]
|
| 64 |
+
solution = tuple(_parse_basic_step(step) for step in raw_steps)
|
| 65 |
|
|
|
|
| 66 |
check_cube = cube.copy()
|
| 67 |
check_cube.apply_moves(solution)
|
| 68 |
+
if not goal_check(check_cube):
|
| 69 |
raise RuntimeError("Teacher solution did not solve cube in local simulator")
|
| 70 |
return solution
|
| 71 |
+
|
| 72 |
+
|
| 73 |
+
def solve_cube_222(cube: Cube) -> tuple[Move, ...]:
|
| 74 |
+
if cube.size != 2:
|
| 75 |
+
raise ValueError(f"solve_cube_222 only supports 2x2, got {cube.size}")
|
| 76 |
+
|
| 77 |
+
from rubikscubennnsolver.RubiksCube222 import RubiksCube222
|
| 78 |
+
return _solve_cube(cube, RubiksCube222, lambda c: c.has_uniform_faces())
|
| 79 |
+
|
| 80 |
+
|
| 81 |
+
def solve_cube_333(cube: Cube) -> tuple[Move, ...]:
|
| 82 |
+
if cube.size != 3:
|
| 83 |
+
raise ValueError(f"solve_cube_333 only supports 3x3, got {cube.size}")
|
| 84 |
+
|
| 85 |
+
from rubikscubennnsolver.RubiksCube333 import RubiksCube333
|
| 86 |
+
return _solve_cube(cube, RubiksCube333, lambda c: c.is_solved())
|