maycca
maycca

Reputation: 4090

NetLogo: record variable for every turtle only at end of run

I am interested to know what distance (dist) every of my turtles traveled over the simulation run. Following this post How to write values in files for each turtle?, I am conscient about utilization of file-print.

However, this file record dist values per each time step over the simulation run. How can I access only the final distance traveled by every beetle ? Can this be included also in BehavioralSpace? Ad what is the meaning of "\r\n" ?

my code:

turtles-own [
  dist
]

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

to setup-turtles
  crt 5
  ask turtles [
    set color red
    setxy random-xcor random-ycor 
  ]
end

to go
  if ticks >= 10 [stop ]
  move-turtles
  write-locations-to-file
  tick
end

to move-turtles
  ask turtles [
    rt random 90 lt random 90
    let step.lenght random 5
    jump step.lenght
    set dist dist + step.lenght
    set label dist
  ]
end

to write-locations-to-file 
  ask turtles [ 
   file-open "/Users/.../Documents/outputs.txt"
   file-print (word who " ; " dist "\r\n" )
   file-close
  ]
end

and I expect final dist per turtle: 22 24 12 13 22

Thanks !

enter image description here

Upvotes: 0

Views: 185

Answers (1)

JenB
JenB

Reputation: 17678

Your problem is that you are calling the write-locations-to-file procedure every tick (in go) and it is doing that - writing the locations to the file. Try this instead:

to go
  if ticks >= 10
  [ write-locations-to-file
    stop
  ]
  move-turtles
  tick
end

Upvotes: 2

Related Questions