alpha beta pruning in python
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,alph开发者_Go百科a, 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?
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!
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.
精彩评论