Netlogo:识别扩展邻居
Netlogo: identifying expanding neighbors
我正在努力编写代码,根据补丁的属性识别不断扩大的社区。我需要能够首先识别出有海龟存在的补丁,然后确定 4 个邻居的补丁是否有乌龟,然后邻居的邻居是否有乌龟,等等......直到达到补丁的阈值。我真的很感激任何 guidance/help.
这是我现在的代码:
'Clusters' 是代表有海龟的补丁的代理集
'Cluster-ring'表示与原始补丁的距离(以相邻补丁的环表示)
Cluster-ID是簇中心补丁的id号。
ask clusters [if any? turtles-here
[ask neighbors4
[set pcolor orange - 3
set cluster? TRUE
set cluster-ID [cluster-ID] of myself
set cluster-ring 2
]]]
ask clusters with [cluster-ring = 2][if any? turtles-here
[ask neighbors4
[set pcolor orange - 5
set cluster? TRUE
set cluster-ID [cluster-ID] of myself
set cluster-ring [cluster-ring] of myself + 1
]]]
我不确定您的设置是什么样的,但我认为这是一种可以满足您需要的方法。它的工作原理如下(一旦世界上到处都是海龟):
- 随机 select 一些有海龟存在的补丁也有
neighbors4
也有海龟存在(这里的过程名称 cluster-node-find
)。在此示例中,我将它们称为节点,并随机选择从 5 开始。接下来,要求这些节点:
- 将
cluster?
设置为true
- 随机选择一个
cluster-ID
- 将它们的
cluster-ring
值设置为 0(因为它们是环的 "center")
选择节点后,使用每次调用时外环增加一个的过程来增加集群(我在这里称之为 build-cluster
)。因此,询问任何 cluster?
设置为 true
且没有任何 neighbors4
且没有乌龟的补丁:
- 请任何
neighbors4
尚未属于集群的成员:
- 将
cluster?
设置为true
- 接受
cluster-ID
的提问补丁
- 将它们的
cluster-ring
值设置为请求补丁的 cluster-ID
+ 1
重复运行那个build-cluster
程序来观察你的集群向外生长,在海龟存在的空隙处停止。
显然,您可能需要根据最初设置集群的方式对其进行修改,但它可能会让您指明如何向外构建的正确轨道。请参阅下面的完整程序,了解其工作原理 - 使用 setup
按钮调用此块:
patches-own [ cluster? cluster-ID cluster-ring ]
to setup
ca
ask patches [
set pcolor 103
set cluster? false
set cluster-ID -999
set cluster-ring -999
]
ask n-of ( round count patches * 0.75) patches [
sprout 1 [
set shape "circle"
set size 0.5
set color white
]
]
cluster-node-find
reset-ticks
end
to cluster-node-find
let potential-clusters n-of 5 patches with [
cluster? = false and
any? turtles-here and
not any? neighbors4 with [ not any? turtles-here ]
]
ask potential-clusters [
set pcolor red + 4
set cluster? true
set cluster-ID random 10000
set cluster-ring 0
ask turtles-here [
set color black
]
]
end
然后,如果您想看到它们长到最后,请为此 build-cluster
制作一个永久按钮:
to build-cluster
let cluster-members patches with [
cluster? and
not any? neighbors4 with [ not any? turtles-here ]
]
ask cluster-members [
ask neighbors4 with [ cluster? = false and any? turtles-here ] [
set pcolor [pcolor] of myself - 0.25
set cluster? true
set cluster-ID [cluster-ID] of myself
set cluster-ring ( [cluster-ring] of myself + 1 )
]
]
tick
end
编辑:
根据下面评论中的问题:
这是输出:
let cluster-members patches with [
cluster? and
not any? neighbors4 with [ not any? turtles-here ]
]
在 build-cluster
过程中。
如果您改用:
let cluster-members patches with [
cluster? and
any? neighbors4 with [ any? turtles-here ]
]
集群需要通过其 neighbors4
上存在海龟的补丁扩展其前沿时没有限制。因此,集群进一步扩展,唯一的差距将是不存在海龟的地方(假设海龟密度足够高):
我正在努力编写代码,根据补丁的属性识别不断扩大的社区。我需要能够首先识别出有海龟存在的补丁,然后确定 4 个邻居的补丁是否有乌龟,然后邻居的邻居是否有乌龟,等等......直到达到补丁的阈值。我真的很感激任何 guidance/help.
这是我现在的代码:
'Clusters' 是代表有海龟的补丁的代理集 'Cluster-ring'表示与原始补丁的距离(以相邻补丁的环表示) Cluster-ID是簇中心补丁的id号。
ask clusters [if any? turtles-here
[ask neighbors4
[set pcolor orange - 3
set cluster? TRUE
set cluster-ID [cluster-ID] of myself
set cluster-ring 2
]]]
ask clusters with [cluster-ring = 2][if any? turtles-here
[ask neighbors4
[set pcolor orange - 5
set cluster? TRUE
set cluster-ID [cluster-ID] of myself
set cluster-ring [cluster-ring] of myself + 1
]]]
我不确定您的设置是什么样的,但我认为这是一种可以满足您需要的方法。它的工作原理如下(一旦世界上到处都是海龟):
- 随机 select 一些有海龟存在的补丁也有
neighbors4
也有海龟存在(这里的过程名称cluster-node-find
)。在此示例中,我将它们称为节点,并随机选择从 5 开始。接下来,要求这些节点:- 将
cluster?
设置为true
- 随机选择一个
cluster-ID
- 将它们的
cluster-ring
值设置为 0(因为它们是环的 "center")
- 将
选择节点后,使用每次调用时外环增加一个的过程来增加集群(我在这里称之为
build-cluster
)。因此,询问任何cluster?
设置为true
且没有任何neighbors4
且没有乌龟的补丁:- 请任何
neighbors4
尚未属于集群的成员: - 将
cluster?
设置为true
- 接受
cluster-ID
的提问补丁 - 将它们的
cluster-ring
值设置为请求补丁的cluster-ID
+ 1
- 请任何
重复运行那个
build-cluster
程序来观察你的集群向外生长,在海龟存在的空隙处停止。
显然,您可能需要根据最初设置集群的方式对其进行修改,但它可能会让您指明如何向外构建的正确轨道。请参阅下面的完整程序,了解其工作原理 - 使用 setup
按钮调用此块:
patches-own [ cluster? cluster-ID cluster-ring ]
to setup
ca
ask patches [
set pcolor 103
set cluster? false
set cluster-ID -999
set cluster-ring -999
]
ask n-of ( round count patches * 0.75) patches [
sprout 1 [
set shape "circle"
set size 0.5
set color white
]
]
cluster-node-find
reset-ticks
end
to cluster-node-find
let potential-clusters n-of 5 patches with [
cluster? = false and
any? turtles-here and
not any? neighbors4 with [ not any? turtles-here ]
]
ask potential-clusters [
set pcolor red + 4
set cluster? true
set cluster-ID random 10000
set cluster-ring 0
ask turtles-here [
set color black
]
]
end
然后,如果您想看到它们长到最后,请为此 build-cluster
制作一个永久按钮:
to build-cluster
let cluster-members patches with [
cluster? and
not any? neighbors4 with [ not any? turtles-here ]
]
ask cluster-members [
ask neighbors4 with [ cluster? = false and any? turtles-here ] [
set pcolor [pcolor] of myself - 0.25
set cluster? true
set cluster-ID [cluster-ID] of myself
set cluster-ring ( [cluster-ring] of myself + 1 )
]
]
tick
end
编辑:
根据下面评论中的问题:
这是输出:
let cluster-members patches with [
cluster? and
not any? neighbors4 with [ not any? turtles-here ]
]
在 build-cluster
过程中。
如果您改用:
let cluster-members patches with [
cluster? and
any? neighbors4 with [ any? turtles-here ]
]
集群需要通过其 neighbors4
上存在海龟的补丁扩展其前沿时没有限制。因此,集群进一步扩展,唯一的差距将是不存在海龟的地方(假设海龟密度足够高):