Skip to content

Heuristic Function takes too long to compute #4

@TalhaGoktopal

Description

@TalhaGoktopal

I tried to implement the cube solver. Everything works but the heuristic function is taking to long to create the heuristic database for a depth of 20. I was only able to generate a database with depth 7. I am doing this project for my A Level Computer Science coursework so I don't have much coding experience. Would you be able to tell me what I'm doing wrong? I would really appreciate it.

`def buildHeuristic(state, actions, maxMoves= None, heuristic=None, Heuristic_Mode = None):
#state - initial state of cube (solved cube)
#actions - list of possible actions
#maxMoves - maximum number of moves that can be carried of to find solution
#heuristic - dictionary holding heuristic values for cube state

#Checks if a heuristic dictionary is given
if heuristic is None:
    heuristic = {state: 0}

#que constains state and current distance
que = [(state, 0)]
visited = set()
visited.add(state)

#Calculates the toatl number of cube states
nodeCount = sum([len(actions) ** (x + 1) for x in range(maxMoves + 1)])

with tqdm(total=nodeCount, desc = 'Heuristic') as progressBar:
    while True:
        if not que:
            break
        currentState, distance = que.pop()
        #Checks if the distance is greater then max moves to prevent excessive exploration
        if distance > maxMoves:
            continue
    
        #Applies all the posssible moves to the current state of the cube
        for m in actions:
            
        #Selects cube based on mode
            if Heuristic_Mode == "2x2":
                cube = miniRubiksCube(miniState = currentState)
            else:
                cube = RubiksCube(state = currentState)
            
            match m:
                case "R":
                    cube.R()
                case "L":
                    cube.L()
                case "U":
                    cube.U()
                case "D":
                    cube.D()
                case "F":
                    cube.F()
                case "B":
                    cube.B()
                case "R'":
                    cube.xR()
                case "L'":
                    cube.xL()
                case "U'":
                    cube.xU()
                case "D'":
                    cube.xD()
                case "F'":
                    cube.xF()
                case "B'":
                    cube.xB()

            #Stores new state as mStr 
            mStr = cube.cubeString()
            #If the new state has not been visited or the new distance if less then the origianl distance for that state
            if mStr not in heuristic or heuristic[mStr] > distance + 1:
                #Sets new distance
                heuristic[mStr] = distance + 1
            #Adds the new state and new distance to the que
            que.append((mStr, distance + 1))
            progressBar.update(1)

return heuristic

cube = RubiksCube()
actions = ["R", "L", "U", "D", "F", "B", "R'", "L'", "U'", "D'", "F'", "B'"]

heuristicDatabase = buildHeuristic(cube.cubeString(), actions, maxMoves = 20, heuristic = None)
#Creates heurisitc databse if it does not exist
with open("3x3Heuristic.json", 'w', encoding='utf-8') as file:
json.dump( heuristicDatabase , file , ensure_ascii = False , indent = 4)`

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions