2010-02-18 58 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