【问题标题】:Divide regions accordingly to physical features根据物理特征划分区域
【发布时间】:2020-03-11 16:02:49
【问题描述】:

我正在做一个较小的项目,但遇到了一个问题,我不确定是否可以在 NetLogo 中解决它,但我想试试 StackOverflow!

我有一个模型,它将世界划分为不同的部分,并随机添加物理特征(例如河流)。如果一个特征穿过整个区域,我希望它把区域分开并分成两个区域。例如,在下图中,我想根据物理特征(黑色)将紫色区域分成两个独特的区域。

我用来生成上面图片的代码,可以在下面找到。

to setup
  ca

  ;Setting world.
  resize-world 0 19 0 19

  ;Creating regions. 
  let x 5
  let y 5
  let col 45
  while [y <= max-pycor + 1][
    while [x <= max-pxcor + 1 ][
      ask patches with [pxcor < x and pxcor >= x - 5 and pycor < y and pycor >= y - 5][
        set pcolor col
      ]
      set x x + 5
      set col col + 10
    ]
    set x 5
    set y y + 5
  ]

  ;Generating physical features.    
  ask n-of 5 patches[ sprout 1[
    set pcolor black]
  ]

  let i 0
  while [ i < (max-pycor * 2 )][
    ask turtles [
      fd 1
      set pcolor black
      ifelse (random 20 <= 1)
      [
        rt one-of [-90 0 90]      
        forward 1
      ]
            [
        fd 1
        set pcolor black
        fd 1
        set pcolor black
      ]
      set pcolor black
      set i i + 1]
  ]

  ask turtles [die]
end 

【问题讨论】:

    标签: netlogo divide agent-based-modeling


    【解决方案1】:

    我处理这个问题的策略是意识到我们真正需要做的就是用颜色“泛滥”一个补丁并标记所有找到的相邻补丁,然后重复任何未标记的非黑色补丁,直到它们全部完成。

    NetLogo 没有“洪水”命令来获取与符合标准的补丁相邻的所有补丁,因此我们自己制作了一个特殊的报告器来处理它,patches-adjacent。然后很容易让那些patches-adjacent 将他们的region 设置为当前选择的区域。

    我不喜欢这段代码,它有点挑剔,如果调整不正确,很容易出现无限循环,但它应该可以工作。我敢打赌,有一种更简洁的方法可以做到这一点,而我目前还没有想到。

    ; add a variable to track the different regions
    ; the default value will be `0` for each patch when `clear-all` is called
    patches-own [ region ]
    
    to set-regions 
      let current-region 1
      ; only act on non-black patches that haven't yet been assigned a region
      let untagged patches with [ region = 0 and pcolor != black ]
      while [any? untagged] [
        ask one-of untagged [
          ask patches-adjacent [
            set region current-region 
          ]
        ]
        ; update the region and the untagged patches we have left to process
        set current-region current-region + 1 
        set untagged patches with [ region = 0 and pcolor != black ]
      ]
      ; this is just to get a view of the regions to quickly see if our code worked, it can be removed
      ask patches [ set plabel region ]
    end
    
    to-report patches-adjacent
      report patches-adjacent-ex (patch-set self) pcolor
    end
    
    to-report patches-adjacent-ex [found pc]
      let newly-found neighbors4 with [ (not member? self found) and pcolor = pc and region = 0 and pcolor != black ]
      set found (patch-set found newly-found)
      ask newly-found [
        ; use recursion to find the patches adjacent to each newly-found one
        ; relying on updating the `found` agentset as we go to avoid duplicates
        ; or looping forwarder
        set found (patches-adjacent-ex found pc) 
      ]
      report found
    end
    

    【讨论】:

    • 同样的方法,也可以试试NetLogo模型库中的Patch Clusters模型
    • 非常感谢@Jasper,看起来很有希望。但是,我在理解您的代码时遇到了一些问题,并且会产生错误。最后,以下代码行 "set found (patches-adjacent-ex found)" 给了我以下错误:"PATCHES-ADJACENT-EX expected 2 inputs" 我应该添加的第二个输入是什么?
    • @JenB 谢谢,让它与 Patch Cluster 模型一起工作。
    【解决方案2】:

    我通过使用可在 NetLogo 模型库中找到的 Patch Clusters 模型解决了这个问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-01-21
      • 2014-09-21
      • 2015-05-05
      • 2021-04-09
      • 2021-01-22
      • 2018-05-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多