Search code examples
averagenetlogoagents

NetLogo: Finding the average value of a set of turtles


I'm trying to implement a monitor in the user interface that displays the average value of a variable shared by a breed of turtles (turtles own). Does anyone know of a method of collecting all the values, adding them together and dividing by the quantity of turtles to get the value or know of an easier method?


Solution

  • If the variable that each turtle has is shell-size, for example, then:

    print mean [shell-size] of turtles
    

    will do it. It might be useful to know how to do this by hand, so that you can do other calculations if you want. Here's one way:

    print (sum [shell-size] of turtles) / (count turtles)
    

    Here's another

    let total 0
    ask turtles [set total total + shell-size]
    print total / (count turtles)
    

    Obviously, you'll want to replace the print statements with whatever suits your needs. For a monitor, you should be able to enter this code directly into the interface, or wrap it up in reporter and then use that in the monitor.