五子棋:搜索时间有限

发布于 2024-09-28 03:23:06 字数 249 浏览 4 评论 0原文

我正在创建一个 C 程序来玩 Gomoku。它使用 Minimax 搜索来决定最佳移动。然而,它只能搜索 10 秒的最佳走法。如何确定我的搜索功能何时已花费 10 秒进行搜索。如果您能为我提供示例或文档链接,我将不胜感激。

I'm creating a C program to play Gomoku. It uses Minimax search to decide on the best move. However, it can only search for the best move for 10 seconds. How to I determine when my search function has spent 10 seconds searching. If you could provide me with either an example or a link to the documentation that would be much appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

冷血 2024-10-05 03:23:06
#include <time.h>
time_t start_time = time(NULL);
while (((int)(time(NULL) - start_time)) < 10) {
  //search
}

这就是我想到的。但它还没有经过测试。

#include <time.h>
time_t start_time = time(NULL);
while (((int)(time(NULL) - start_time)) < 10) {
  //search
}

That is, what comes to my mind. It is not tested though.

吹泡泡o 2024-10-05 03:23:06

我认为你的问题不是时间函数本身。您提到了最小最大算法,它是递归的。最小最大算法的停止标准是给定的搜索深度。如果您想要基于时间的停止标准,您应该使用迭代深化框架来扩展您的算法如果时间结束,让递归 Minmax 函数返回 Sentinel Value

I think your problem is not the time function itself. You mentioned the Minmax Algorithm, which is recursive. The stopping criterion of the Minmax Algorithm is the given search-depth. If you like to have a time-based stopping criterion you should expand your Algorithm with a Iterative Deepening framework and let the recursive Minmax Function return a Sentinel Value, if time is over.

等待我真够勒 2024-10-05 03:23:06

仅靠时间检查并不能完成这项工作!极小极大是一种递归深度优先搜索算法,当存在一些明显更好的走法并且恰好在最后1秒找到好招!

你必须使用某种算法,它可以在短时间内找到一个很好的移动,然后,随着越来越多的可用时间,它会改进解决方案!您必须将极小极大(或 alpha-beta)算法修改为广度优先搜索策略。然后你就可以随时砍掉它,动作相当好。

The sole time checking will not do the job! The minimax is a recursive depth-first search algorithm, which can spend e.g. 30 seconds examining very wrong moves when there are some obviously much better moves and just in the last 1 second to find some good move!

You have to use some algorithm which finds quite a good move in short time and then, with more and more time available, it improves the solution! You have to modify your minimax (or alpha-beta) algorithm to breadth first search strategy. Then you can cut it at any time having quite good moves.

黑寡妇 2024-10-05 03:23:06

您可以使用警报信号。只需让信号处理程序设置一个名为 okWereDoneNow 的全局标志,然后开始搜索、检查并重置它。

与计时器函数相比,它的优点是每次搜索迭代只需要一次比较。信号工作很昂贵,但只运行一次。在密集的、可能受 CPU 限制的重复操作中,这可能是一个显着的优势。但不要相信我的话——测试!

You could use the alarm signal. Simply have the signal handler set a global flag called okWereDoneNow and have your search start, check for, and reset it.

The advantage of this over the timer functions is that it requires only a single comparison per iteration of the search. The signal work is expensive, but only run once. In an intensive, presumably-CPU-bound repeated operation, this could be a significant advantage. But don't take my word for it - test!

一抹苦笑 2024-10-05 03:23:06

您可以使用 time.h 中的 time() 函数。一般情况下,返回值以秒为单位。即使不是,您也可以简单地使用同一标头中的 difftime() 。

这是有关必要功能的良好资源。

上面的链接来自 C++ 参考站点,但该标头和示例都是 C 代码。

You could use the time() function in time.h. Generally, the returned value is in seconds. Even if it is not, you could simply use difftime() from the same header.

This is a good resource on the necessary functions.

The above link is from a C++ reference site, but that header and examples are all C code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文