我正在写一些代码。乌龟和斑块设置为具有值。它们都没有零值。我正在尝试用农民周围DWS(补丁变量)的平均值来划分地表水(农民拥有)。我假设没有值是零,但得到除以零的错误。我要求找出错误并帮助我进行更正。这是代码
Globals [ Water-Availbility]
Breed [farmers farmer]
farmers-own [ WA sw ]
patches-own [
GW wtd DWS]
to setup
clear-all
setup-farmers
ask patches [ ifelse random 4 = 0 [
set dws distance-from-water-source + random-float 50
set WTD DWS / depth-WT + random-float 1
set GW Ground-water + random-float 5.005 + (100 / wtd)
set pcolor blue
]
[ set dws distance-from-water-source + random-float 100
set WTD DWS / depth-WT + 5 + random-float 5
set GW Ground-water + random-float 5.005 + (100 / wtd)
set pcolor red]]
end
to setup-farmers
create-farmers num-farmers [move-to one-of patches
set shape "person"
set sw random 100 + surface-water / sum [DWS] of patches in-radius 1 / count patches in-radius 1
SET WA sw + sum [gw] of patches in-radius 1 / count patches in-radius 1 + [gw] of self
]
end
答案 0 :(得分:2)
这似乎只是setup
中的调度问题-在设置补丁之前调用setup-farmers
,因此当农民查询时所有补丁的DWS
都等于零那个价值。 sum [DWS] of patches in-radius 1
因此为0,因此分母为零。将您的设置更改为以下内容,您应该一切顺利。
to setup
clear-all
ask patches [
ifelse random 4 = 0 [
set dws 50 + random-float 50
set WTD DWS / 50 + random-float 1
set GW 50 + random-float 5.005 + (100 / wtd)
set pcolor blue
] [
set dws 50 + random-float 100
set WTD DWS / 50 + 5 + random-float 5
set GW 50 + random-float 5.005 + (100 / wtd)
set pcolor red
]
]
setup-farmers
reset-ticks
end