Cube Solver

deepSubDiver

Regular
Joined
Jun 17, 2009
Messages
161
Location
Germany
I am currently writing a tool to generate scrambles and give optimal cross solutions.
I bruteforce all possibilities (BFS) to find the shortest solution, taking ages to compute.
What I am asking for, are there any ideas how I could speed it up?

Below an excerpt of the code.
Code:
def get_moves(seq):
    ret = []
    for m in ["F ", "F2 ", "F' ", "B ", "B2 ", "B' ", "R ", "R2 ", "R' ", "L ", "L2 ", "L' ", "U ", "U2 ", "U' ", "D ", "D2 ", "D' "]:
        if len(seq) > 0:
            if m[0] == seq[:-1].rsplit(" ")[-1][0]:
                continue
        yield m

def seqlen(seq): return len(seq.rsplit(" ")) - 1

def brute(cube, mdepth = 8):
    depth = 0
    q = Queue()
    q.put("")
    while not q.empty():
        seq = q.get()
        if seqlen(seq) > depth:
            depth = seqlen(seq)
            print "depth: %d (qsize: %d)" % (depth, q.qsize())
        cube.apply_sequence(seq)
        if cube.cross_solved(): return seq
        cube.undo()
        if seqlen(seq) == mdepth: continue
        for m in get_moves(seq):
            q.put(seq + m)
 
If you're only looking for optimal cross solutions, you can generate one large table with all configurations for the 4/all edges and their solutions.

btw. i don't get your get_moves method. Couldn't you just use
seq.split(" ").each{|m| yield m} ?
 
get_moves does not yield moves that don't make sense, for example for a given sequence ending with F, get_moves will not yield any moves on the F-layer, since double turns and ccw-turns are already part of the possible moves.

The idea is not to get a list with all solutions for each edge configuration. It is, for me, to train the cross solutions, on a fully scrambled cube.
 
The idea is not to get a list with all solutions for each edge configuration. It is, for me, to train the cross solutions, on a fully scrambled cube.

So, why does a precomputed table doesn't work then? You just look for configuration x in the table and you'll get the solution. Scramble -> leads to configuration x on cube -> search for x in table.
A typical table entry could look like this:
[1, 4, 3, 11] => "F U' F2"
which means: cross piece 1 is at position 1, cross piece 2 is at position 4, ... and the optimal solution is F U' F2. (I didn't take orientation into account)
 
I dont intimately understand what your code is doing/how it is working since Ive never used whatever language it is written in, so this may or may not be a useful suggestion.

Rather than straightup bruteforce, try a depth-limited type search; starting at (4 - solved crosspieces) moves, first check all possible sets of 4 moves, being sure to ignore the 2 counterparts to the last move you did (IE if you just did R2 ignore R and R' as possibilities); if you find no solution in the 4-move set, try 5; if you find a solution you know that maxlength = 5, otherwise try 6 and repeat up to 8. While this uses absolutely no cube theory or heuristic, it should end up faster since it is a somewhat "guided" bruteforce.
 
This is Python ;)

This is exactly what it does, a breadth first search goes into breadth instead of depth ;) starting with 0-move solutions up to 8-move solutions, it tries all possible move sequences (that somehow make sense, get_moves does what you were suggesting with ignoring certain moves).

I think about implementing the Kociemba algorithm to Python, is it worth the work?
 
I think about implementing the Kociemba algorithm to Python, is it worth the work?
If you're writing just a cross solver then using Kociemba makes no sense. If you're writing an efficient full cube solver then Kociemba is an excellent choice but using Python makes no sense...

Jaap has everything you need to know here: http://www.jaapsch.net/puzzles/compcube.htm. Generating a table (God's algorithm) like tim said is the way to go. I wouldn't store the solutions, though - just the distance to solved. Uses much less memory and solutions can be generated really quickly when needed.

In general, I'd say that depth first with iterative deepening is much better than breadth first for a puzzle solver. The branching factor is usually big enough so that the repeated computations are insignificant, and the difference in memory usage is huge.
 
Last edited:
Jaaps site is a great resource for my project, I will definitely have a deeper look into that.
You say you would use a table - isn't that, as you describe it, more of a pruning table instead of a solution table?
For cross solutions I experienced a BFS to be more efficient than DFS. I can't tell anything about more complex problems, though.
 
You say you would use a table - isn't that, as you describe it, more of a pruning table instead of a solution table?
Yeah, it's exactly like a pruning table. Jaap only uses that word for smaller tables, though, but the idea is the same.

For cross solutions I experienced a BFS to be more efficient than DFS.
Hmm that's interesting. Would be interesting to see the code.
 
Back
Top