如何在netlogo中随机生成不重叠/触摸的海龟

时间:2014-05-28 17:21:49

标签: loops netlogo

我正在建立一个停车模型,但在设置我的模型时遇到了麻烦。允许用户选择停放的汽车数量,我目前有程序在圆形随机x坐标上随机生成这些海龟(确保所有海龟只在一个空间/补丁上)。问题是多只乌龟可以位于同一个补丁上。有没有办法创建这些海龟,以便他们不会在同一个补丁上,而随机化x坐标?如果没有,我正在设想一个do-until类型的循环,以保持乌龟向前移动,直到乌龟打开的补丁没有其他乌龟。

如果循环是明确的解决方案,我从来没有在这个程序中使用过循环,并且真的会从看到一个例子中受益,但是如果这太过要求我确定我可以找到一个在线发布的某处。

    to setup-turtles 
  ask n-of 10 patches with [pycor = -1] [
    sprout 1
  ]
  print max [count turtles-here] of patches

  ask turtles with [who > 0]
    [set color blue
    set shape "car"
    set heading 90
    set xcor round random-xcor
    set ycor -1  ;; this ycor indicates that it is in the parking lane
    set pcolor red
    ]

2 个答案:

答案 0 :(得分:3)

另一种解决方法是使用n-of,例如制作20只乌龟,

ask n-of 20 patches with [pycor = 0] [
  sprout 1
]

例如,如果我们用以下方法测试它:

to test
  clear-all
  ask n-of 20 patches with [pycor = 0] [
    sprout 1
  ]
  print max [count turtles-here] of patches
end

打印的结果始终为1

答案 1 :(得分:2)

你最好的选择是使用海龟 - 这里的原始。如果补丁上没有其他海龟,请查看下面的内容,只会将海龟添加到pycor 1上的补丁中:

let new-patch one-of patches with [ (pycor = 1) and not any? turtles-here ]

if new-patch != nobody [
  ask new-patch [ sprout 1 ]
]

请参阅http://ccl.northwestern.edu/netlogo/docs/dictionary.html#turtles-here