【问题标题】:Specify neighbor in specific direction指定特定方向的邻居
【发布时间】:2020-10-24 16:40:41
【问题描述】:

我正在尝试为海龟创建一个移动到相邻补丁的速率模型。最终,我希望海龟在特定条件下孵化 8 只潜在的其他海龟,每只海龟根据它们的方向专门前往它们的邻居之一。基本上我想将邻居补丁指定为北邻居、南邻居、东邻居、西邻居、西北邻居等。从那时起,我希望创建每只海龟以移动到特定的补丁。每个补丁变量有 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

【问题讨论】:

    标签: netlogo patch direction rate


    【解决方案1】:

    您的代码中存在一些拼写错误,我假设是针对您的 StackOverflow 问题复制/粘贴和更新它。我尽力解决这些问题。

    我花了几次阅读您的描述和代码,但这是我认为您想要实现的算法:

    • 位于斑块中心的乌龟选择其北斑块邻居作为新目标
    • 如果当前补丁的I-B-N 小于heat-sink-N,我们将孵化一只新的海龟,朝着那个目标前进。
    • 幼体以目标的ROM-N 确定的速度向目标前进。

    我在代码中看到的最大问题是您使用while 来控制海龟的运动。在 NetLogo 中,时间的概念是基于滴答声,如果您希望乌龟一次缓慢地向目标移动一点,您希望它在每个滴答声中移动一点,而不是在 for 循环中一次全部移动.

    第二点是我认为你对目标的处理有点混乱,所以我想让它更清晰、更明确。因此,我添加了一个turtles-own 变量goal 来保存目标,以便我们可以在刻度之间使用它。我还对补丁的ROM-NI-b-Nhead-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-bheat-sink - 这对我来说似乎很奇怪。但是,如果您无法完成这项工作,最好在此提出第二个问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-02
      相关资源
      最近更新 更多