rubik-cube


Namerubik-cube JSON
Version 0.0.2 PyPI version JSON
download
home_pagehttps://github.com/pglass/cube
SummaryA basic, pure-Python Rubik's cube solver
upload_time2023-05-13 20:22:33
maintainer
docs_urlNone
authorPaul Glass
requires_python
licenseMIT
keywords rubik cube solver
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            ![PyPI](https://img.shields.io/pypi/v/rubik-cube)
![PyPI - Python Version](https://img.shields.io/pypi/pyversions/rubik-cube)

# Overview

This is a Python 3 implementation of a (3x3) Rubik's Cube solver.

It contains:

- A simple implementation of the cube
- A solver that follows a fixed algorithm
- An unintelligent solution sequence optimizer
- A decent set of test cases

## Installation

The package is hosted on PyPI.

```
pip install rubik-cube
```

Import from the `rubik` package,

```python
>>> from rubik.cube import Cube
>>> c = Cube("OOOOOOOOOYYYWWWGGGBBBYYYWWWGGGBBBYYYWWWGGGBBBRRRRRRRRR")
>>> print(c)
    OOO
    OOO
    OOO
YYY WWW GGG BBB
YYY WWW GGG BBB
YYY WWW GGG BBB
    RRR
    RRR
    RRR
```

## Implementation

### Piece

The cornerstone of this implementation is the Piece class. A Piece stores two
pieces of information:

1. An integer `position` vector `(x, y, z)` where each component is in {-1, 0,
1}:
    - `(0, 0, 0)` is the center of the cube
    - the positive x-axis points to the right face
    - the positive y-axis points to the up face
    - the positive z-axis points to the front face

2. A `colors` vector `(cx, cy, cz)`, giving the color of the sticker along each
axis. Null values are place whenever that Piece has less than three sides. For
example, a Piece with `colors=('Orange', None, 'Red')` is an edge piece with an
`'Orange'` sticker facing the x-direction and a `'Red'` sticker facing the
z-direction. The Piece doesn't know or care which direction along the x-axis
the `'Orange'` sticker is facing, just that it is facing in the x-direction and
not the y- or z- directions.

Using the combination of `position` and `color` vectors makes it easy to
identify any Piece by its absolute position or by its unique combination of
colors.

A Piece provides a method `Piece.rotate(matrix)`, which accepts a (90 degree)
rotation matrix. A matrix-vector multiplication is done to update the Piece's
`position` vector. Then we update the `colors` vector, by swapping exactly two
entries in the `colors` vector:

- For example, a corner Piece has three stickers of different colors. After a
  90 degree rotation of the Piece, one sticker remains facing down the same
  axis, while the other two stickers swap axes. This corresponds to swapping the
  positions of two entries in the Piece’s `colors` vector.
- For an edge or face piece, the argument is the same as above, although we may
  swap around one or more null entries.

### Cube

The Cube class is built on top of the Piece class. The Cube stores a list of
Pieces and provides nice methods for flipping slices of the cube, as well as
methods for querying the current state. (I followed standard [Rubik's Cube
notation](http://ruwix.com/the-rubiks-cube/notation/))

Because the Piece class encapsulates all of the rotation logic, implementing
rotations in the Cube class is dead simple - just apply the appropriate
rotation matrix to all Pieces involved in the rotation. An example: To
implement `Cube.L()` - a clockwise rotation of the left face - do the
following:

1. Construct the appropriate [rotation matrix](
http://en.wikipedia.org/wiki/Rotation_matrix) for a 90 degree rotation in the
`x = -1` plane.
2. Select all Pieces satisfying `position.x == -1`.
3. Apply the rotation matrix to each of these Pieces.

To implement `Cube.X()` - a clockwise rotation of the entire cube around the
positive x-axis - just apply a rotation matrix to all Pieces stored in the
Cube.

### Solver

The solver implements the algorithm described
[here](http://www.chessandpoker.com/rubiks-cube-solution.html). It is a
layer-by-layer solution. First the front-face (the `z = 1` plane) is solved,
then the middle layer (`z = 0`), and finally the back layer (`z = -1`). When
the solver is done, `Solver.moves` is a list representing the solution
sequence.

My first correct-looking implementation of the solver average 252.5 moves per
solution sequence on 135000 randomly-generated cubes (with no failures).
Implementing a dumb optimizer reduced the average number of moves to 192.7 on
67000 randomly-generated cubes. The optimizer does the following:

1. Eliminate full-cube rotations by "unrotating" the moves (Z U L D Zi becomes
L D R)
2. Eliminate moves followed by their inverse (R R Ri Ri is gone)
3. Replace moves repeated three times with a single turn in the opposite
direction (R R R becomes Ri)

The solver is not particularly fast. On my machine (a 4.0 Ghz i7), it takes
about 0.06 seconds per solve on CPython, which is roughly 16.7 solves/second.
On PyPy, this is reduced to about 0.013 seconds per solve, or about 76
solves/second.

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/pglass/cube",
    "name": "rubik-cube",
    "maintainer": "",
    "docs_url": null,
    "requires_python": "",
    "maintainer_email": "",
    "keywords": "rubik cube solver",
    "author": "Paul Glass",
    "author_email": "pnglass@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/00/7d/1ebf605adcde336c4771188ee3f6082d52e34f968dc749013672d0c93e13/rubik-cube-0.0.2.tar.gz",
    "platform": null,
    "description": "![PyPI](https://img.shields.io/pypi/v/rubik-cube)\n![PyPI - Python Version](https://img.shields.io/pypi/pyversions/rubik-cube)\n\n# Overview\n\nThis is a Python 3 implementation of a (3x3) Rubik's Cube solver.\n\nIt contains:\n\n- A simple implementation of the cube\n- A solver that follows a fixed algorithm\n- An unintelligent solution sequence optimizer\n- A decent set of test cases\n\n## Installation\n\nThe package is hosted on PyPI.\n\n```\npip install rubik-cube\n```\n\nImport from the `rubik` package,\n\n```python\n>>> from rubik.cube import Cube\n>>> c = Cube(\"OOOOOOOOOYYYWWWGGGBBBYYYWWWGGGBBBYYYWWWGGGBBBRRRRRRRRR\")\n>>> print(c)\n    OOO\n    OOO\n    OOO\nYYY WWW GGG BBB\nYYY WWW GGG BBB\nYYY WWW GGG BBB\n    RRR\n    RRR\n    RRR\n```\n\n## Implementation\n\n### Piece\n\nThe cornerstone of this implementation is the Piece class. A Piece stores two\npieces of information:\n\n1. An integer `position` vector `(x, y, z)` where each component is in {-1, 0,\n1}:\n    - `(0, 0, 0)` is the center of the cube\n    - the positive x-axis points to the right face\n    - the positive y-axis points to the up face\n    - the positive z-axis points to the front face\n\n2. A `colors` vector `(cx, cy, cz)`, giving the color of the sticker along each\naxis. Null values are place whenever that Piece has less than three sides. For\nexample, a Piece with `colors=('Orange', None, 'Red')` is an edge piece with an\n`'Orange'` sticker facing the x-direction and a `'Red'` sticker facing the\nz-direction. The Piece doesn't know or care which direction along the x-axis\nthe `'Orange'` sticker is facing, just that it is facing in the x-direction and\nnot the y- or z- directions.\n\nUsing the combination of `position` and `color` vectors makes it easy to\nidentify any Piece by its absolute position or by its unique combination of\ncolors.\n\nA Piece provides a method `Piece.rotate(matrix)`, which accepts a (90 degree)\nrotation matrix. A matrix-vector multiplication is done to update the Piece's\n`position` vector. Then we update the `colors` vector, by swapping exactly two\nentries in the `colors` vector:\n\n- For example, a corner Piece has three stickers of different colors. After a\n  90 degree rotation of the Piece, one sticker remains facing down the same\n  axis, while the other two stickers swap axes. This corresponds to swapping the\n  positions of two entries in the Piece\u2019s `colors` vector.\n- For an edge or face piece, the argument is the same as above, although we may\n  swap around one or more null entries.\n\n### Cube\n\nThe Cube class is built on top of the Piece class. The Cube stores a list of\nPieces and provides nice methods for flipping slices of the cube, as well as\nmethods for querying the current state. (I followed standard [Rubik's Cube\nnotation](http://ruwix.com/the-rubiks-cube/notation/))\n\nBecause the Piece class encapsulates all of the rotation logic, implementing\nrotations in the Cube class is dead simple - just apply the appropriate\nrotation matrix to all Pieces involved in the rotation. An example: To\nimplement `Cube.L()` - a clockwise rotation of the left face - do the\nfollowing:\n\n1. Construct the appropriate [rotation matrix](\nhttp://en.wikipedia.org/wiki/Rotation_matrix) for a 90 degree rotation in the\n`x = -1` plane.\n2. Select all Pieces satisfying `position.x == -1`.\n3. Apply the rotation matrix to each of these Pieces.\n\nTo implement `Cube.X()` - a clockwise rotation of the entire cube around the\npositive x-axis - just apply a rotation matrix to all Pieces stored in the\nCube.\n\n### Solver\n\nThe solver implements the algorithm described\n[here](http://www.chessandpoker.com/rubiks-cube-solution.html). It is a\nlayer-by-layer solution. First the front-face (the `z = 1` plane) is solved,\nthen the middle layer (`z = 0`), and finally the back layer (`z = -1`). When\nthe solver is done, `Solver.moves` is a list representing the solution\nsequence.\n\nMy first correct-looking implementation of the solver average 252.5 moves per\nsolution sequence on 135000 randomly-generated cubes (with no failures).\nImplementing a dumb optimizer reduced the average number of moves to 192.7 on\n67000 randomly-generated cubes. The optimizer does the following:\n\n1. Eliminate full-cube rotations by \"unrotating\" the moves (Z U L D Zi becomes\nL D R)\n2. Eliminate moves followed by their inverse (R R Ri Ri is gone)\n3. Replace moves repeated three times with a single turn in the opposite\ndirection (R R R becomes Ri)\n\nThe solver is not particularly fast. On my machine (a 4.0 Ghz i7), it takes\nabout 0.06 seconds per solve on CPython, which is roughly 16.7 solves/second.\nOn PyPy, this is reduced to about 0.013 seconds per solve, or about 76\nsolves/second.\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "A basic, pure-Python Rubik's cube solver",
    "version": "0.0.2",
    "project_urls": {
        "Homepage": "https://github.com/pglass/cube"
    },
    "split_keywords": [
        "rubik",
        "cube",
        "solver"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "7a1795dda81a529aa8f2750bd4395ecf68a5582fff8bf6b87b32ee5c3de80734",
                "md5": "4c55874697ad95820a7098d5d050e6df",
                "sha256": "8693fb238e2ffb8eb8380aa81f2f5c62c45dba30e598ead54a29981b08b78e30"
            },
            "downloads": -1,
            "filename": "rubik_cube-0.0.2-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "4c55874697ad95820a7098d5d050e6df",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": null,
            "size": 14124,
            "upload_time": "2023-05-13T20:22:31",
            "upload_time_iso_8601": "2023-05-13T20:22:31.816318Z",
            "url": "https://files.pythonhosted.org/packages/7a/17/95dda81a529aa8f2750bd4395ecf68a5582fff8bf6b87b32ee5c3de80734/rubik_cube-0.0.2-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "007d1ebf605adcde336c4771188ee3f6082d52e34f968dc749013672d0c93e13",
                "md5": "d6b01100db7079f809c7b0e131642f4b",
                "sha256": "e06971e4fa1c0f6cbb97a2f12e9a9c315cb3013bf634b98eef015a40b633986f"
            },
            "downloads": -1,
            "filename": "rubik-cube-0.0.2.tar.gz",
            "has_sig": false,
            "md5_digest": "d6b01100db7079f809c7b0e131642f4b",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 19387,
            "upload_time": "2023-05-13T20:22:33",
            "upload_time_iso_8601": "2023-05-13T20:22:33.748169Z",
            "url": "https://files.pythonhosted.org/packages/00/7d/1ebf605adcde336c4771188ee3f6082d52e34f968dc749013672d0c93e13/rubik-cube-0.0.2.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-05-13 20:22:33",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "pglass",
    "github_project": "cube",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "tox": true,
    "lcname": "rubik-cube"
}
        
Elapsed time: 0.16446s