如何将这里的补丁添加到 netlogo 的列表中?
how to add patch-here to a list in netlogo?
或者如何将坐标添加到补丁集?
我想创建一个列表并将访问的每个补丁添加为列表的另一个元素(使用 lput)但是列表每次都会替换补丁,因此我尝试使用补丁集,但是我不知道如何添加在它的末尾添加新的补丁并在那里添加坐标。
到目前为止,这是我的代码:
globals [ frontier frontier2 ]
breed [squares square]
breed [circles circle]
to setup
ca
set-default-shape squares "square"
set-default-shape circles "circle"
create-breeds
reset-ticks
end
to create-breeds
create-squares 1 [
setxy 0 0 ]
create-circles 1 [
setxy 5 5 ]
end
to go
ask squares [
fd 1
set frontier []
set frontier lput patch-here frontier
show frontier
]
ask circles [
fd 1
set frontier2 patch-set patch-here
show frontier2
]
tick
end
这就是指挥中心显示的内容:
"(方块 0): [(补丁 7 -3)]
(圆圈 1):(代理集,1 个补丁)
我期待的是 (square 0): [(patch 0 0) (patch 1 0) (patch 2 0)]
它每次都会替换补丁,因为您每次在 go
过程中都明确地重新初始化列表:
set frontier []
只需将该行移到您的 setup
过程中即可!
至于将补丁添加到补丁集而不是列表,正确的语法应该是:
set frontier2 (patch-set frontier2 patch-here)
但我不建议你使用它,因为它每次都需要重建补丁集,因此比使用列表和 lput
慢。此外,补丁集总是以随机顺序访问,我怀疑这不是你想要的。
或者如何将坐标添加到补丁集? 我想创建一个列表并将访问的每个补丁添加为列表的另一个元素(使用 lput)但是列表每次都会替换补丁,因此我尝试使用补丁集,但是我不知道如何添加在它的末尾添加新的补丁并在那里添加坐标。
到目前为止,这是我的代码:
globals [ frontier frontier2 ]
breed [squares square]
breed [circles circle]
to setup
ca
set-default-shape squares "square"
set-default-shape circles "circle"
create-breeds
reset-ticks
end
to create-breeds
create-squares 1 [
setxy 0 0 ]
create-circles 1 [
setxy 5 5 ]
end
to go
ask squares [
fd 1
set frontier []
set frontier lput patch-here frontier
show frontier
]
ask circles [
fd 1
set frontier2 patch-set patch-here
show frontier2
]
tick
end
这就是指挥中心显示的内容:
"(方块 0): [(补丁 7 -3)]
(圆圈 1):(代理集,1 个补丁)
我期待的是 (square 0): [(patch 0 0) (patch 1 0) (patch 2 0)]
它每次都会替换补丁,因为您每次在 go
过程中都明确地重新初始化列表:
set frontier []
只需将该行移到您的 setup
过程中即可!
至于将补丁添加到补丁集而不是列表,正确的语法应该是:
set frontier2 (patch-set frontier2 patch-here)
但我不建议你使用它,因为它每次都需要重建补丁集,因此比使用列表和 lput
慢。此外,补丁集总是以随机顺序访问,我怀疑这不是你想要的。