2010-02-18 57 views
2

完全公開,這是一項家庭作業的一部分(儘管是一小段代碼,該項目本身就是一個玩AI的遊戲)。Python中的無盡遞歸

我有這個功能內置到樹節點類:

def recursive_score_calc(self): 
     current_score = self.board 
     for c in self.children: 
      child_score = c.recursive_score_calc() 
      if(turn_color == 1): 
       if(child_score > current_score): 
        current_score = child_score 
      else: 
       if(child_score < current_score): 
        current_score = child_score 
     self.recursive_score = current_score 
     return current_score 

深度1(根有的孩子)的樹,它擊中了Python遞歸限制了。該函數旨在使用動態編程從下到上構建最小最大樹。說實話,我不知道爲什麼它不能按預期工作,但我對Python也相當陌生。

堆棧溢出的好人:爲什麼這段代碼給我堆棧溢出?

有關整個類:

from Numeric import * 

class TreeNode: 
    children = [] 
    numChildren = 0 
    board = zeros([8,8], Int) 
    turn_color = 0 # signifies NEXT to act 
    board_score = 0 # tally together board items 
    recursive_score = 0 # set when the recursive score function is called 

def __init__(self, board, turn_color): 
    self.board = copy.deepcopy(board) 
    self.turn_color = turn_color 
    for x in range (0,7): 
     for y in range (0,7): 
      self.board_score = self.board_score + self.board[x][y] 

def add_child(self, child): 
    self.children.append(child) 
    self.numChildren = self.numChildren + 1 

def recursive_score_calc(self): 
    current_score = self.board # if no valid moves, we are the board. no move will make our score worse 
    for c in self.children: 
     child_score = c.recursive_score_calc() 
     if(turn_color == 1): 
      if(child_score > current_score): 
       current_score = child_score 
     else: 
      if(child_score < current_score): 
       current_score = child_score 
    self.recursive_score = current_score 
    return current_score 

與此(請注意,這是接壤的什麼是適當的在這裏發表邊緣相互作用的功能,我將我接受後刪除此部分答案):[它反正不是關鍵部分]

+0

你能告訴我們創建樹的代碼嗎?您可能將自己添加爲其中一個孩子。 – 2010-02-18 03:14:49

+0

現在所有。合法移動是當前遊戲狀態中的一組可能移動,並且應該包含樹節點的所有子節點。 – alexwood 2010-02-18 03:18:46

回答

7

代碼的此位:

class TreeNode: 
    children = [] 

意味着類股同樣children列表的每一個實例。所以,在這個位:

def add_child(self, child): 
    self.children.append(child) 

你正在追加到「全球級」列表。所以,當然,每個節點都是其他節點的孩子,災難是有保證的。

修復:改變你的類

class TreeNode(object): 
    numChildren = 0 
    board = zeros([8,8], Int) 
    turn_color = 0 # signifies NEXT to act 
    board_score = 0 # tally together board items 
    recursive_score = 0 # set when the recursive score function is called 

def __init__(self, board, turn_color): 
    self.children = [] 
    self.board = copy.deepcopy(board) 
    self.turn_color = turn_color 
... etc, etc ... 

休息並不需要改變,以修復這個bug(雖然可能有機會改善其或修復其他錯誤,我沒有深刻檢查它) ,但未能指定self.children__init__正在導致你目前的錯誤,並沒有從object繼承(除非你使用Python 3,但我希望你會提到這個小細節,如果是這樣的話;-)只是一個等待發生的錯誤。

3

它看起來像self.children包含self

EDIT

children屬性被初始化爲相同的數組實例爲TreeNode類的每個實例。

您需要爲每個TreeNode實例創建一個單獨的陣列實例,方法是將self.children = []添加到__init__
board數組有同樣的問題。

+0

這是一個有效的可能性,我不完全理解「自我」的含義,除非我在我身上添加錯誤,直到它發生錯誤。你能否詳細說明一下? – alexwood 2010-02-18 03:07:39

+0

您正在循環瀏覽'self.children'並在每個孩子身上調用相同的方法。如果其中一個孩子是對象本身,則會發生遞歸。你能發佈你的整個程序嗎? – SLaks 2010-02-18 03:11:33

+2

'self'是對類對象的引用。就其本身而言。得到它? – jathanism 2010-02-18 03:12:03