首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >指定特定方向的邻居

指定特定方向的邻居
EN

Stack Overflow用户
提问于 2020-10-25 00:40:41
回答 1查看 105关注 0票数 2

我正在尝试创建一个乌龟到相邻补丁的移动速率模型。最终,我希望这只海龟,在一定的条件下,孵化出8只潜在的其他海龟,每只海龟都可以根据它们的方向专门前往它们的邻居之一。基本上,我希望将邻居补丁指定为北邻居、南邻居、东邻居、西邻居、西北邻居,等等。从那时起,我想让每只海龟移动到一个特定的补丁。每个补丁变量有8个移动速率:向北移动的速率,向南移动的速率,等等。所以我想让这8只乌龟以它们邻居的速度移动到它们特定的补丁上。在它到达它的North邻居的中心之后,我想要重置North neighbor变量,以便新的North邻居成为旧的North邻居的North邻居。这是一个仅指定北邻居的代码示例(我计划对北邻居、南邻居等有单独的过程。

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

发布于 2020-10-31 04:27:58

你的代码中有一些拼写错误,我假设是从复制/粘贴和更新它来回答你的StackOverflow问题的。我试着尽我所能解决这些问题。

我通读了几遍你的描述和代码,但我认为这是你想要实现的算法:

  • 位于补丁中心的海龟选择它的北边补丁邻居作为它的新目标
  • 如果当前补丁的I-B-N小于heat-sink-N,我们就孵化出一只新的乌龟朝向那个目标。
  • 幼龟以该目标的ROM-N.

确定的速率前进到目标

我在代码中看到的最大的问题是您使用while来控制乌龟的移动。在NetLogo中,时间的概念是基于节拍的,如果你想让一只乌龟一次缓慢地朝着一个目标移动一点,你就希望它为每个节拍移动一点,而不是在for循环中一次移动一点。

第二点是,我认为你对目标的处理有点混乱,所以我想让它更清晰和明确。因此,我添加了一个turtles-own变量goal来保存目标,这样我们就可以在节拍之间使用它。我还对补丁的ROM-NI-b-Nhead-sink-N值进行了随机化,以便我们可以在每次运行测试时获得不同的结果。

另一个问题是,你的移动功能可能会让海龟超过它们想要的目标,所以我为此添加了一个检查。

以下是代码。如果你运行它,你会看到至少一只孵化的幼鸟,并移动到北边的下一个补丁,然后可能会根据我生成的随机数据重复这个过程,以填充整个世界进行测试。我希望它是有意义的,并且是有帮助的。

代码语言:javascript
复制
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-bheat-sink --这对我来说似乎很奇怪。但是如果你很难做到这一点,在这里作为第二个问题是很好的。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64515740

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档