我有三个模型:Parent、Child1、Child2。

Child1 生成字符串,然后必须将其传递给 Child2 以进行进一步处理。字符串是在 Child1 执行时即时生成的。每当生成新字符串时,必须立即将其发送给 Child2。有时(由不同的代理)同时生成 2 个或更多字符串,然后必须同时发送这 2 个或更多字符串(即在同一滴答内)。

一种解决方案是跳过父模型,让 Child1 成为父模型,然后它可以访问 Child 2。但是,这会使 Child1 复杂化,因为它将包含所有必需的 LevelSpace 代码。此外,这两个子模型必须可以单独使用。这两个模型用于教育目的,学生不应看到LevelSpace代码。因此,父模型。

另一种解决方案是让 Parent 模型不断轮询 Child 1 模型以请求新字符串。不是很漂亮。效率不高。

所以基本上我在 LevelSpace 中寻找一些共享内存和/或事件功能 :-)

顺便说一下,字符串代表 DNA、RNA 等,我们正在说明字符串等的转录和翻译。

有任何想法吗?

谢谢,
帕莱

最佳答案

这绝对不是传统的 NetLogo 代码,但回调方法似乎可以正常工作,至少对于这个简单的例子是这样。您确实需要稍微复杂化子模型以添加 run callback 语句,但它可能会导致整体代码比轮询方法更清晰,具体取决于用例。

父模型:

extensions [ ls ]

globals [ child1 child2 ]

to setup
  ls:reset
  (ls:create-models 1 "LS callback child.nlogo" [ [id] -> set child1 id ])
  (ls:create-models 1 "LS callback child.nlogo" [ [id] -> set child2 id ])
  ls:ask ls:models [
    child-setup
  ]
  ; here we set the callback for the child models
  ; we could set the callback for just `child1` instead of all `ls:models`
  ls:assign ls:models callback [ [id message] -> alert id message ]
  ls:assign child1 id child1
  ls:assign child2 id child2
end

to go
  ls:ask ls:models [
    child-go
  ]
end

; In this case our callback is simple, just taking the caller id
; and a message.  We could add more parameters for it if we want to
to alert [id message]
  show (word id ": " message)
  ; this is just to show that we can use the callback to update
  ; the state of one of the other models
  if id = 0 [
    ls:ask child2 [ set some-val (some-val + 1) ]
  ]
end

child 模型:
globals [ id callback some-val ]

to child-setup
  set some-val 0
  ; set the callback to an "empty" procedure so we don't have to check
  ; if it is set while we run the go method.
  set callback [ [model-id message] -> ]
end

to child-go
  if random 10 < 3 [
    (run callback id (word "child alert: " some-val))
  ]
end

示例输出:
observer: "1: child alert: 0"
observer: "1: child alert: 0"
observer: "1: child alert: 0"
observer: "0: child alert: 0"
observer: "1: child alert: 1"
observer: "0: child alert: 0"
observer: "1: child alert: 2"
observer: "1: child alert: 2"
observer: "0: child alert: 0"
observer: "1: child alert: 3"
observer: "1: child alert: 3"
observer: "1: child alert: 3"
observer: "0: child alert: 0"
observer: "1: child alert: 4"
observer: "0: child alert: 0"
observer: "1: child alert: 5"
observer: "1: child alert: 5"
observer: "0: child alert: 0"
observer: "1: child alert: 6"

每次 child1 模型运行回调时, alert 过程都会增加 some-val 中的 child2 全局变量。

关于网络标识和关卡空间 : how to pass strings between two child-models?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/54332081/

10-09 13:36