alpha beta pruning in python

删除回忆录丶 提交于 2019-12-24 04:29:21

问题


In alpha, beta pruning algorithm, I have a class in which a function def getAction(self,gamestate) id defined. I made 2 more function in def getAction

Like:

class BAC:
  def you(self,gamestate):
    def me(gamestate,depth,alpha, beta):
    ------
      return v
    def both(gamestate,depth,alpha, beta):
    -------------------
      return v
   return me(gamestate,0,alpha, beta)-

I need to put alpha, beta in functions me and both. But where do I define alpha and beta values. If I define alpha and beta in def me and def both then error occurs as there global name alpha is not present.

How do I make alpha and beta local variables or How can I make it possible to work correctly?


回答1:


I guess that you whant to do define alpha and beta as your classes' attribute.

You may need a class constructor that initializes your variables.

class BAC:
    def __init__(self):
        self.alpha= 'Hello '
        self.beta= 'World!'
    def me(self):
        print self.alpha + self.beta

Or you can initialize your attributes variables like:

class BAC:
    def me(self,alpha,beta):
        self.alpha = alpha
        self.beta= beta
        print self.alpha + self.beta

All you need is to refer to the classes' attribute if you want persistence.

If you choose the first solution:

my_bac = Bac()
print my_bac.alpha + my_bac.beta

The output will be

Hello World!
Hello World!



回答2:


Often the alpha-beta algorithm is implemented as a higher level function that assigns initial value to both alpha and beta and then the passes these values to the recursive (or alternately a non-recursive stack-based) search function.

The class solution given above is good too, although I would probably add a method to reset alpha and beta so that a class instance doesn't have to be instantiated at the start of each search.



来源:https://stackoverflow.com/questions/3392228/alpha-beta-pruning-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!