2016-09-17 23 views
0

我想在python中实现minimax算法,但我在创建分支树结构时遇到了一些问题。我仍然是一个业余程序员,所以请不要介意我的代码看起来不好。这里是我的节点结构如何在Python中正确实现具有n分支因子的树?

class Node: 
    parent = None 
    state = None 
    pmax = 0  #this represents the MAX player symbol; i.e. SELF 
    pmin = 0  #this represents the MIN player symbol; i.e. OPPONENT 
    stateCost = 0 #this represents utility cost of leaf nodes based on the matrix state 
    bestCost = None #this represents the chosen path in the minimax algorithm 
    children = [] 

    def __init__(self,mat,up,mx,mn): 
     self.parent = up 
     self.state = mat 
     self.pmax = mx 
     self.pmin = mn 
     stateCost = 0 

    def addChild(self,newState,up): 
     n = Node(newState,up,self.pmax,self.pmin) 
     self.children.append(n) 

    def evaluateChildren(self,minmax): 
     ln = len(self.state[0]) 
     for x in range(ln): 
      #newState = insertIntoSink(self.state[0],x) 
      cloneState = cloneMatrix(self.state) 
      newState = insertIntoSink(cloneState,x,minmax) 
      print "state being added" 
      for list in newState: 
       print list 
      self.addChild(newState,self) 

     print "done Evaluating CHILDREN" 


    def evaluateSubChildren(self,minimax): 
     ln = len(self.state[0]) 

     for l in self.children: 
      for x in range(ln): 
       cloneState = cloneMatrix(self.state) 
       newState = insertIntoSink(cloneState,x,minimax) 
       l.addChild(newState,l) 

在我在做什么的情况下,必须有7个孩子的根节点,每个孩子应该有7个孩子的本身。每个孩子将在父节点中拥有初始矩阵的修改克隆。

发生错误是在添加子级(即第二级子级)后,不会将子级添加到子级列表中,而是会将它们添加到根节点中。

孩子的创造被称为如下。

def getBestMove(self): 
    move =0 
    maxVal = -1; 
    i=-1; 
    #evaluate all the 7 children 

    self.evaluateChildren(2) 

    self.evaluateSubChildren(1) 

    #more calculations go here 

我第一次尝试调用相同evaluateChildren()函数如下:

def getBestMove(self): 
    move =0 
    maxVal = -1; 
    i=-1; 
    #evaluate all the 7 children 

    self.evaluateChildren(2) 


    #evaluate second level children 
    for n in self.children: 
     print "evaluating SECOND LEVEL CHILDREN" 
     n.evaluateChildren() 

如果我检查评估后的根节点的子节点的数量,应该是7,但是它保持加更多的2级的孩子,这是在无限循环中抛出我的程序。

请让我知道我要去哪里错了。请询问是否需要更多信息。

回答

0

你打了普通跳闸了列表和变量绑定在Python - 你让children = []类变量,这使得它相同的列表中的每个节点。内存中有一个列表,每个节点指向它。改变它,所有的节点看到变化。将其移动到__init__以使其成为每个实例的新建立。

Class Node: 

    def __init__(self,mat,up,mx,mn): 

     self.children = [] 

形形色色的人绊倒了同样的问题,“很多事情引用错误完全相同的名单”,吨的讨论发生了什么,为什么,如何避免它:

Python Strange Behavior with List & Append

Weird list behavior in python

Python array weird behavior

Some strange behavior Python list and dict

Strange behavior of lists in python

List of lists changes reflected across sublists unexpectedly

Generating sublists using multiplication (*) unexpected behavior

List of lists changes reflected across sublists unexpectedly

Nested List Indices

Function changes list values and not variable values in Python

Why does my original list change?

Python: why does my list change when I'm not actually changing it?

python: list changes when global edited

python: changes to my copy variable affect the original variable

+1

完美!!非常感谢!!简单而直接点:) –