当时间用完时,退出java中的递归

发布于 2024-11-05 18:19:06 字数 247 浏览 1 评论 0原文

我正在为一款类似国际象棋的游戏实现人工智能。我打算使用递归来尝试棋盘的所有可能状态并选择“最佳动作”。

由于每次移动的时间限制,我需要有某种机制来在达到时间限制时打破这些递归过程。当然,我可以在进行递归调用之前不断检查时间,并在当前时间接近限制时中断,但这是对我的程序性能的权衡。

如果有一种方法可以在计时器结束时中断这些递归过程,那就太好了。但是,由于我是Java新手,我不知道在Java中是否有任何方法可以做到这一点?你能给出示例代码吗? :)

I'm implementing AI for a chess-like game. I intend to use recursion to try all the possible state of the board and choose out the 'best move'.

Because of the time's limit per move, i need to have some mechanism to break out of those recursive procedure whenever the time limit is reached. Of course i can keep checking the time before making a recursion call and break out if the current time is near the limit, but it is a trade-off with the performance of my program.

It would be great if there is a way to break out of those recursive procedure whenever a timer end. However, since i'm new to Java, i don't know if there are any way to do so in java? Can you give an example code? :)

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

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

发布评论

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

评论(3

星星的轨迹 2024-11-12 18:19:07

检查时间,例如 System.currentTimeMillis() 每次调用花费大约 200 ns。但是,如果这对您来说太多了,您可以让另一个线程设置一个标志来停止。

已经有一种机制可以做到这一点。

ExecutorService es = Executors.newSingleThreadExecutor();
Future f = es.submit(new Runnable() {
    @Override
    public void run() {
        long start = System.nanoTime();
        while(!Thread.interrupted()) {
            // busy wait.
        }
        long time = System.nanoTime() - start;
        System.out.printf("Finished task after %,d ns%n", time);
    }
});
try {
    f.get(1, TimeUnit.SECONDS); // stops if the task completes.
} catch (TimeoutException e) {
    f.cancel(true);
}
es.shutdown();

注意

Finished task after 1,000,653,574 ns

:您不需要每次都启动/停止 ExecutorService。

Checking the time, e.g. System.currentTimeMillis() costs about 200 ns per call. However if this is to much for you, you can have another thread set a flag to stop.

There is a mechanism to do this already.

ExecutorService es = Executors.newSingleThreadExecutor();
Future f = es.submit(new Runnable() {
    @Override
    public void run() {
        long start = System.nanoTime();
        while(!Thread.interrupted()) {
            // busy wait.
        }
        long time = System.nanoTime() - start;
        System.out.printf("Finished task after %,d ns%n", time);
    }
});
try {
    f.get(1, TimeUnit.SECONDS); // stops if the task completes.
} catch (TimeoutException e) {
    f.cancel(true);
}
es.shutdown();

prints

Finished task after 1,000,653,574 ns

Note: you don't need to start/stop the ExecutorService every time.

小嗷兮 2024-11-12 18:19:07

我认为没有任何好的方法可以做到这一点,而不涉及检查是否可以继续。

即使您确实检查了时间...如果还剩 8 毫秒 会发生什么。你能保证你的递归调用会在那个时间内完成吗?您是否在每一个小步骤之后检查时间(这可能会增加很多额外的开销)?

一种方法是让执行(递归)逻辑在一个线程中运行,并在另一个线程中运行计时器。当计时器完成时,它会在执行线程上调用 interrupt()。在工作线程中,每次完成递归时,都会保存所需的状态。然后,如果它被中断,则返回最后保存的状态。

这只是一种方法的简要描述..绝不是最好的方法

I don't think there is any nice way of doing this that doesn't involve checking if you can continue.

Even if you did check the time... what happens if you have 8 milliseconds remaining. Can you guarantee that your recursive call will finish in that time? Do you check the time after every little step (this may add a lot of extra overhead)?

One way is to have your execution(recursion) logic running in one thread, and a timer in another thread. When the timer completes, it invokes an interrupt() on your execution thread. In your worker thread, everytime you complete a recursion, you save the state that you need. Then if it gets interrupted, return the last saved state.

That's just a brief description of one way to do it.. by no means the best way

巨坚强 2024-11-12 18:19:07

您可以使用布尔标志来设置 AI 任务何时必须停止。

创建一个将运行 AI 任务的线程,该线程将在每次递归调用之前检查布尔变量。检查布尔变量比调用方法获取时间更有效。父线程是否在有限时间内休眠。唤醒后,设置布尔标志以停止子线程。

You can use a boolean flag to set when the AI task have to stop.

Create a thread that will run the AI task, this thread will check a boolean variable before each recursive call. To check boolean variable is more efficient than to call a method to get time. Do the parent thread sleep for the limited time. After it wake up, set the boolean flag to stop the child thread.

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