我正在教自己如何使用Railsback& amp;的书在Netlogo中创建ABM。 Grimm 2012.我遇到了一个关于蝴蝶跟随"虚拟"走廊。基本的想法是蝴蝶上坡交配使用高度差异作为指导。我需要计算走廊的宽度,将蝴蝶使用的补丁数量除以蝴蝶从起始补丁到最终补丁的平均距离。我是 努力绘制这个走廊宽度,我编码如下:
to-report corridor-width
let patches-visited count patches with [used?]
let mean-distance mean [distance start-patch] of turtles
report patches-visited / mean-distance
然后我在界面中用命令创建了一个图:
plot corridor-width
我收到的错误消息是:
除以零。观察者运行/调用过程时出错 CORRIDOR-WIDTH称为情节'走廊宽度'笔'默认'更新 由Button' setup'
调用的过程SETUP调用的代码
我认为我编码distance start-patch
的方式有问题,但我浏览网页并查看了几个代码,我无法发现错误。我的整个代码看起来像这样:
globals [ q ] ;; q is the probability that butterfly moves directly to highest patch
turtles-own [ start-patch ]
patches-own [ elevation used? ] ;; patches property of elevation and whether the patch has been used by butterfly or not.
to setup
ca
;; Let's create patches and asign them an elevation and color by using ask patches statement
ask patches
[
;; Elevation decreases linearly with distance from the center of hills. Hills are at (30,30) and
;; (120,120) coordinates. The first hill is 100 units high whereas the second one is 50
let elev1 100 - distancexy 30 30
let elev2 50 - distancexy 120 100
ifelse elev1 > elev2
[ set elevation elev1 ]
[ set elevation elev2 ]
set pcolor scale-color green elevation 0 100
set used? false
]
;; Create 50 butterflies
crt 50
ask turtles [
set size 6
;; set their initial location as their initial patch
setxy random-pxcor random-pycor
set start-patch patch-here
;; have the butterfly draw its path with the pen-down statement
pen-down
]
reset-ticks
;; Initialize the q parameter
set q 0.4
end
;; The master schedule
to go
ask turtles [ move ]
plot corridor-width
tick
if ticks >= 1000
[
let final-corridor-width corridor-width
write "Corridor width: " print final-corridor-width
;export-plot "Corridor width" (word "Corridor-width-output-for-q-" q ".csv")
stop
]
end
;; let's code the butterfly procedure of movement
to move
if elevation >=
[ elevation ] of max-one-of neighbors [ elevation ]
[ stop ]
ifelse random-float 1 < q ;; Decide whether to move to the highest sorrounding
;; patch with p=q
[ uphill elevation ] ;; move deterministically uphill
[ move-to one-of neighbors ] ;; or move randomly
set used? true
end
to-report corridor-width
let patches-visited count patches with [used?]
let mean-distance mean [distance start-patch] of turtles
report patches-visited / mean-distance
end
答案 0 :(得分:1)
当平均距离为0时会发生什么?
let mean-distance mean [distance start-patch] of turtles
基本上,在您的设置中,您将所有海龟的启动补丁设置为当前补丁。所以,如果你问他们所有的海龟离开始补丁有多远,他们都会告诉你0个单位。
因此,[distance start-patch] of turtles
填充了所有0的列表。
因此,所有0的列表的平均值为0,导致除以0错误。
也许在这种情况下,你想报告0而不是......所以
ifelse mean-distance = 0
[ report 0]
[report patches-visited / mean-distance]