【发布时间】:2017-05-11 11:33:31
【问题描述】:
我使用以下代码创建了一个小型网络并实现了随机游走算法。我使用一些节点作为目标,其中步行者最初放置在一个节点上。我使用带有walker 的路径(walker-own 变量)列表来保存它的位置。
问题:如何防止 walker 不返回那些已经访问并列在其内存(列表)中的节点。 我是 Netlogo 的新手,无法实现这个逻辑。
breed [nodes node]
breed [walkers walker]
walkers-own [location path]
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
set path (list location path)
]
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
set path lput 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
【问题讨论】:
标签: netlogo