gpt4 book ai didi

python - python国际象棋引擎的换位表

转载 作者:行者123 更新时间:2023-12-04 17:07:55 25 4
gpt4 key购买 nike

这是我上一篇文章的后续。
代码运行没有任何错误,可以计算下一个最佳移动。我一直在研究如何将换位表和排序移动到我的 negamax 函数中以使其运行得更快更准确,但对于像我这样的初学者来说似乎有些困难和高级。
你可以找到我的代码 here .
在研究国际象棋编程维基时,我发现了一些换位表的示例代码:

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
我尝试进行一些修改以将其集成到我的代码中,但没有得到任何结果。
我还看到了一些关于使用 Zobrist 键存储位置哈希键的内容,但我不太了解它是如何工作的,所以我放弃了这个想法。目前有点卡在这些问题上,不知道下一步是什么。

最佳答案

要使用换位表,您“需要”使用 Zorbrist 散列。散列为每个位置提供一个(几乎)唯一的代码,您将其与其评估一起存储在换位表中。然后,为了简单解释,如果在您的换位表中找到您正在搜索的当前位置,您将不必再次评估它,您只需使用存储的值。
Zorbrist 键是正确的噩梦,而且很难调试。如果有帮助,您可以在 Endamat Chess Engine 中查看我的实现,但由于您可能有不同的方法,因此阅读 Zorbrist 键的工作原理并尝试使其适合您的实现可能会更容易。

关于python - python国际象棋引擎的换位表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70145904/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com