cube_solver.solver.solver module

BaseSolver module.

class cube_solver.solver.solver.BaseSolver(use_transition_tables=True, use_pruning_tables=True)[source]

Bases: ABC

Create BaseSolver object.

Parameters:
  • use_transition_tables (bool, optional) – Whether to use transition tables for cube state transitions. If True, creates or loads the tables from the tables/ directory. Default is True.

  • use_pruning_tables (bool, optional) – Whether to use pruning tables to reduce the tree search space. If True, creates or loads the tables from the tables/ directory. Default is True.

See also

solve

Solve a cube position.

num_phases: int = 1

Number of phases of the solving algorithm.

partial_corner_perm: bool

Whether the solving algorithm uses the normal or the partial corner permutation.

partial_edge_perm: bool

Whether the solving algorithm uses the normal or the partial edge permutation.

phase_moves: List[List[Move]]

Available moves for each phase.

transition_defs: List[TransitionDef]

Transition table definitions for each phase.

pruning_defs: List[List[PruningDef]]

Pruning table definitions for each phase.

solved_coords: List[Tuple[int, ...]]

Solved flatten coordinates for each phase.

next_moves: List[Dict[Move, List[Move]]]

Allowed next moves based on the previous move, for each phase.

final_moves: List[Set[Move]]

Final allowed moves for each phase, except the last.

use_transition_tables: bool

Whether to use transition tables for cube state transitions.

use_pruning_tables: bool

Whether to use pruning tables to reduce the tree search space.

transition_tables: Dict[str, ndarray]

Transition tables used to compute cube state transitions.

pruning_tables: Dict[str, ndarray]

Pruning tables used to reduce the tree search space.

nodes: List[int]

Number of visited nodes during a solve for each phase.

checks: List[int]

Number of solve checks during a solve for each phase.

prunes: List[int]

Number of pruned nodes during a solve for each phase.

terminated: bool

Whether the solve search was terminated due to a timeout.

abstract static phase_coords(coords, phase)[source]

Get the coordinates for the specified phase.

Parameters:
  • coords (tuple of int) – Flatten cube coordinates.

  • phase (int) – Solver phase (0-indexed).

Returns:

phase_coords – Phase coordinates.

Return type:

tuple of int

Notes

Depending on the class attributes partial_corner_perm and partial_edge_perm, the coords parameter is the flattened version of the output from the get_coords() method.

get_coords(cube)[source]

Get cube coordinates.

Get the corner orientation, edge orientation, (partial) corner permutation and (partial) edge permutation coordinates, according to partial_corner_perm and partial_edge_perm.

Parameters:

cube (Cube) – Cube object.

Returns:

coords – Cube coordinates in the following order: corner orientation, edge orientation, (partial) corner permutation, (partial) edge permutation, according to partial_corner_perm and partial_edge_perm.

Return type:

tuple of (int or tuple of int)

Examples

>>> from cube_solver import Cube, Kociemba
>>> solver = Kociemba()
>>> cube = Cube("U F2 R2")
>>> solver.get_coords(cube)
(0, 0, 26939, (1007, 11859, 673))
set_coords(cube, coords)[source]

Set cube coordinates.

Set the corner orientation, edge orientation, (partial) corner permutation and (partial) edge permutation coordinates, according to partial_corner_perm and partial_edge_perm.

Parameters:
  • cube (Cube) – Cube object.

  • coords (tuple of (int or tuple of int)) – Cube coordinates in the following order: corner orientation, edge orientation, (partial) corner permutation, (partial) edge permutation, according to partial_corner_perm and partial_edge_perm.

Examples

>>> from cube_solver import Cube, Kociemba
>>> solver = Kociemba()
>>> cube = Cube(random_state=True)
>>> coords = (0, 0, 26939, (1007, 11859, 673))
>>> solver.set_coords(cube, coords)
>>> solver.get_coords(cube)
(0, 0, 26939, (1007, 11859, 673))
is_solved(position, phase)[source]

Whether the cube position is solved at the specified phase.

Parameters:
  • position (Cube or tuple of (int or tuple of int)) – Cube object or cube coordinates to check.

  • phase (int) – Solver phase (0-indexed).

Returns:

True if the cube position is solved, False otherwise.

Return type:

bool

Examples

>>> from cube_solver import Cube, Kociemba
>>> solver = Kociemba()
>>> cube = Cube("U F2 R2")
>>> solver.is_solved(cube, phase=0)
True
>>> solver.is_solved(cube, phase=1)
False
prune(position, phase, depth)[source]

Whether to prune the search tree.

Checks whether the current depth meets the lower bound specified by the pruning_tables.

Parameters:
  • position (Cube or tuple of (int or tuple of int)) – Cube object or cube coordinates to check.

  • depth (int) – Current search depth.

  • phase (int) – Solver phase (0-indexed).

Returns:

True if the search tree should be pruned, False otherwise.

Return type:

bool

Examples

>>> from cube_solver import Cube, Kociemba
>>> solver = Kociemba()
>>> cube = Cube("U F2 R2")
>>> solver.prune(cube, phase=1, depth=2)
True
>>> solver.prune(cube, phase=1, depth=3)
False
next_position(position: Cube, move: Move) Cube[source]
next_position(position: CoordsType, move: Move) CoordsType

Get the next cube position.

Parameters:
  • position (Cube or tuple of (int or tuple of int)) – Cube object or cube coordinates.

  • move (Move) – Move to apply.

Returns:

next_position – Cube object or cube coordinates with the move applied.

Return type:

Cube or tuple of (int or tuple of int)

Examples

>>> from cube_solver import Cube, Move, Kociemba
>>> solver = Kociemba()
>>> cube = Cube("R2")
>>> coords = solver.get_coords(cube)
>>> solver.next_position(cube, Move.R2)
WWWWWWWWWOOOOOOOOOGGGGGGGGGRRRRRRRRRBBBBBBBBBYYYYYYYYY
>>> solver.next_position(coords, Move.R2)
(0, 0, 0, (0, 11856, 1656))
solve(cube, max_length=None, optimal=False, timeout=None, verbose=0)[source]

Solve the cube position.

Parameters:
  • cube (Cube) – Cube object to be solved.

  • max_length (int or None, optional) – Maximum number of moves to search. If None, search indefinitely. Default is None.

  • optimal (bool, optimal) – If True, finds the optimal solution. Default is False.

  • timeout (int or None, optional) – Stop the search after the specified number of seconds. If None, no time limit is applied. Default is None.

  • verbose ({0, 1, 2}, optional) –

    Verbosity level. Default is 0.

    • 0 returns only the solution.

    • 1 logs all solutions found if optimal is True.

    • 2 returns the solution for each phase.

Returns:

solution – Solution for the cube position, or list of solutions for each phase if verbose is 2, or None if no solution is found.

Return type:

Maneuver or list of Maneuver or None

Examples

>>> from cube_solver import Cube, Kociemba
>>> solver = Kociemba()
>>> cube = Cube("L2 U R D' B2 D2 F B D")
>>> solver.solve(cube)
"D' F' B' U2 F2 D L' F2 D2 L2 F2 U D L2 B2 D L2"
>>> solver.solve(cube, optimal=True, verbose=2)
["D' F' B' D2 B2 D R'", "U' L2"]