Skip to content

About

Rubik's cube solver web app with three solvers: Kociemba two-phase search, human-style CFOP, and a neural network trained with value iteration.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

Cube Solver

A Rubik's cube solver with three different solvers in one web app: a classic search algorithm, the method people learn by hand, and a neural network that taught itself.

Scrambling a cube and solving it with CFOP, stage by stage

CI

What it does

  • Scramble, paint or reset a 3D cube you can drag to rotate. Every turn is animated, and the scramble can be stepped through move by move.
  • Pick a solver and get a solution you can play, pause and step through.
  • Kociemba has a thinking time slider: the longer it thinks, the shorter the solution.
  • CFOP shows its solution in named stages (Cross, Pair 1–4, the OLL case and the PLL perm), highlighting the stage you're on.

The solvers

100 scrambles of 30 random moves each, from scripts/benchmark.py:

Solver Solved Average moves Most moves Average time Slowest
Kociemba (two-phase), 0.1 s 100/100 20.8 22 0.23 s 1.35 s
Kociemba (two-phase), 1 s 100/100 20.5 22 1.01 s 1.32 s
CFOP (human) 100/100 54.1 64 0.11 s 1.78 s
Neural (value iteration) 72/100 21.9 27 4.14 s 6.33 s

Kociemba finds the shortest solutions. CFOP is the fastest but uses about 2.6× as many moves, which is normal for a method a person can follow. The neural solver's solutions are nearly as short as Kociemba's, but it gives up on some scrambles. Retraining it is planned.

Kociemba two-phase

Herbert Kociemba's algorithm splits the problem in two. Phase 1 moves the cube into a subgroup where edges and corners are oriented and the middle-layer edges are in the middle layer. Phase 2 solves it using only moves that stay in that subgroup.

  • Each phase searches with iterative-deepening A* over small coordinates (twist, flip, slice position and so on) instead of whole cubes.
  • Move tables and pruning tables (about 7 MB) are built on first run, saved to disk, and give a lower bound on the moves left.
  • The solver is anytime: once it has a solution, it keeps trying longer phase-1 paths with shorter phase-2 finishes until its thinking time runs out.

CFOP

The method most speedcubers use, built from scratch:

  1. Cross: a breadth-first search over all 190,080 positions of the four cross edges gives the shortest cross.
  2. F2L: the four corner-edge pairs are solved one at a time, quickest first, by iterative deepening. The search is pruned by the cross table and by a table for each pair together with the two cross edges beside it.
  3. OLL: all 57 cases, tried with every turn of the top layer; the shortest wins.
  4. PLL: all 21 cases, with every turn before and after.

The algorithms are written in standard notation (including wide turns, slice moves and rotations) and expanded into face turns. Tests check that every one of the 216 OLL and 288 PLL positions is solved.

Neural (value iteration)

A network learns how far any cube is from solved without ever seeing a solution, in the style of DeepCubeA.

  • Model: a DeepCubeA-style residual network (about 14.7M parameters) that reads the 54 stickers as a 324-value one-hot vector.
  • Training: approximate value iteration. Each cube's target is 1 plus the smallest predicted distance among its 18 neighbours, using a frozen copy of the network that's updated as the loss falls. 500,000 scrambled cubes per iteration with bf16 mixed precision.
  • Solving: beam search (width 300) keeps the cubes the network thinks are closest to solved.

How it fits together

cube_solver/
├── frontend/    3D cube, animations and controls (HTML, CSS, JavaScript)
├── backend/     FastAPI app: /solve, /scramble, /apply, /methods
├── cube/        cube model, moves and validation
│   ├── kociemba/   two-phase solver, coordinates and pruning tables
│   └── cfop/       cross, F2L, OLL/PLL algorithms and stages
├── training/    PyTorch: data, models, value iteration, beam search
├── scripts/     benchmark.py
├── notebooks/   data exploration
└── tests/       900+ pytest tests

The backend keeps a registry of solvers. Kociemba and CFOP are always available; the neural solver is added only if PyTorch and a trained checkpoint are found.

Running it

You'll need uv and Python 3.12.

uv sync
uv run fastapi dev backend/main.py

Then open http://localhost:8000. The first start builds the Kociemba tables, which takes about two minutes. They're saved to cube/kociemba/data/, so later starts take a few seconds.

The neural solver needs PyTorch and a checkpoint, which isn't in the repo (it's about 59 MB). Train one with training/value_iteration.py, or point CUBE_MODEL_PATH at an existing one, then run:

uv run --group training fastapi dev backend/main.py

Tests and benchmark

uv run pytest
uv run ruff check .
uv run --group training python scripts/benchmark.py --scrambles 100

There are over 900 tests. CI runs them, along with ruff, on every pull request, and pre-commit runs ruff before each commit.

Built with

Python, NumPy, FastAPI, PyTorch, vanilla JavaScript and CSS 3D transforms, uv, pytest, ruff and GitHub Actions.

License

MIT

About

Rubik's cube solver web app with three solvers: Kociemba two-phase search, human-style CFOP, and a neural network trained with value iteration.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages