News for hackers. Stuff that matters.
Re:The Mathematics of the Rubik’s Cube (2009) [pdf] by perfect_wave on Monday February 11, 2019 @03:03PM (#19137740)
I've been fascinated by the Rubik's cube for years after learning to solve one in 7th grade. I started to get back into it high school and dropped my solve times down below a minute. Then I continued in college and got down to 25 seconds average with a personal best of 15.15.

I created a 2x2 Rubik's cube in Java as my freshman final project for my second computer science class, but it was quite ugly.

Recently, I decided to take another look at implementing the cube - this time using Python. I also wanted a way to generate cubes and check if they were valid. The main representation of the cube is as a permutation group - a 48-tuple where each element in the tuple is unique. The solved cube is represented as (0, 1, ... , 46, 47).

Turns of the cube simply permute this tuple. To figure these all out I spent a lot of time with a cube covered in post it notes.

All of the stuff I implemented in the checking for valid cube comes from this stackoverflow post: https://math.stackexchange.com/questions/127577/how-to-tell-...

I've had a lot of fun working with this cube as my personal project. I recently created a Django website and a RESTful API to display randomly generated cubes. Soon I'll be working on a data pipeline to process these random cubes and display them nicely.

I've looked into working on a solution algorithm to implement Thistlethwaite's algorithm - https://www.jaapsch.net/puzzles/thistle.htm. I think this will take me a while, but it should be doable. I haven't really though too much about implementing it, but I think the way to do it is to define what it is for each step to be completed and then BFS a graph of moves to end up in that state. If anyone has looked into this I'd love some advice!

You can find the code I've written on Github: https://github.com/elliotmartin/RubikLite/blob/master/Rubik....

Re:The Mathematics of the Rubik’s Cube (2009) [pdf] by Retric on Monday February 11, 2019 @04:08PM (#19138371)
My only suggestion is trying to quickly validate a cube is solvable seems like a waste of time vs generating them via a large number of random rotations. Especially when no cube can be more than 20 rotations from a solution.
Re:The Mathematics of the Rubik’s Cube (2009) [pdf] by nsilvestri on Monday February 11, 2019 @04:55PM (#19138749)
Checking whether a cube is valid is effectively a random-state generator; i.e. it randomly chooses one of the 43 quintillion possible states. Generating random moves biases towards certain scrambles. This is why the World Cube Association generates random states of the cube with their library TNoodle, rather than random sequences of moves.
Re:The Mathematics of the Rubik’s Cube (2009) [pdf] by Retric on Monday February 11, 2019 @06:28PM (#19139566)
Citation needed on that.

It’s trivial to do 10k random rotations in memory. At which point you have something indistinguishable from random.

Re:The Mathematics of the Rubik’s Cube (2009) [pdf] by Someone on Monday February 11, 2019 @07:47PM (#19140006)
Depends on what you call a ‘rotation’. After 10k random 90 degree turns of a face, you have an even permutation on both corners and edges, which means you can’t reach half the positions.

That’s easily corrected (e.g: flip a coin first to determine whether to do 10k or 10k+1 random rotations), but this shows it is easy to make slip ups solving such a seemingly trivial problem.

Re:The Mathematics of the Rubik’s Cube (2009) [pdf] by Retric on Tuesday February 12, 2019 @12:37AM (#19141315)
What you’re describing falls under rotational symmetry.

Rotating top side left one is the same as rotating bottom 2 sides right.

But, more generally limiting rotations to 90 degrees slows things down as 90 or 180 degree rotations can be directly computed with the same overhead.

Re:The Mathematics of the Rubik’s Cube (2009) [pdf] by mturmon on Monday February 11, 2019 @11:46PM (#19141116)
You have been down voted, but I think this is a reasonable point and a reasonable question. It seems to me that the method of iterated transformation you suggest will indeed yield a random draw.

My analogy is to Markov chain Monte Carlo. It is well known that MCMC on a discrete space will converge to the desired distribution (we’ll get to that in a second) if the chain is irreducible and aperiodic.

Irreducible means any state can eventually transition to any other state using the permutations allowed. This is easy to validate if we just use the various face rotations. Aperiodic essentially means that there is no fixed “loop” from one state back to itself that occurs with probability one. (The even parity issue that @someone raised comes in here. An absolute fix in the lack of full understanding of all possible such issues is to allow null moves with some low probability.). For a simple state space like this, the main thing is to make the permutations chosen in such a way that all states are reachable.

OK, iterated permutations converge in distribution, but to what? All the states in the target distribution here have equal probability, so (in MCMC terms) the posterior is totally flat:

  P(s) / P(s’) = 1
for all states s, s’. This means that a reversible Metropolis-Hastings sampler that just accepts every proposed move will converge to a flat posterior. OK, so we just need to make the permutations we propose reversible.

What is reversible? It means that, if you are at state s, you are just as likely to go to s’ as if you are at s’ and going to s. [1] The easy way to ensure this is to make “clockwise twist of left face” chosen with the same probability as “counterclockwise twist of left face”.

This is the obvious strategy anyway, which is why reversible samplers are popular.

So in sum, the recommended algorithm is to start from any legal state and perform “N” permutations randomly chosen from clockwise/counterclockwise (50/50 chance) rotations of a randomly chosen face. To accomplish the null moves, just subtract a small random number from N.

On a discrete state space, you will get exponentially fast convergence to the desired distribution. The space is strongly connected, because you can get from any state to any other state with at most 40 moves.

I don’t see the problem here.

[1] https://en.m.wikipedia.org/wiki/Metropolis–Hastings_algorith..., A(x, x’) = 1.

Re:The Mathematics of the Rubik’s Cube (2009) [pdf] by seaneganx on Monday February 11, 2019 @07:16PM (#19139827)
https://www.worldcubeassociation.org/regulations/#4b3

The World Cube Association requires "equal probability for each state" by scramble programs used for their events.

You're right that a large number of rotations will result in something indistinguishable from random by an individual, but that is not sufficient according to WCA regulations as it's not uniformly random.

Re:The Mathematics of the Rubik’s Cube (2009) [pdf] by Retric on Monday February 11, 2019 @07:28PM (#19139903)
That’s not supporting your point, a solution that’s harder to mess up is suggested. Secondly, they specifically don’t want a truely ranodm sequence as they require a minimum number of moves from a solution.

If I can hand you multiple sets of 10^10 cubes and you can’t tell which method was used that’s what I mean from indistinguishable not simply humans can’t tell.

Re:The Mathematics of the Rubik’s Cube (2009) [pdf] by perfect_wave on Monday February 11, 2019 @06:56PM (#19139731)
Except that simply isn't true. When randomly performing rotations you're much more likely to end up in certain states (as I mentioned in my other comment). Whether you make 10k random rotations or 20 random rotations you're still more likely to end up in a certain state.
Re:The Mathematics of the Rubik’s Cube (2009) [pdf] by Retric on Monday February 11, 2019 @07:33PM (#19139928)
In theory I agree with you, in practice I don’t.

Sum 20 coin flips mod 3 and you can find the bias in the output. It’s hard with a small sample set, but you could do it. Sum 10k coin flips and it’s a different situation.

Eventually you fall through the noise floor for any reasonable sample size.

Re:The Mathematics of the Rubik’s Cube (2009) [pdf] by bunderbunder on Monday February 11, 2019 @07:47PM (#19140002)
It's almost certainly less effort (though arguably also less fun) to write a valid state checker than it is to prove that N random cube operations will yield something that's within some tolerance of a uniform distribution across all legal cube states.
Re:The Mathematics of the Rubik’s Cube (2009) [pdf] by perfect_wave on Monday February 11, 2019 @05:53PM (#19139291)
Some positions are far, far more common than others. So generating a cube by a random string of rotations biases towards these positions. Take a look at the count of positions table in the bottom right corner of this page: http://www.cube20.org/

Plus figuring out how to implement the various ways of checking validity was a lot of fun.

Re:The Mathematics of the Rubik’s Cube (2009) [pdf] by Someone on Monday February 11, 2019 @07:57PM (#19140063)
Firstly, that table doesn’t show the number of essentially different positions. For example, at distance 2, you have (54, by my count) positions where two faces were turned that do not share an edge and positions where they do.

Secondly, I don’t see why would want to pick a ‘random’ position that way. If those numbers were correct, it would mean a 1:21 probability that your ‘random’ position was zero moves from the solution.

Re:The Mathematics of the Rubik’s Cube (2009) [pdf] by Retric on Monday February 11, 2019 @06:31PM (#19139577)
That’s a different problem. If you want to check every possibly you don’t want a randomized cube. You want the full list.
Re:The Mathematics of the Rubik’s Cube (2009) [pdf] by perfect_wave on Monday February 11, 2019 @06:54PM (#19139721)
I don't think generating every possibility of a cube is reasonably doable - it's over 43 quintillion - 10^16

Edit: I think you simply didn't understand what I was talking about. The distribution of states is not even. If you randomly perform moves on a cube then you're more likely to end up in certain states.

Re:The Mathematics of the Rubik’s Cube (2009) [pdf] by Retric on Monday February 11, 2019 @07:23PM (#19139877)
For scale, fastest supercomputer is over 1.8 x 10^17 operations a second. So generating 10^16 numbers is not that bad for a distributed project over a few weeks.

As to the distribution of states, I am not sure what you mean. Insufficient shuffling can introduce bias, but that gets reduced as you continue shuffling. You can trivially shuffle past the point where remaining bias is not detectable. Unless, you want a specific bias, then that’s more easily achieved generating a random number with a specific format.