Search code examples
netlogocumulative-sumintegrated

Netlogo: How can I obtain the accumulate value in Netlogo?


I already confirmed the information on the link. However I could not apply this. The following is a part of the sample model program. I wanted to accumulate the number of turtles, however I could not accumulate it with the sample program. I probably need your advice. Thank you.

globals [num-turtles cumulative-sum average-time number-dead ]
turtles-own [count-up]

to setup
  clear-all
  set num-turtles 5
  reset-ticks
end

to go

  if count turtles < num-turtles
  [ ask patch 0 0
    [ sprout 1
      [ set count-up 0 ]
    ]
  ]

  set cumulative-sum cumulative-sum + 1 ;I would like to calculate the integral value here, but this syntax is not a cumulative value. 
  ask (turtles-on patch 0 0)
    [
      set cumulative-sum count turtles-here
    ]

  set average-time ifelse-value (number-dead = 0)
  [ 0 ][(cumulative-sum) / (number-dead)]

  if (count turtles > 0) [
    ask min-one-of turtles [who] [
      if count-up >= 6 [
        set number-dead number-dead + 1
        die
      ]
    ]
  ]

  ask (turtles-on patch 0 0)
  [ set count-up count-up + 1
  ]

  tick
end

Solution

  • that's much better, thanks- I can now run the code no problem. However, I still don't think I understand what you are wanting cumulative-sum to actually count. Are you just looking for the total number of turtles, including both those still alive and the ones that have died? If so, I think it's just a matter of moving your set cumulative-sum cumulative-sum + 1 line. For example:

    EDIT:

    Ok I think I understand now from your comment. Try this:

    globals [num-turtles cumulative-sum average-time number-dead ]
    turtles-own [count-up]
    
    to setup
      clear-all
      set num-turtles 5
      reset-ticks
    end
    
    to go
    
      if count turtles < num-turtles [
        ask patch 0 0 [
          sprout 1 [
            set count-up 0
          ]
        ]
      ] 
    
      if (count turtles > 0) [
        ask min-one-of turtles [who] [
          if count-up >= 6 [
            set number-dead number-dead + 1
            die
          ]
        ]
      ]
    
      ask turtles-on patch 0 0 [
        set count-up count-up + 1
      ]
    
      set cumulative-sum cumulative-sum + count turtles
      set average-time ifelse-value (number-dead = 0) [0]  [(cumulative-sum) / (number-dead)]
    
      tick
    end