首页 > 解决方案 > Conways 社区的扩展社区未在自定义代理集中读取

问题描述

我正在尝试编写一个版本的康威生命游戏,而不是查看相邻的 8 个单元格,而是查看相邻的 24 个单元格。(而不是中心周围的 1 个正方形,看 2)。

我遵循了一些建议并设置了一个“neighbors24”代理,它应该查看活细胞中的周围细胞。

patches-own [
  living?         ;; indicates if the cell is living
  live-neighbors  ;; counts how many neighboring cells are alive
]

to setup-blank
  clear-all
  ask patches [ cell-death ]
  reset-ticks
end

to setup-random
  clear-all
  ask patches
    [ ifelse random-float 100.0 < initial-density
      [ cell-birth ]
      [ cell-death ] ]
  reset-ticks
end


to cell-birth
  set living? true
  set pcolor fgcolor
end

to cell-death
  set living? false
  set pcolor bgcolor
end

to go
  let neighbors24 patches with [abs pxcor <= 2 and abs pycor <= 2]
  ask patches
    [ set live-neighbors count neighbors24 with [living?] ]
  ask patches
    [ ifelse live-neighbors = 3
      [ cell-birth ]
      [ if live-neighbors != 2
        [ cell-death ] ] ]
  tick
end

to draw-cells
  let erasing? [living?] of patch mouse-xcor mouse-ycor
  while [mouse-down?]
    [ ask patch mouse-xcor mouse-ycor
      [ ifelse erasing?
        [ cell-death ]
        [ cell-birth ] ]
      display ]
end

虽然代码编译正确,但它的行为完全不是我所期望的。例如,如果我在 24 个邻域半径内放置 3 个活细胞,而不是细胞出生,所有细胞都会死亡。

标签: netlogo

解决方案


go使用来自 NetLogo 模型库的 Moore & Von-Naumann 邻域示例的一些输入对您的程序进行了一些小的调整。有关调整的更多详细信息,请查看下面代码中的注释。

to go
  ;; creates a list with patches of the surrounding 24 patches 
  ;; with the caller included.
  let neighbors24 [list pxcor pycor] of patches with [abs pxcor <= 2 and abs pycor <= 2]

  ;; uncomment the line below, if you don´t want to consider the caller 
  ;; patch to adjust the neighbors24 set

  ;set neighbors24 remove [0 0] neighbors24

  ;; for illustration, shows the number of coordinates considered as neighbors
  ;show length neighbors24 

  ;; for illustration, shows the patch coordinates of the neighbors24 set
  ;show neighbors24 

  ask patches [ 
    ;; each patch checks the the "living" neighbors at the given coordinates (relative to this agent). 
    ;; Check documentation of "at-points"
    set live-neighbors count patches at-points neighbors24 with [living? = true]
  ]
  ask patches 
  [ ifelse live-neighbors = 3
    [ cell-birth ]
    [ if live-neighbors != 2
      [ cell-death ] ] ]

  tick
end

我没有对代码进行广泛的测试,但是对于低随机起始密度的活补丁(20-30%)来说似乎没问题。请查看 27% 密度的第一轮示例截图。

在此处输入图像描述

在此处输入图像描述

在此处输入图像描述


推荐阅读