Conways 社区中的扩展社区未读取自定义代理集
Extended neighborhood in Conways neighborhood not reading in custom agentset
我正在尝试编写康威生命游戏的一个版本,而不是查看相邻的 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 个活细胞,而不是细胞诞生,所有细胞都会死亡。
所有细胞都在死亡,因为它们不是在计算自己周围的活细胞,而是在原点周围。像这样编辑您的代码:
to go
let neighbors24 patches with [abs pxcor <= 2 and abs pycor <= 2]
type "Live patches near centre: " print count neighbors24 with [living?]
ask patches
...
我添加了一行打印出变量 live-neighbors 中的内容。
看看你是如何计算 neighbors24 的。您正在使用坐标值。所以这始终是围绕原点(在 0,0 处)最多 2 个补丁的补丁。正如我在对您上一个问题的评论中所述,您需要查看模型库中名为 "Moore & van Nuemann example" 的模型。只需搜索 "Moore"。其代码将使用补丁自身的坐标来居中邻域。
请注意,您还必须获得 ask patches
内的社区。
我根据来自 NetLogo 模型库的 Moore & Von-Naumann 邻域示例的一些输入,对您的 go
程序做了一些小的调整。有关调整的更多详细信息,请查看下面代码中的注释。
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% 的密度。
我正在尝试编写康威生命游戏的一个版本,而不是查看相邻的 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 个活细胞,而不是细胞诞生,所有细胞都会死亡。
所有细胞都在死亡,因为它们不是在计算自己周围的活细胞,而是在原点周围。像这样编辑您的代码:
to go
let neighbors24 patches with [abs pxcor <= 2 and abs pycor <= 2]
type "Live patches near centre: " print count neighbors24 with [living?]
ask patches
...
我添加了一行打印出变量 live-neighbors 中的内容。
看看你是如何计算 neighbors24 的。您正在使用坐标值。所以这始终是围绕原点(在 0,0 处)最多 2 个补丁的补丁。正如我在对您上一个问题的评论中所述,您需要查看模型库中名为 "Moore & van Nuemann example" 的模型。只需搜索 "Moore"。其代码将使用补丁自身的坐标来居中邻域。
请注意,您还必须获得 ask patches
内的社区。
我根据来自 NetLogo 模型库的 Moore & Von-Naumann 邻域示例的一些输入,对您的 go
程序做了一些小的调整。有关调整的更多详细信息,请查看下面代码中的注释。
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% 的密度。