简体   繁体   English

Netlogo:计算海龟拥有清单的平均值

[英]Netlogo: Calculate average of turtles-own list

I have 2 to 10 turtles remembering a route home. 我有2到10只海龟想起回家的路。 The coordinate of the turtles is saved every tick to the list path . 海龟的坐标每一次滴答都会保存到列表路径中

to path-tracker
  let x xcor                     ;; gets the current coordinates
  let y ycor
  facexy x y
  setxy x y
  set path lput (list x y) path       ;; adds the coordinate pair (x,y) to the end of the path
end

When the turtles have all reached the target the procedure stops and are set back to the place where they started. 当海龟全部到达目标位置时,程序停止,并重新设置为它们开始的地方。 Now another procedure starts and they should not follow their own path but should follow an average path of all paths. 现在开始另一个过程,它们不应遵循自己的路径,而应遵循所有路径的平均路径。

So how do I calculate an average list of all turtle-own lists? 那么,如何计算所有乌龟拥有的清单的平均清单?

This is a very simple solution which creates a list of size path-length where the ?th element is list (coordinate) of the mean of all the turtle's 0th item (the xcordinate) of the ?th element (the coordinate) and mean of all the turtle's 1st item (the ycoordinate) of the ?th element (the coordinate): 这是一个非常简单的解决方案,它创建了一个路径长度大小的列表,其中第th个元素是乌龟第n个元素的所有第0个项(x坐标)的均值(坐标)的列表(坐标)和所有乌龟第一个元素(坐标)的第一个项目(y坐标):

 let path-length (length [path] of one-of turtles)
 let average n-values path-length [
   (list  (mean [(item 0 (item ? path)) ] of turtles)
          (mean [(item 1 (item ? path)) ] of turtles))]

If you don't like it being so condensed, you can use the following which accomplishes the same result. 如果您不喜欢它这么简洁,可以使用以下命令来达到相同的效果。

  let average-path (list)
  foreach (n-values path-length [?])
  [
    let meanx (mean [(item 0 (item ? path)]] of turtles)
    let meany (mean [(item 1 (item ? path)]] of turtles)
    set average-path lput (list meanx meany) average-path 
  ]

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

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