Amal
Amal

Reputation: 77

How to implement a timer for each turtle in netlogo

I am trying to implement a timer for each turtle in Netlogo so I can take the minimum value, maximum and average time taken for each turtle. Can anyone help

Upvotes: 0

Views: 284

Answers (4)

Matteo
Matteo

Reputation: 2926

I think there is something that none of the previous answers considered: the way you implement your timer depends on how you want to use the measurement.

1 - If you want to read the measurement only after the measurement is completed

The expression "after the measurement is completed" can mean both that you want to read it at some point later during the model, or maybe even just from some model's output.

The approach I'd take in this case is similar to what JenB suggested, but I believe you can put it a bit simpler because here I only use one extra turtle variable (apart from my-variable, that is there only to represent something that you already have in your model):

turtles-own [
 my-variable    ; This represents something you have in your model, to be used as a condition for the timer
 my-timer
]

to setup
  clear-all
  reset-ticks
  create-turtles 10
end

to go
  ; Here you have your normal code. When you need, just include
  ; 'start-timer' and 'stop-timer'. For simplicity, let's
  ; say you want to count how long does each turtle take
  ; to get from a value of 1 to a value of 5 for my-variable.
  ; You can just do:
  
  ask turtles [
    if (random 10 < 2) [
     set my-variable (my-variable + 1)
    ]
    
    if (my-variable = 1) [
     start-timer 
    ]
    
    if (my-variable = 5) [
     stop-timer
     type "I am turtle " type who type " and it took me " type my-timer print " ticks."
     die
    ]
  ]
  
  tick
  
  if (not any? turtles) [stop]
end

to start-timer
  set my-timer ticks
end

to stop-timer
  set my-timer (ticks - my-timer)
end

Note that most of the code is there only to make a full reproducible example, but the actual implementation only consists of my-timer, to start-timer and to stop-timer.

You can take this approach because the hypothesis here is that you will be interested in reading the measurement only after to stop-timer happened. Otherwise, see point 2.

2 - If you want to be able to read the measurement at any moment during the simulation

In this case, you need to make the timer progress as time progresses (as opposed to point 1, where you could just take the initial and final time, and make the difference between the two).

I guess the easiest way to do this is to conditionally increment the timer every time there is a tick.

Taking the same example as before, it would be something like:

turtles-own [
 my-variable    ; This represents something you have in your model, to be used as a condition for the timer
 timer-active?
 my-timer 
]

to setup
  clear-all
  reset-ticks
  create-turtles 10 [
   set timer-active? FALSE 
  ]
end

to go  
  ask turtles [
   if (random 10 < 2) [
     set my-variable (my-variable + 1)
    ]
    
   if (my-variable = 1) [
     set timer-active? TRUE 
    ]
   
   if (my-variable = 5) [
     set timer-active? FALSE 
    ]
  ]
  
  tick
  ask turtles with [timer-active?] [
   set my-timer (my-timer + 1) 
  ]
  
  if (count turtles with [my-variable < 5] = 0) [stop]
end

This way, you will be able to see at any moment what is the current value of my-timer for each turtle.

Upvotes: 0

goodgest
goodgest

Reputation: 418

For example, you could do this coding that flags the turtle you want to monitor and increments the flag variable when an event occurs. You can then monitor it in the BehaviorSpace and analyses the results output in a csv file. For example, the following codes:

globals [ID]
turtles-own [special-turtle]

to create-turtle
  crt 1 [
    setxy min-pxcor 0
    set heading  90
    set special-turtle false
    set ID who]
end

to go
 ;omitted
 special-turtles
 tick
end

to special-turtles
 ask turtles-on patch 0 0 [set ID who]
 ask max-one-of turtles [who] [set special-turtle true]
 ask turtles with [special-turtle = true][set special-turtle (special-turtle + 1)]
end

Upvotes: 0

Steve Railsback
Steve Railsback

Reputation: 1736

And check out the new Time extension if you want your timers to use real time units (seconds, minutes, years...) and have non-integer values.

https://github.com/NetLogo/Time-Extension

Look at primitives such as time:difference-between

Upvotes: 0

JenB
JenB

Reputation: 17678

You haven't said what you actually want timed (or given any code attempt). Any time you need a turtle to remember anything from one tick to the next, you need a variable. The simplest code for a timer is to have a variable that you set to the current value of ticks when the process starts and subtract that start time from the current value of ticks when the process stops. Here is a complete model example:

turtles-own
[ start-time
  time-taken
]

to setup
  clear-all
  create-turtles 20
  [ set start-time 2 + random 10
    set time-taken -1
  ]
  reset-ticks
end

to go
  let movers turtles with [time-taken = -1 and start-time <= ticks]
  ask movers
  [ set heading random 360
    forward 1 + random 3
    if random-float 1 < 0.05 [ set time-taken ticks - start-time ]
  ]
  tick
end

Upvotes: 2

Related Questions