netlogo - 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 个活细胞,而不是细胞出生,所有细胞都会死亡。
解决方案
我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% 密度的第一轮示例截图。
推荐阅读
- r - 无法使用 rsconnect 验证 r shinyapps 帐户
- c++ - 一个普通的链表
- ios - 如何将 NSString 转换为具有以下格式的 NSDate:
- regex - Spark-SQL 是否支持使用 regex 规范的 Hive Select All Query with except Columns
- c# - 自定义集合在 Template10 中不起作用,但在空白项目中起作用
- php - 在控制器中检索单个 FormType 的值
- c# - 捕获 C# winform 应用程序中的所有异常
- grammar - 在 Bison(或 yacc )中,是否存在由语法定义的顺序?
- c# - 在实体框架核心中获取无效的列名'EmploymentTypeEntityEmploymentTypeID
- python - Web 服务器可以从脚本中检测登录吗?