简体   繁体   English

C#minimax树实现

[英]C# minimax tree realization

I'm trying to write C# Chess AI. 我正在尝试编写C#Chess AI。

At that moment I have to build my minmax tree. 那一刻我必须建立我的minmax树。 I try by using recursion, but my recursive functions has to call itself about 1 000 000 times for every node. 我尝试使用递归,但我的递归函数必须为每个节点调用自己大约1 000 000次。 I get Stack Overflow exception after about... 60 000 calls. 大约有60 000个电话后,我得到Stack Overflow异常。

I guess you are using a depth first search. 我想你正在使用深度优先搜索。 This isn't too useful when the search space is so large. 当搜索空间如此之大时,这不太有用。 When implementing minimax you can use a breadth first search implemented as a depth first search with iterative deepening . 在实现minimax时,您可以使用广度优先搜索作为深度优先搜索,并进行迭代深化

You should have a maximum number of levels you are allowed to recurse as a parameter to your functions, and decrease that by one each time you call your function recursively, until you hit zero when you stop and backtrack. 你应该有一个最大数量的级别可以作为函数的参数递归,并且每次递归调用函数时减少一个级别,直到你停止和回溯时达到零。 Start with a small maximum depth and slowly increase it until you find an acceptable solution, or else run out of time. 从较小的最大深度开始,然后慢慢增加它,直到找到可接受的解决方案,否则就会耗尽时间。

Given that the depth of the recursion might not be known you might want to stop to at a reasonable limit like 10 moves in advance and/or ignore trees that have a lower utility score. 鉴于递归的深度可能未知,您可能希望停止在合理的限制,例如提前10次移动和/或忽略具有较低效用得分的树。 By adding extra constraints such as these you can also ensure that a solution will be found quickly without having to optimize extensively. 通过添加诸如此类的额外约束,您还可以确保快速找到解决方案,而无需进行广泛优化。

As echoed by others it sounds like you probably have a bug given your large number of iterations. 正如其他人所回应的那样,听起来你可能会因为大量的迭代而出现错误。 It might be possible to prune out a variety of rules or chose a different search strategy to reduce the number of iterations required, such as iterative deepenning , A* or perhaps a Genetic Algorithm for fun, 有可能删除各种规则或选择不同的搜索策略来减少所需的迭代次数,例如迭代加深A *或者可能是遗传算法

It would be much better to return a result even if it is not perfect rather than failing after searching the tree too deep. 返回结果即使它不完美而不是在搜索树太深之后失败也会好得多。

Good luck. 祝好运。

Most chess search programs can only search to depth 9 or 10. This is not enough to overflow the stack at all. 大多数国际象棋搜索程序只能搜索到9或10深度。这根本不足以溢出堆栈。

You probably have an error in your program. 您的程序可能有错误。 In any case, you do need to have a depth limit on your search as you will not be able to do full-width search (exploring all possibilities across all moves up to a given depth) without a depth limit, since a chess game is potentially of unlimited depth (except for the repeated positions or limit on moves rule). 在任何情况下,你都需要对你的搜索有一个深度限制,因为你将无法进行全角搜索(探索所有移动到达给定深度的所有可能性)而没有深度限制,因为国际象棋游戏是可能具有无限深度(除了重复的位置或移动限制规则)。

After you search to about depth 9, you have to use a static board evaluation function to evaluate and score the position. 搜索到深度9后,您必须使用静态板评估功能来评估和评分位置。 You then use the mini-max algorithm to prune branches of the search tree. 然后使用mini-max算法修剪搜索树的分支。 It's still an exponential search though. 尽管如此,它仍然是一个指数搜索。

There are also techniques such as quiescent search, where you continue searching in positions where the position is not stable (ie if a piece can be taken or the king is in check). 还有一些技术,例如静止搜索,您可以继续搜索位置不稳定的位置(即,是否可以拍摄一张照片或检查国王)。

if you are interested in learning how to write a C# Chess Engine I invite you to visit the Computer Chess Blog 如果您有兴趣学习如何编写C#国际象棋引擎,我邀请您访问计算机国际象棋博客

The blog describes a creation of a C# Chess Engine from the first steps, providing C# code examples. 该博客描述了从第一步开始创建C#Chess引擎,提供了C#代码示例。

The page you might be the most interested in is: Move Searching and Alpha Beta 您可能最感兴趣的页面是: 移动搜索和Alpha Beta

This article gives a detailed explanation of Min Max and the Alpha Beta search algorithms including C# code examples of both. 本文详细解释了Min Max和Alpha Beta搜索算法,包括两者的C#代码示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM