乌龟之间的最小距离 [英] Minimum distance between turtles

查看:67
本文介绍了乌龟之间的最小距离的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写计算乌龟之间距离的代码.如果彼此距离太近,则需要移开;如果彼此距离太远,则需要移开.

I am trying to write code that calculates the distance between turtles. They need to move away if too close to each other and move closer if too far apart.

它们的距离不能小于一个补丁的1/2,并且不能超过一个补丁.如果它们之间的距离不介于1/2和1之间,则它们需要移动直到它们在此范围内.

They can be no closer than 1/2 of a patch and no further than 1 patch. If they aren't between 1/2 and 1 patch distance from each other, then they need to move until they are within this range.

我是否必须链接他们才能执行此操作,或者我可以取消链接吗?

Would I have to link them to do this or can I do this unlinked?

推荐答案

由于您是在设置过程中进行此操作的,因此您可以做的是让NetLogo逐步创建海龟并确保每个海龟都处于适当的距离.逻辑上存在一个问题,因为没有其他海龟,所以不应该测试第一只海龟的距离,并且您假设在这些距离限制下,可以容纳在世界上的海龟数量很少.

Since you are doing this in setup, then what you could do is have NetLogo create the turtles gradually and make sure each is a suitable distance. There is a logical issue, that the first turtle should not be tested for distance since there's no other turtles, and you are assuming that there are few enough turtles that they can fit in the world with those distance restrictions.

尽管如此,这里还是有一些代码可以做到的(针对9只海龟).但是,如果您尝试创建过多的海龟,则确实存在无限循环的风险.随着乌龟数量的增加,它的效率也非常低,因为乌龟会随机放置直到找到合适的位置,这可能需要几次尝试.

Nevertheless, here is some code that will do it (for 9 turtles). It does run the risk of an infinite loop though if you try and create too many turtles. It is also incredibly inefficient as the number of turtles increases because a turtle is randomly placed until it finds a suitable location and that may take several attempts.

to setup
  clear-all
   create-turtles 1
   repeat 8
   [ let min-x min [xcor] of turtles - 1
     let max-x max [xcor] of turtles + 1
     let min-y min [ycor] of turtles - 1
     let max-y max [ycor] of turtles + 1
     create-turtles 1
     [ loop
       [ setxy random-float (max-x - min-x) + min-x random-float (max-y - min-y) + min-y
         let close-turtles other turtles-on (patch-set patch-here neighbors)
         let how-close distance min-one-of close-turtles [distance myself]
         if how-close > 0.5 and how-close < 1 [stop]
       ]
     ]
   ]
end

这篇关于乌龟之间的最小距离的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆