如何解决指定特定方向的邻居
我正在尝试为乌龟到相邻斑块的运动速率模型创建一个模型。最终,我希望乌龟在一定条件下孵化出8只潜在的其他乌龟,每只乌龟都根据其方向专门行进到其邻居之一。基本上,我想将邻居补丁指定为北邻居,南邻居,东邻居,西邻居,西北邻居,等等。从那时起,我希望创建每个乌龟以移动到特定补丁。每个补丁变量具有8个移动速率:向北移动的速率,向南移动的速率等等。因此,我希望这八只海龟以其邻近斑块的速率移动到其特定斑块。在到达其北邻居的中心之后,我希望重置北邻居变量,以使新的北邻居成为旧北邻居的北邻居。这是仅指定北邻的代码示例(我计划为北邻,南邻等设置单独的过程。
patches-own[I-b-N heat-sink-N ROM-N ROM-S]
to setup[
ca
reset-ticks
crt 1 [
set shape "triangle"
set color red
]
ask patches[
set pcolor green
set ROM-N 5
set ROM-S 6 ;here there would be ROS-N ROS-S ROS-E ... for each patch
set I-b-N 8
set heat-sink-N 2
]
]
to go
spread-north
;spread-south
tick
end
to spread-north ;duplicate and modify the same procedure for south,east,west,...
ask turtles [
if xcor = [pxcor] of patch-here and ycor = [pycor] of patch-here[
set N-neighbor [patch-at-heading-and-distance 0 1] of self]
]
ask turtles[ ;I use two ask turtles to make sure N-neighbor is created at the beginning of the procedure
let goal N-neighbor ;here to store N-neighbor
let parent patch-here
if [I-b-N] of parent > [heat-sink-N] of goal[ ;If my patch's north-facing I-b > north heat-sink of north neighbor
hatch 1 [
face goal ;make sure it is facing towards north neighbor
while [xcor != [pxcor] of goal and ycor != [pycor] of goal][
fd 1 / ((ROM-N + [ROM-N] of goal) / 2) ;moves forward a fraction of the distance between patch-here and north neighbor until it has reached the center coordinates of north neighbor
]
set pcolor black
]
end
解决方法
您的代码中有一些错别字,我认为是出于复制/粘贴和更新您的StackOverflow问题的目的。我试图尽力解决这些问题。
这花了我一些您的描述和代码的通读,但这是我认为您要实现的算法:
- 位于斑块中心的乌龟将其北部斑块作为新目标
- 如果当前补丁的
I-B-N
小于heat-sink-N
,我们将孵化出一只新的乌龟以实现该目标。 - 孵化器以该目标的
ROM-N
确定的比率进入目标。
我在代码中看到的最大问题是您使用while
控制乌龟的移动。在NetLogo中,时间的概念是基于刻度的,如果您希望乌龟一次向目标缓慢移动一点,则希望它为每个刻度移动一点点,而不是一次在for循环中一次全部移动
第二点是,我认为您对目标的处理有些混乱,因此我想使其更加清晰明了。这样,我添加了一个turtles-own
变量goal
来容纳目标,因此我们可以在刻度之间使用它。我还随机化了补丁的ROM-N
,I-b-N
和head-sink-N
值,以便在每次测试中获得不同的结果。
另一个问题是您的运动功能可能会使海龟超过其期望的目标,因此我为此添加了一个检查。
这是代码。如果运行它,您将看到至少一个孵化场产卵并移至北面的下一个斑块,然后可能会重复此过程,具体取决于我生成的用于填充测试环境的随机数据。我希望这是有道理的并且对您有所帮助。
turtles-own [ goal ]
patches-own [ I-b-N heat-sink-N ROM-N ]
to setup
clear-all
reset-ticks
create-turtles 1 [
; easier for me to see the direction of the default arrow shape
; set shape "triangle"
set color red
setxy 0 0
set heading 0
set goal patch 0 0 ; the first turtle needs its goal set so it'll spawn immediately
]
ask patches [
set pcolor green
set ROM-N random 10
set I-b-N random 10
set heat-sink-N random 10
]
; this is required to make sure we always see at least one move
ask patch 0 0 [ set I-b-N 10 ]
ask patch 1 0 [ set heat-sink-N 0 ]
end
to go
; since `spread-north` asks the same group of turtles that we move below we could probably merge
; this with that and make it a turtle procedure,but I'll leave it as is for now
spread-north
ask turtles with [goal != nobody] [
; moves forward a fraction of the distance between patch-here and North neighbor until it has reached the center coordinates of North neighbor
let max-move 1 / ((ROM-N + [ROM-N] of goal) / 2)
fd min list max-move (distance goal)
]
tick
end
to spread-north
; you didn't say what to do with turtles that were finished,so I'll just skip them
ask turtles with [goal != nobody] [
; make sure to check against the final destination,and not the patch here
; (we might not be on the goal patch yet)
if xcor = [pxcor] of goal and ycor = [pycor] of goal [
; instead of setting our goal for our hatchlings,let's just clear our goal
; and then handle our hatching
set goal nobody
set pcolor black
let maybe-goal patch-at 0 1
; If my patch's North-facing I-b > North heat-sink of North neighbor
if [I-b-N] of patch-here > [heat-sink-N] of maybe-goal [
hatch 1 [
; all we do for the hatchlings is point them at a goal
set goal maybe-goal
; make sure it is facing towards the goal and ready to move on the next tick
face goal
]
]
]
]
end
您还提到复制/粘贴其他方向的逻辑。我认为您可以将其概括化以处理目标,而无需过多麻烦。我不清楚您是否真的需要每个补丁在每个方向上都有不同的I-b
和heat-sink
,这对我来说似乎很奇怪。但是,如果您在完成该操作时遇到困难,那么这里的第二个问题就很好了。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。