如何根据距离标准创建给定数量的海龟

How to create a given number of turtles based on a distance criterion

我们正在创建代码,但在执行以下操作时遇到问题:

我们想根据以下条件创建特定数量的海龟(例如 100 只海龟):

  1. 代理之间的距离必须大于或等于 2

我们已经尝试过:

to setup    
ask n-of 100 patches [
        if not any? turtles in-radius 2 [      
    sprout-turtles 1 [ setup-turtles ] ]
        ]
      ]
end

to setup-turtles
set energy 0
set size 1
set color orange
end

它没有用,因为即使世界拥有所需数量的代理人,也只有不到 100 个代理人诞生,在这种情况下是 100

有人对我们如何解决这个问题有什么建议吗?

提前致谢

您概述的方法 运行 会遇到问题,因为您要求补丁在海龟满足某些条件时发芽。因为补丁以随机顺序运行,所以您选择的一些补丁在轮到它们行动时不再满足 sprout 的条件,因为附近的其他补丁已经在附近发芽了一只海龟。

一种选择是使用 while 循环不断尝试发芽海龟,直到达到所需的数量:

to setup
  ca
  while [ count turtles < 100 ] [
    ask one-of patches with [ not any? turtles in-radius 2 ] [
      sprout 1    
    ]
  ]
  reset-ticks
end

小心 while 循环 - 如果您不编写代码以使 while 条件最终变为假,您的模型将 运行 永远卡住在循环中。

如果失败会给出更明确的错误的另一个选项是只创建您的海龟数量,然后让它们移动到满足条件的space:

to setup-2
  ca
  crt 100 [
    move-to one-of patches with [ not any? turtles in-radius 2 ] 
  ]
  reset-ticks
end