简体   繁体   English

乌龟均匀分布

[英]Uniform distribution of turtles

I am attempting to create a uniform distribution of turtles in a world with max-pxcor & max-pycor = 49, with 0,0 at bottom left hand corner, in a world that does not wrap.我正在尝试在 max-pxcor 和 max-pycor = 49 的世界中创建海龟的均匀分布,左下角为 0,0,在一个不环绕的世界中。 I have used code from the Virtual Lab ( https://virtualbiologylab.org/population-ecology/ ) MarkRecpature model, to create the uniform distribution, but I am running into problems that I don't fully understand.我使用了虚拟实验室 ( https://virtualbiologylab.org/population-ecology/ ) MarkRecpature model 中的代码来创建统一分布,但我遇到了一些我不完全理解的问题。 (My NetLogo knowledge would not be good enough to attempt to create my own code for uniform distribution of points:) The model stops with a Runtime Error: (我的 NetLogo 知识不足以尝试创建我自己的代码来统一分配点:)model 因运行时错误而停止:

 “ASK expected input to be an agent or agentset but got nobody instead” 

relating to: 
“ask patch x y” part of the code. 

Some of the turtles have been setup, but not all and I cannot figure what the error is, as when I inspect the turtle creating the error, it has a counter & counter2 value that falls within the world dimensions, and from this code I would expect the patch with counter & counter2 as x,y coordinates also falls within the world.已经设置了一些海龟,但不是全部,我不知道错误是什么,因为当我检查产生错误的海龟时,它有一个 counter 和 counter2 值落在世界维度内,从这段代码我会期望带有 counter 和 counter2 作为 x,y 坐标的补丁也落在世界中。 So there is something about this piece of code that I do not fully understand.所以这段代码有些地方我没有完全理解。 Can anyone help?有人可以帮忙吗? Thanks, Aine谢谢,爱音

turtles-own [counter counter2 home_x home_y]

to setup
clear-all                               
reset-ticks                             
  
create-turtles N-turtles                  ;;create N turtles based on slider

end
  
to go
  
    ask turtles
[
    set counter max-pxcor  / sqrt N-turtles  / 2
set counter2 max-pycor / sqrt N-turtles  / 2
repeat N-turtles
[
let x (counter + random 5 - random 5)
let y (counter2 + random 5 - random 5)
ask patch x y
[
    sprout 1
    [
    set home_x x
    set home_y y
    ]
]
set counter counter +  max-pxcor  / sqrt N-turtles
if counter >= max-pxcor
[
    set counter max-pxcor  / sqrt N-turtles  / 2
    set counter2  counter2 + max-pycor  / sqrt N-turtles
    ]]]
  
end

I was able to reproduce a crash in two distinct scenarios.我能够在两个不同的场景中重现崩溃。 To find out where exactly the error lies, it helps to add in some extra commands to your code that provide output:要找出错误的确切位置,可以在代码中添加一些额外的命令来提供 output:

  let x (counter + random 5 - random 5)
show word "x: " x
  let y (counter2 + random 5 - random 5)
show word "y: " y
  ask patch x y [...]

This showed me that the error mostly occurs when your Y goes over 49, at which point there is no longer a patch that corresponds to the coordinates.这告诉我错误主要发生在你的 Y 超过 49 时,此时不再有对应于坐标的补丁。 You already fixed this problem for the X coordinate yourself.您已经为 X 坐标自己解决了这个问题。 Repurposing your solution, by adding if counter2 >= max-pycor [set counter2 max-pycor / sqrt N-turtles / 2] near the end of your code this problem is solved.通过在代码末尾附近添加if counter2 >= max-pycor [set counter2 max-pycor / sqrt N-turtles / 2]来重新调整您的解决方案,此问题已解决。

The second scenario where I got a crash was when either X or Y falls below 0. This can happen when you have a high N-turtles, so that your max-pxcor / sqrt N-turtles / 2 is lower than 5. If this happens, let x (counter + random 5 - random 5) might occasionally return a negative value.我崩溃的第二种情况是当 X 或 Y 低于 0 时。这可能发生在你有一个高 N-turtles 时,所以你的max-pxcor / sqrt N-turtles / 2低于 5。如果这个碰巧, let x (counter + random 5 - random 5)可能偶尔会返回一个负值。 I suggest having the random number here scale with your world size and turtle count as well.我建议让这里的随机数与你的世界大小和海龟数量成比例。 (I am not knowledgeable enough about uniform distributions to advise the best option here) (我对均匀分布的了解不够,无法在此处建议最佳选择)

The original model didn't have to worry about any of these since there, world wrapping is turned on.原来的 model 不必担心这些,因为那里打开了世界环绕。

Finally, although you didn't specifically ask for it, your code would generate (N-turtles^2 + N-turtles) turtles, not N-turtles, since you first generate N-turtles and then let every single one of them generate another N-turtles.最后,虽然你没有特别要求它,但你的代码会生成 (N-turtles^2 + N-turtles) 海龟,而不是 N-turtles,因为你首先生成 N-turtles 然后让它们中的每一个生成另一个N-海龟。 I removed your first turtle generation, and moved the counters to a be global variables.我删除了你的第一代乌龟,并将计数器移动到一个全局变量中。

turtles-own [home_x home_y]
globals [counter counter2]

to setup
clear-all                               
reset-ticks                             
end
  
to go

  let distance-x max-pxcor  / sqrt N-turtles
  let distance-y max-pycor / sqrt N-turtles
  
  set counter distance-x / 2
  set counter2 distance-y / 2
  repeat N-turtles [
    let x (counter + random distance-x / 2 - random distance-x / 2)
    show word "x: " x
    let y (counter2 + random distance-y / 2 - random distance-y / 2)
    show word "y: " y
    
    ask patch x y [
      sprout 1 [
        set home_x x
        set home_y y
      ]
    ]
    
    set counter counter + distance-x
    if counter >= max-pxcor [
      set counter distance-x / 2
      set counter2  counter2 + distance-y
      if counter2 >= max-pycor [
        set counter2 distance-y / 2
      ]
    ]
  ]
  
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM