Bru*_*uno 6 python chess alpha-beta-pruning negamax
这是我上一篇文章的后续内容。该代码运行时没有任何错误,并且可以计算下一个最佳移动。我一直在研究如何将换位表和移动排序合并到我的 negamax 函数中,以使其运行得更快、更准确,但对于像我这样的初学者来说,这似乎有些困难和先进。
你可以在这里找到我的代码。
在研究国际象棋编程维基时,我发现了一些换位表的示例代码:
def negamax(node, depth, alpha, beta, color):
alphaOrig = alpha
## Transposition Table Lookup; node is the lookup key for ttEntry
ttEntry = transpositionTableLookup(node)
if ttEntry.is_valid is True and ttEntry.depth >= depth:
if ttEntry.flag == EXACT :
return ttEntry.value
if ttEntry.flag == LOWERBOUND:
alpha = max(alpha, ttEntry.value)
if ttEntry.flag == UPPERBOUND:
beta = min(beta, ttEntry.value)
if alpha >= beta:
return ttEntry.value
if depth == 0 or node is terminal_node:
return color* heuristic_value_of_node
childNodes = domove(node)
childNodes = orderMoves(childNodes)
bestValue = -99999
for child in childNodes:
bestValue = max(bestValue, -negamax(child, depth - 1, -beta, -alpha, -color))
alpha = max(alpha, bestValue)
if alpha >= beta:
break
##Transposition Table Store; node is the lookup key for ttEntry
ttEntry.value = bestValue
if bestValue <= alphaOrig:
ttEntry.flag = UPPERBOUND
if bestValue >= beta:
ttEntry.flag = LOWERBOUND
else:
ttEntry.flag = EXACT
ttEntry.depth = depth
transpositionTableStore(node, ttEntry)
return bestValue
Run Code Online (Sandbox Code Playgroud)
我尝试进行一些修改以将其集成到我的代码中,但没有得到任何结果。我还看到过一些关于使用 Zobrist 键存储哈希键的信息,但我不太明白它是如何工作的,所以我放弃了这个想法。目前有点陷入这些问题,不知道下一步是什么。
要使用转置表,您“需要”使用 Zorbrist 哈希。散列为每个位置提供一个(几乎)唯一的代码,您将其及其评估存储在转置表中。然后,为了简单地解释一下,如果您正在搜索的当前位置在换位表中找到,您将不必再次评估它,您只需使用存储的值即可。
Zorbrist 按键的正确性是一场噩梦,而且很难调试。如果它有帮助,您可以查看我在Endamat Chess Engine中的实现,但由于您可能有不同的方法,因此阅读 Zorbrist 键的工作原理并尝试使其适合您的实现可能会更容易。
| 归档时间: |
|
| 查看次数: |
1895 次 |
| 最近记录: |