[PYTHON] How to use the Rubik's Cube solver library "kociemba"

Introduction

This article introduces the library kociemba that calculates the solution of the Rubik's cube and explains how to use it easily.

There are various libraries in python, from libraries for drawing 3D graphs to mahjong libraries. Introducing this time is a library that instantly calculates the solution method for aligning the entire surface from the current state of the Rubik's cube.

I wrote this article so that those who are looking for a Rubik's Cube solver in university classes and free study during summer vacation can find kociemba immediately.

Example of work using kociemba

Thanks to this library, I was able to create a "robot that solves the Rubik's cube fully automatically". Video: Automatic Rubik's Cube Solver

Screenshot from 2020-07-05 22-13-16.png

What is kociemba?

Originally, it is the name of the scholar who invented the Kociemba's algorithm, which solves the Rubik's cube. His real name is Herbert Kociemba.

I was looking for a library to solve the Rubik's Cube for my department's production tasks. Actually, there are many other libraries for solving the Rubik's Cube, but many of them are extremely slow or I have no idea how to use them, so I had a hard time not knowing which one to use at that time. Meanwhile, I decided to use this library because it was the easiest to use and the calculation was quickest I tried.

Installation method

My environment was Ubuntu 16.04 LTS. You can enter it with pip. It seems that it has been confirmed with Python2.7, Python3.3 +

pip install kociemba

Those who use Unix-based OS such as mac and ubuntu also need the following command.

sudo apt-get install libffi-dev

How to use

Use it as follows. The meaning of the symbols will be explained later.

python


>>> import kociemba

>>> kociemba.solve('DRLUUBFBRBLURRLRUBLRDDFDLFUFUFFDBRDUBRUFLLFDDBFLUBLRBD')
u"D2 R' D' F2 B D R2 D2 R' F2 D' F2 U' B2 L2 U2 D R2 U"

Rubik's Cube Operation Notation

The axis is immovable

It is common sense for those who can make a Rubik's cube, but the axis of the Rubik's cube does not change no matter how many screws are turned. In the image below, you can see that the square in the center of each surface does not move no matter how you turn the surface.

Screenshot from 2020-07-05 22-39-13.png

By using this fact, we can see that even the complicated "Rubik's cube state" can be represented by a symbol that is uniquely determined.

The name of the face is represented by the color of the central cell

Since it was found that the center cell of each surface does not move, it is now possible to specify the surface like "turn the surface whose center is red" no matter how disjointed it is.

The rotation angle is also represented by a symbol

In kociemba, the rotation angle is expressed as follows. R is the Red side,

R:90 degrees clockwise
R':90 degrees counterclockwise
R2:180 degree rotation

How to input to the solver

kociemba has each face instead of color (F (front), B (back), R (right), L (left), U (up), B (bottom)) so that it can be solved in any direction. It is represented by. Therefore, first, determine the correspondence between these symbols and the color of your Rubik's cube as follows. Naturally, the correspondence is decided by the color of the center of the surface facing the front.

--F (front) → white --B (back) → yellow

Next, we will investigate which color is in which position based on the following development drawing (this is extremely difficult).

Screenshot from 2020-07-05 23-06-07.png

For example, if there is yellow at the top of ʻU1, then ʻU1 = B. Finally, ʻU1, U2, U3, U4, U5, U6, U7, U8, U9, R1, R2, R3, R4, R5, R6, R7, R8, R9, F1, F2, F3, F4, F5, F6, F7, F8, F9, D1, D2, D3, D4, D5, D6, D7, D8, D9, L1, L2, L3, L4, L5, L6, L7, L8, L9, B1, B2, B3, Create the string str in the order of B4, B5, B6, B7, B8, B9. and execute kociemba.solve (str) `to get the solution.

reference

kociemba - GitHub

Recommended Posts

How to use the Rubik's Cube solver library "kociemba"
How to use the C library in Python
How to use the graph drawing library Bokeh
How to use the generator
How to use the decorator
[Python] How to use the graph creation library Altair
How to use the zip function
How to use the optparse module
How to use Requests (Python Library)
[Python] How to import the library
How to use the ConfigParser module
[python] How to use the library Matplotlib for drawing graphs
Notes on how to use marshmallow in the schema library
How to use the Spark ML pipeline
[Linux] How to use the echo command
How to use the Linux grep command
How to use the IPython debugger (ipdb)
How to use the library "torchdiffeq" that implements Neural ODE's ODE Block
How to use the Google Cloud Translation API
How to use the NHK program guide API
How to use PyTorch-based image processing library "Kornia"
How to use xml.etree.ElementTree
How to use Python-shell
How to use tf.data
How to use Seaboan
How to use image-match
How to use shogun
How to use Pandas 2
How to use Virtualenv
How to use numpy.vectorize
How to use pytest_report_header
How to use partial
How to use Bio.Phylo
How to use x-means
How to use WikiExtractor.py
How to use IPython
How to use virtualenv
How to use Matplotlib
How to use iptables
How to use TokyoTechFes2015
How to use dictionary {}
How to use Pyenv
How to use list []
How to use python-kabusapi
How to use OptParse
How to use return
How to use dotenv
How to use pyenv-virtualenv
How to use Go.mod
How to use imutils
How to use import
How to use the Raspberry Pi relay module Python
I wanted to use the Python library from MATLAB
Linux user addition, how to use the useradd command
How to use the exists clause in Django's queryset
[Introduction to Udemy Python3 + Application] 30. How to use the set
How to use argparse and the difference between optparse
How to read the CBC (Pulp, python-mip) solver log
How to use the model learned in Lobe in Python
(Remember quickly) How to use the LINUX command line
How to use Qt Designer