我有三个模型:Parent,Child1,Child2。
Child1生成字符串,然后必须将其传递给Child2进行进一步处理。当执行Child1时,将即时生成字符串。每当生成新字符串时,都必须立即将其发送给Child2。有时(由不同的代理)同时生成2个或更多字符串,然后必须在同一时间(即,在同一时间间隔内)发送这2个或更多字符串。
一种解决方案是跳过父模型,然后让Child1成为父模型,然后可以访问Child2。但是,这会使Child1变得复杂,因为它将包括所有必需的LevelSpace代码。此外,两个子模型必须可以单独使用。这两个模型用于教育目的,学生不应看到LevelSpace代码。因此,父模型。
另一种解决方案是让Parent模型连续轮询Child 1模型以请求新的字符串。不太漂亮不太有效。
所以基本上我在LevelSpace中寻找一些共享的内存和/或事件功能:-)
通过字符串代表DNA,RNA等的方式,我们正在说明字符串等的转录和翻译。
有什么想法吗?
谢谢, 帕尔
答案 0 :(得分:3)
Jasper编写了一个非常不错的解决方案,尽管它确实依赖于不应该起作用的行为。另一种方法是让child1
收集在全局变量中运行的字符串。例如
孩子1的代码:
globals [ strings ]
to go
set strings []
ask turtles [
set strings lput (word "hello from turtle " who) strings
]
tick
end
然后让child2
在其go
方法中接受字符串列表
孩子2的代码:
to go [ strings ]
; do stuff with the strings
end
然后父母在他们之间传递清单:
父代码:
to go
ls:ask child1 [ go ]
ls:let child1-strings [ strings ] ls:of child1
ls:ask child2 [ go child1-strings ]
tick
end
答案 1 :(得分:1)
这绝对不是常规的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
子模型:
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
。