首页 > 解决方案 > 在 NetLogo 中限制海龟移动到边界

问题描述

在 NetLogo 中,我正在寻找

目前,我让它们在质心处发芽,但很快它们就遍布整个地图。如何将 shapefile 合并到海龟的运动中?理想情况下,它们会稍微超出边界,但我确信在我限制它们之后很容易操纵。海龟需要能够跨越国界相互交互,而不是穿越整个地图。

请参阅此图像以供参考: MSOA 文件

一个单独的解决方案可能是将它们限制在它们发芽的半径范围内,但也不确定如何做到这一点。

我考虑过:

标签: gisnetlogoshapefile

解决方案


你想要做的是给补丁两个变量:

  1. 一个标识它们属于哪个区域(假设您导入了 shapefile,我假设这已经存在于您的代码中);
  2. 另一个标识补丁自己的区域+区域足够近,以至于来自另一个区域的乌龟可能会移动到那里。

我将调用第一个变量region,它将是一个整数,第二个变量allowed,它将是一个整数列表。

这个想法是海龟,每一个都有自己的my-region价值,基于它的起源,将查看allowed补丁变量(而不是 at region)来查看它们可以移动的位置。这样,每只海龟将能够移动到属于它自己区域的任何补丁 + 到足够靠近其区域的那些补丁(根据您指定的值,这里我称之为buffer-range)。

这就是逻辑。

下面的代码实现了它,相关部分是to create-buffers- 我在代码中评论过。然后,to go使用这些新信息来确定海龟可以移动的潜在斑块,包括那些在其区域之外但足够近的斑块(你没有分享你的乌龟是如何移动的,但是应该很容易将相同的条件应用于你的任何程序实施)。

; Untick the 'World wraps horizontally' box in Interface > Settings.

globals [
 buffer-range
]

patches-own [
 region
 allowed
]

turtles-own [
 my-region 
]

to setup
  clear-all
  create-regions
  create-buffers
  populate-world
end

to create-regions
  ask patches [
   ifelse (pxcor < 0)
    [set region 1
     set allowed (list region)
     set pcolor 43]
    [set region 2
     set allowed (list region)
     set pcolor 63]
  ]
end

to create-buffers
  set buffer-range 3
  
; First, each patch targets the patches that belong to another region which is near enough according to
; the buffer value, and it also creates a local empty list that will be used by those patches.
; Then the patch ask those target patches, if any, to check if their region has not been recorded already
; as a region allowed for movement or as such a candidate. If that's the case, that region is added to
; the allowed-candidates list.
  ask patches [
    let target-patches (patches with [region != [region] of myself] in-radius buffer-range)
    let allowed-candidates (list)
    
    if (any? target-patches) [
      ask target-patches [
       if (not member? region [allowed] of myself) and (not member? region allowed-candidates) [
         set allowed-candidates (lput region allowed-candidates)
        ]
      ]
    ]
    
; Now, each element of the allowed-candidates list is added to the allowed list.
    foreach allowed-candidates [x -> set allowed (lput x allowed)]
  ]
end
  
to populate-world
  create-turtles 10 [
    setxy random-xcor random-ycor
    set my-region region
    set color pcolor + 2
  ]
end

to go
  ask turtles [
    move-to one-of patches with [member? [my-region] of myself allowed]
  ]
end

对于未来的问题,请分享您拥有的/您所做的(查看此处此处)!理想情况下,您应该提供一个问题示例,就像我的答案的代码部分一样:您可以将其复制粘贴到 NetLogo 中,并且您有一个可行的示例。


推荐阅读