簡體   English   中英

在python中使用遞歸在dfs中超過了最大遞歸深度

[英]Maximum recursion depth exceeded in dfs using recursion in python

我編寫了一個python代碼,以在python中使用遞歸dfs解決傳教士和食人族的問題。 但是我不斷收到此錯誤:RecursionError:超過最大遞歸深度

我不知道該怎么辦,我已經堅持了很長時間。 任何幫助或建議對我來說都會挽救生命。 謝謝。 這是代碼:

class State(object):
#left = 1
#right = 0 for boat
        def __init__(self, missionaries, cannibals, boat):
    self.missionaries = missionaries
    self.cannibals = cannibals
    self.boat = boat

#def __str__(self):
#    return "%s, %s %s %s" % (self.by_move, self.missionaries, self.cannibals, self.boat)

def is_valid(self):
    if self.missionaries < 0 or self.missionaries > 3:
        return False
    if self.cannibals < 0 or self.cannibals > 3:
        return False
    if self.boat > 1 or self.boat < 0:
        return False
    if self.missionaries < self.cannibals and self.missionaries > 0:
        return False
    # Check for the other side
    if self.missionaries > self.cannibals and self.missionaries < 3:
        return False

    return True

def is_goal(self):
    return self.missionaries == 0 and self.cannibals == 0 and self.boat == 0

def new_states(self):
    op = -1  # Subtract
    boat_move = "from left shore to right"
    if self.boat == 0:
        op = 1  # Add
        boat_move = "from right shore to left"

    for x in range(3):
        for y in range(3):
            by_move = "Move %s missionaries and %s cannibals %s" % (x, y, boat_move)
            new_state = State(self.missionaries + op * x, self.cannibals + op * y, self.boat + op * 1)
            if x + y >= 1 and x + y <= 2 and new_state.is_valid():
                yield new_state

class Node(object):
def __init__(self, parent, state, depth):
    self.parent = parent
    self.state = state
    self.depth = depth

def children(self):
    for state in self.state.new_states():
        yield Node(parent=self, state=state, depth=self.depth + 1)

def extract_solution(self):
    print
    "Extracting soln"
    solution = []
    node = self
    solution.append(node)
    while node.parent is not None:
        solution.append(node.parent)
        node = node.parent
    solution.reverse()
    return solution

def dfs(root,visited,sol = None):
if root in visited:
    return
if root is None:
    return
visited.append(root)
if root.state.is_goal():
    sol = root
    return

for child in root.children():
    if child not in visited:
        dfs(child,visited,sol)

def main():
    initial_state = State(3,3,1)
    root = Node(parent = None, state = initial_state,depth = 0)
    visited = []
    sol = Node(parent = None, state = initial_state,depth = 0)
    dfs(root,visited,sol)
    ans = sol.extract_solution()
    print(ans)


if __name__ == '__main__':
    main()

有兩個問題:

1:您的“已訪問”列表未正確跟蹤所有狀態。 可以通過訪問全局變量來輕松解決此問題(如最終解決方案中所述,將其放在def main()前面)

2:程序正在尋找永遠無法幫助的可能性(例如:來回帶同一個家伙),這

if root in visited:
    return
if root is None:
    return

不能解決這個問題,因為它永遠不會是同一個根對象(即使root.state.missionaries,cannibals和boat是相同的值),因此我使用了一個字典對象來更改它:

if root is None:
    return
state = str(root.state.missionaries) + ',' + str(root.state.cannibals) + ',' + str(root.state.boat)
if state in routes:
    if routes[state] < root.depth:
        return
    else:
        routes[state] = root.depth
else:
    routes[state] = root.depth
visited.append(root)

這將導致以下代碼(它返回一個答案,我不確定這是正確的答案,因為我不知道傳教士和食人族的問題)

class State(object):
#left = 1
#right = 0 for boat
    def __init__(self, missionaries, cannibals, boat):
        self.missionaries = missionaries
        self.cannibals = cannibals
        self.boat = boat

    #def __str__(self):
    #    return "%s, %s %s %s" % (self.by_move, self.missionaries, self.cannibals, self.boat)

    def is_valid(self):
        if self.missionaries < 0 or self.missionaries > 3:
            return False
        if self.cannibals < 0 or self.cannibals > 3:
            return False
        if self.boat > 1 or self.boat < 0:
            return False
        if self.missionaries < self.cannibals and self.missionaries > 0:
            return False
        # Check for the other side
        if self.missionaries > self.cannibals and self.missionaries < 3:
            return False

        return True

    def is_goal(self):
        return self.missionaries == 0 and self.cannibals == 0 and self.boat == 0

    def new_states(self):
        op = -1  # Subtract
        boat_move = "from left shore to right"
        if self.boat == 0:
            op = 1  # Add
            boat_move = "from right shore to left"

        for x in range(3):
            for y in range(3):
                by_move = "Move %s missionaries and %s cannibals %s" % (x, y, boat_move)
                new_state = State(self.missionaries + op * x, self.cannibals + op * y, self.boat + op * 1)
                if x + y >= 1 and x + y <= 2 and new_state.is_valid():
                    yield new_state

class Node(object):
    def __init__(self, parent, state, depth):
        self.parent = parent
        self.state = state
        self.depth = depth

    def children(self):
        for state in self.state.new_states():
            yield Node(parent=self, state=state, depth=self.depth + 1)

    def extract_solution(self):
        print "Extracting soln"
        solution = []
        node = self
        solution.append(node)
        while node.parent is not None:
            solution.append(node.parent)
            node = node.parent
        solution.reverse()
        return solution

def dfs(root,sol = None):
    if root is None:
        return
    state = str(root.state.missionaries) + ',' + str(root.state.cannibals) + ',' + str(root.state.boat)
    if state in routes:
        if routes[state] < root.depth:
            return
        else:
            routes[state] = root.depth
    else:
        routes[state] = root.depth
    visited.append(root)

    if root.state.is_goal():
        sol = root
        return

    for child in root.children():
        if child not in visited:
            dfs(child,sol)

visited = []
routes = {}

def main():
    initial_state = State(3,3,1)
    root = Node(parent = None, state = initial_state,depth = 0)
    sol = Node(parent = None, state = initial_state,depth = 0)
    dfs(root,sol)
    ans = sol.extract_solution()
    print(ans)


if __name__ == '__main__':
    main()

PS。 就像@PM 2Ring所說的那樣,下一次:問問題時請修正縮進,這會使閱讀代碼更容易理解。 您可以通過選擇所有代碼,在所有選中的行中添加一個標簽,然后復制它來完成此操作。 粘貼之前,請確保有一個空行。 :)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM