将 Turtle 变量除以给出错误的 Patch 变量

Dividing a Turtle variable by Patch variable giving Error

我正在写一些代码。海龟和补丁被设置为具有值。 None 其中的值为零。我试图用农民周围的 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

看起来这只是您 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