简体   繁体   English

广度优先搜索无限循环

[英]Breadth First Search Infinite Loop

I'm creating the 8 puzzle AI game in bfs and it always ends up in an infinite loop, i'm using a queue to store the explored (not yet visited nodes), and a list to store the explored and visited nodes to avoid visiting the same state multiple times, also im using getNextStates() function to get all of the possible next states depending on the position of "0", switch() is responsible for creating the next state, my code:我在 bfs 中创建了 8 个拼图 AI 游戏,它总是以无限循环结束,我使用队列来存储已探索(尚未访问的节点),并使用列表来存储已探索和访问的节点以避免visiting the same state multiple times, also im using getNextStates() function to get all of the possible next states depending on the position of "0", switch() is responsible for creating the next state, my code:

goalState=[0, 1, 2, 3, 4, 5, 6, 7, 8]
def switch(list,index1,index2):
    newList=[]
    for i in range(len(list)):
        newList.append(list[i])

    temp=newList[index1]
    newList[index1]=newList[index2]
    newList[index2]=temp
    return newList

def getNextStates(state):
    nextStates=[]
    length=len(state)
    emptyTile=0
    for i in range(length):
        if state[i]==0:
            emptyTile=i
            #    1
            # 0  3
    print('empty tile in position : ' , emptyTile)
    if emptyTile==0:
        nextStates.append(switch(state,0,1))
        nextStates.append(switch(state, 0, 3))
    elif emptyTile==1:
        nextStates.append(switch(state, 1, 0))
        nextStates.append(switch(state, 1, 4))
        nextStates.append(switch(state, 1, 2))
    elif emptyTile==2:
        nextStates.append(switch(state, 2, 1))
        nextStates.append(switch(state, 2, 5))
    elif emptyTile==3:
        nextStates.append(switch(state, 3, 0))
        nextStates.append(switch(state, 3, 4))
        nextStates.append(switch(state, 3, 6))
    elif emptyTile==4:
        nextStates.append(switch(state, 4, 3))
        nextStates.append(switch(state, 4, 1))
        nextStates.append(switch(state, 4, 5))
        nextStates.append(switch(state, 4, 7))
    elif emptyTile==5:
        nextStates.append(switch(state, 5, 2))
        nextStates.append(switch(state, 5, 4))
        nextStates.append(switch(state, 5, 8))
    elif emptyTile==6:
        nextStates.append(switch(state, 6, 3))
        nextStates.append(switch(state, 6, 7))
    elif emptyTile==7:
        nextStates.append(switch(state, 7, 6))
        nextStates.append(switch(state, 7, 4))
        nextStates.append(switch(state, 7, 8))
    else:
        nextStates.append(switch(state, 8, 7))
        nextStates.append(switch(state, 8, 5))

    return nextStates


def breadthFirst(initialState,goal):
    global exploredCount , visitedCount
    exploredCount = 1
    visitedCount = 0
    frontier = []
    frontier.append(initialState)
    explored=[]
    print("Staring Dequing....")
    while len(frontier) > 0:
        print(len(frontier))
        state=frontier.pop(0)
        print("dequed : " , state)
        explored.append(state)
        print("appended in explored and visitedCount incremented")
        visitedCount += 1
        if state==goal:
            print("State Is Accomplished")
            return state
        nextStates=getNextStates(state)
        print("possible Next States : " , nextStates)
        for i in range(len(nextStates)):
            print('Checking Child states , current : ' , nextStates[i])
            if  not nextStates[i] in explored:
                if not nextStates[i] in frontier:
                    print("not in visited or explored , enqueue")
                    frontier.append(nextStates[i])
                    exploredCount += 1
 
    return initialState

The issue was that there are initial states which are unsolvable, if this is the case the algorithm will keep exploring in an infinite loop.问题是存在无法解决的初始状态,如果是这种情况,算法将在无限循环中继续探索。 The solution was to count the number of inversions (empty tile not included) and if its odd then its unsolvable, if its even then its solvable, this resource helped me understand: https://www.cs.princeton.edu/courses/archive/fall12/cos226/assignments/8puzzle.html解决方案是计算反转的数量(不包括空瓷砖),如果它是奇数,那么它是不可解的,如果它是偶数那么它是可解的,这个资源帮助我理解: https://www.cs.princeton.edu/courses/存档/fall12/cos226/assignments/8puzzle.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM