Clojure 代理交互失败

发布于 2024-11-07 05:41:13 字数 601 浏览 9 评论 0原文

想象一下:平坦的世界有 n*n 个单元。有移动的物体。它们可能会发生碰撞,根据流程,其中之一应该被摧毁。

我正在使用 Rich Hickey 的蚁群的想法来将对象表示为代理。因此,发生碰撞后,其中一个智能体必须在收到其他智能体的消息后停止工作。

有一个代码片段可以说明我的问题:第二个代理意外停止。

;agents
(def f_guy (agent nil))

(def s_guy (agent nil))

;functions for agents
(defn f_do [x]
  (do
    (Thread/sleep 20)
    (println "f")
    (send-off *agent* f_do)))

(defn s_do [x]
  (do
    (Thread/sleep 40)
    (println "s")
    (send-off f_guy nil)
    (send-off *agent* s_do)))

;the process
(defn start []
  (do
    (send-off f_guy f_do)
    (send-off s_guy s_do)))

;go
(start)

谢谢你!

Imagine: flat world n*n cells. There are moving objects. They can collide, and according to process one of them should be destroyed.

I am using the ideas from ant colony of Rich Hickey to represent objects as agents. So after collision one of agent must stop his work after receiving message from other agent.

There is a code snippet to illustrate my problem: second agent stops unexpectedly.

;agents
(def f_guy (agent nil))

(def s_guy (agent nil))

;functions for agents
(defn f_do [x]
  (do
    (Thread/sleep 20)
    (println "f")
    (send-off *agent* f_do)))

(defn s_do [x]
  (do
    (Thread/sleep 40)
    (println "s")
    (send-off f_guy nil)
    (send-off *agent* s_do)))

;the process
(defn start []
  (do
    (send-off f_guy f_do)
    (send-off s_guy s_do)))

;go
(start)

Thank you!

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

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

发布评论

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

评论(2

深居我梦 2024-11-14 05:41:13

您向 f_guy 发送了一个请求,以将 nil 作为函数调用。他这样做了,这导致他因抛出异常而陷入崩溃状态。然后,你让他再次调用nil。但他甚至不能同意这样做,因为他已经崩溃了,正在等待有人来修复他。因此,s_guy 正在等待 f_guy 确认(但不一定执行)请求,并且 f_guy 当天已经下班了。

You sent f_guy a request to call nil as a function. He did, and that got him into a broken state by throwing an exception. Then, you ask him to call nil again. But he can't even agree to do that, because he is broken and waiting for someone to fix him. So s_guy is hanging around waiting for f_guy to acknowledge (not necessarily perform, yet) the request, and f_guy has clocked out for the day.

不乱于心 2024-11-14 05:41:13

请问您在这里想实现什么目标?

第一个和第二个代理在我的实验中都失败了,这是因为您基本上将代理放入代理内部的代理中...

示例:

(def a (agent 0))
(send-off a inc) ; -> #<Agent@29de03: 1>

因此,通过这样做:

(defn f_do [x]
  (do
    ...
    (send-off *agent* f_do)))

您将递归地返回代理内部的代理。

May I ask what are you trying to accomplish here?

Both the first and the second agent fail in my experiments which is because you are basically putting agents inside agents inside agents...

Example:

(def a (agent 0))
(send-off a inc) ; -> #<Agent@29de03: 1>

So by doing this:

(defn f_do [x]
  (do
    ...
    (send-off *agent* f_do)))

You are recursively returning agents inside agents.

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