我创建了一个由通过链接连接的节点组成的小型网络。 一些节点是源和一些目标。我尝试实现随机游走算法。
我将步行者放在源节点上,步行者随机移动通过网络。现在,我想检查walker到达目标节点,如果所有目标节点都被walker访问,那么请求walker停止或死亡。 我是NetLogo的新手,不知道如何实现这个逻辑。 任何帮助或指南将不胜感激。
答案 0 :(得分:1)
执行此操作的一种方法是让您的节点知道它们是否是目标,以及它们是否已被访问过。这样,如果乌龟访问节点,则可以将该节点标记为已访问过。然后,您可以在go过程结束时使用stop
过程检查是否仍有任何节点仍然存在,但是未已访问过。我对Link-Walking Turtles示例做了一些修改,以显示一种可以做到这一点的方法 - 几乎所有下面的代码都直接从该模型中提取。
breed [nodes node]
breed [walkers walker]
walkers-own [location]
nodes-own [ target? visited? ]
to setup
clear-all
set-default-shape nodes "circle"
create-nodes 30 [
set color blue
set target? false
set visited? false
]
ask nodes [ create-link-with one-of other nodes ]
repeat 500 [ layout ]
ask nodes [
setxy 0.95 * xcor 0.95 * ycor
]
ask n-of 5 nodes [
set target? true
set color white
]
create-walkers 1 [
set color red
set location one-of nodes
move-to location
]
reset-ticks
end
to layout
layout-spring nodes links 0.5 2 1
end
to go
ask links [ set thickness 0 ]
ask walkers [
let new-location one-of [link-neighbors] of location
move-to new-location
set location new-location
;; This gets turtles to ask their current location
;; to set visited and target to true.
ask location [
set visited? true
if target? = true [
set color red
]
]
]
;; Check for target nodes that have NOT been visited.
;; If there aren't any, stop the model.
if not any? nodes with [ target? = true and visited? = false ] [
print ("All target nodes have been visited.")
stop
]
tick
end