node in children
始终为假
但在调试器中:
(pdb) children.keys()
...
turn: 1, last: (5, 2), hash: 5837165650205296398,
...
(pdb) node
turn: 1, last: (5, 2), hash: 5837165650205296398
(pdb) node in children
False
(pdb) node.__eq__(node)
True
此处是函数。
def _select(node):
path = []
global new, terminals, found
while True:
path.append(node)
if node not in children: new += 1;return path
if not children[node]: terminals += 1;return path;
unexplored = children[node] - children.keys()
if unexplored:
found += 1
n = unexplored.pop() # ignore best path?
path.append(n)
return path
# node = _uct_select(node)
else: node = choice(tuple(children[unexplored]))
这是哈希()和 eq ()函数
def __hash__(self):
"Nodes must be hashable"
return hash(tuple(self.board.flatten() ))
def __eq__(node1, node2):
"Nodes must be comparable"
return node1.board is node2.board
board只是一个[6,7]数组
答案 0 :(得分:1)
问题出在您的eq
函数上。除非它们是完全相同的列表,即。 ID等于is
时将返回false。它与==
board_b = [6,7]
board_a = [6,7]
print(board_a is board_b)
print(board_a == board_b)
打印
False
True
有关顶部的更多信息,请参见Is there a difference between "==" and "is"?。
答案 1 :(得分:1)
我从您的问题和评论中猜测node.board
是一个numpy数组;如果您明确提到这一点,将会有所帮助。 numpy数组上的==
运算符(即__eq__()
)确实具有一些令人惊讶的行为:它返回一个numpy布尔数组,而不是单个布尔值。
>>> import numpy as np
>>> a = np.array([1, 2])
>>> b = np.array([1, 2])
>>> a == b
array([ True, True])
但是is
运算符不能用作替代,因为这两个数组不是同一对象:
>>> a is b
False
因此,在这种情况下,您需要使用numpy.array_equal(node1.board, node2.board)
:
>>> np.array_equal(a, b)
True