Understanding of The Rubik’s Cube and Solving Using AI

VIDITA DAHIYA
6 min readDec 27, 2020

The Rubik’s Cube is one of those toys that just won’t go away. Solving it is either something very easy that you can do in minutes to impress, or find it is very difficult to solve and you fill it is so hard and you end up using it as a paperweight. 😅

So in this blog we are going to learn about how to solve a Rubik’s cube using Artificial Intelligence algorithm.

Source: https://d2t1xqejof9utc.cloudfront.net/screenshots/pics/6c855a4a0ef58f299cdee3ed78520445/large.gif

Let’s Understand this with some history about the Rubik’s cube.

Hungarian design teacher and serious puzzler Erno Rubik assembled his first cube puzzle in 1974 and called it the Magic Cube. After a toy agent pitched the puzzle to Ideal Toy & Novelty Company, it renamed the puzzle Rubik’s Cube and began putting it in stores in 1980.

Though media first circulated a story about Rubik designing the cube to help teach students about three-dimensional objects, Rubik himself later acknowledged that he purposefully set out to design a puzzle based on geometry.

tiny cubes called “cubies” produced a truly challenging puzzle.

How to Solve it?

There are several algorithms for solving the classic cube, which has a whopping 43,252,003,274,489,856,000 — about 43 quintillion — possible combinations. One state-of-the-art approach has shown the puzzle can be defeated in 20 or fewer moves, no matter the combination of the colored squares. This figure is dubbed God’s Number.

To Solve cube of 0 → 11 faced cubes

– Each face may be rotated either clockwise/counter-clockwise.

– -Examples-

– 0: Rotate face 0 clockwise

– 1: Rotate face 0 counter-clockwise

– 2: Rotate face 1 clockwise

To Solve cube of 12+ faced cubes

If the Cube is larger than 2x2x2, each inner column may be rotated upwards/downwards

And each inner row may be rotated leftwards/rightwards.

So this are some tips/tricks regarding how to solve Rubik’s cube.

Now let’s understand the main AI algorithm

In this we are using CNN[ Convolution Neural Network] algorithm to solve the Rubik’s cube.

Source: image

Let’s try to understand with simple Original cube and Scrambled cube to understand the process.

We will first see how the original cube will look with our python code.

Image of Original Cube

Now we will see the scrambled cube image:

Image of Scrambled cube

To solve this automatically we have made a many functions that are:

  1. rotate() → To rotate a section of a cube appropriately
  2. rotate_face() → Rotate a single face 90 degrees in the given direction (side edges not accounted for).
  3. rotate_edges() → Rotate a face’s adjacent edges 90 degrees in the given direction (face not accounted for).
  4. rotate_middle() → Rotate an inner row/column of the Cube. Rotations are taken with face 0 as the front.
  5. scramble() → Rotate the Cube randomly the specified number of times (iterations).
  6. random_rotation() → Take a random rotation action.

So this are the main core function that we are going to use while solving Rubik’s cube automatically.

So from the above figure we can see that first there is a simple/original cube was there but when we have to scrambled it we need to use the scrambling function by using that function we can able to get our cube scrambled.

This scrambling function which we are using to scrambled cube is purely random based so there is no any fix scrambled is there. for that we are using random_rotation() function.

Now we have seen the main functions now we can start to understand how our cube will get solved automatically using AI algorithm.

Steps to solve the cube:

Step:1

First step is there to generate a small cubes of each side, to generate that we can generate that through the code given below.

code snippet:

cube1 = make_small_cube(color.blue, color.red, vector(0.5,0.5,0.5), vector(0.5,0.5,0.5), color.white, vector(0.5,0.5,0.5))

Step:2

As we have generate a small cube from every cube then now we need to make different type of function to rotate. Without making that function we can not able to solve the scrambled cube. So we are going to make a function to rotate it according to clockwise.

To make it possible we have to write the code as given below to rotate a cube.

Code snippet:

def front_turn_clock(self):

#print(“front rotation clock”)

def orientation_changer(self):

if self.orientation1 == vector(0,1,0):

self.orientation1 = vector(1,0,0)

elif self.orientation1 == vector(1,0,0):

self.orientation1 = vector(0,-1,0)

elif self.orientation1 == vector(0,-1,0):

self.orientation1 = vector(-1,0,0)

elif self.orientation1 == vector(-1,0,0):

self.orientation1 = vector(0,1,0)

Step:3

We have made a cube rotate function to rotate the cube but as we want to get the solved cube automatically so for now we have to made that function through we can solve the Rubik’s cube.

To make a self rotate function we have to write a code as given below. So in this function it will rotate the cube automatically and it will stop when it comes at a particular position.

Code Snippet:

if self.position == vector(0,2,0):

self.position = vector(2,2,0)

self.name.rotate(angle=-1.57079633, axis=vector(0,0,1), origin=vector(0,0,0))

self.name.pos = self.position

orientation_changer(self)

elif self.position == vector(2,0,0):

self.position = vector(0,0,0)

self.name.rotate(angle=-1.57079633, axis=vector(0,0,1), origin=vector(0,0,0))

self.name.pos = self.position

orientation_changer(self)

As we have made a rotate function to rotate a cube Now we have to follow the same function for six different sides that are:

  1. Front
  2. Top
  3. Back
  4. Bottom
  5. Right
  6. Left

We will make same six function for listed side to solve the cube.

Step:4

We have made all function to rotate a cube but we have to set the default position. So from that position our cube get start the cube to solve cube. To set default position we need to write a code as given below.

Code Snippet:

v1 = vector(0,2,0)

v2 = vector(1,2,0)

v3 = vector(2,2,0)

v4 = vector(0,1,0)

v5 = vector(1,1,0)

v6 = vector(2,1,0)

v7 = vector(0,0,0)

v8 = vector(1,0,0)

Step:5

In this last second step we are going to det the clock counter for all sides of the cube. So accordingly the clock counter it will going to rotate the cube.

The code for to set the clock counter is given below.

Code Snippet:

def front_rotation_clock():

for i in range(len(clocks)):

clocks[i].front_turn_clock()

#time.sleep(time1)

def front_rotation_counter():

for i in range(len(clocks)):

clocks[i].front_turn_counter()

#time.sleep(time1)

Step:6

To solve the cube this is the last step / final step to solve the cube. In this last function we have give rotation function of a cube according to position / or a position wise rotation we have given. To do position wise rotation we need to write a code as given in a code snippet.

Code Snippet:

if pos == vector(0,2,-1):

front_rotation_clock()

back_rotation_counter()

left_rotation_clock()

front_rotation_counter()

back_rotation_clock()

if pos == vector(2,2,-1):

front_rotation_counter()

back_rotation_clock()

right_rotation_clock()

front_rotation_clock()

back_rotation_counter()

So after doing this 6 steps we will get our cube solved. Let’s checked the output of our cube.

Result of our code

So at last we have get our output/ we have got out scrambled cube solved.🎉🎊

Now we will take a look at logs for getting information like after how many step or iteration we have got our output.

Logs

In the above images we can able to watch the output and logs . So we can say that by using our this algorithm we can able to solve the Rubik’s cube. Further I will going to add 3D feature to solve the Rubik’s cube.

So this was all about my project.

You may also like to watch my project video for detailed explanation to this project.

Thank You for reading this blog.

If you have any query feel free to contact me on LinkedIn.

Vidita Dahiya: https://www.linkedin.com/in/vidita-dahiya-/

--

--