子节点中的节点
始终为 false
但在调试器中:
(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]))
这是 hash() 和 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] 数组
最佳答案
我从你的问题和评论中猜测 node.board
是一个 numpy 数组;如果您明确提及这一点将会有所帮助。 numpy 数组上的 ==
运算符(即 __eq__()
)确实有一些令人惊讶的行为:它返回一个 numpy bool 值数组,而不是单个 bool 值。
>>> 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
关于python - "in dict"始终为假,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57553295/