Search code examples
netlogo

How can I record the identity of turtles another turtle interacts with?


My model involves one turtle surveying the area and it should keep track of the ID/ 'who' of the turtles it bumps into. Importantly, the surveyor could potentially record the same ID/ 'who' twice but I'm not sure how to implement that memory side of things.

breed[boats boat]

boats-own [
  my-neighbors
  num-neighbors
count-neighbors]


to setup
  clear-all
  crt 100 [
    move-to one-of patches with [ not any? turtles-here ]
    
  ]
  
  crt 1 [set breed boats
  set count-neighbors 0
  ]

  reset-ticks
end

to go 
  ask turtles [
    fd 1
  ]
  
  ask boats [
    survey
  ]
  tick 
end 

to survey
 
    set my-neighbors (other turtles) in-radius 3
    set num-neighbors count my-neighbors
    set count-neighbors [who] of my-neighbors
   
end 

UPDATE In light of the answer below I came up with the following which also records when the interaction took place by creating a new time variable.

breed[boats boat]

boats-own [
  my-neighbors
  num-neighbors
id-neighbors
survey-time]

turtles-own [
  time
]


to setup
  clear-all
  crt 10 [
    move-to one-of patches with [ not any? turtles-here ]
    
  ]
  
  crt 1 [
    set breed boats
    set shape "circle"
    set id-neighbors []
    set survey-time []
  ]

  reset-ticks
end

to go 
  ask turtles [
    fd 1
    set time ticks 
  ]
  
  ask boats [
    survey
  ]
  tick 
end 

to survey
 
    set my-neighbors (other turtles-here )  
    set num-neighbors count my-neighbors
    set id-neighbors (sentence id-neighbors [who] of my-neighbors )
    set survey-time (sentence survey-time [time] of my-neighbors )
   
end 

Solution

  • Currently, you are always setting count-neighbors to the list of all its current neighbors, but this list is cleared and made anew with each tick. Instead of that, you can use primitives like sentence to combine the old list of count-neighbors with the new list you are generating this iteration and set the new list of count-neighbors to be this combined list.

      set count-neighbors (sentence count-neighbors [who] of my-neighbors )
    

    I also initialised your count-neighbors as an empty list rather than as 0, since 0 is already a turtle who so that would give you false information

      crt 1 [set breed boats
        set count-neighbors []
      ]
    

    To check if the memory is working as intended, you can use inspect one-of boats in the command center and see how each new entry gets added to the end of the list.


    Edit for followup question

    Your solution here definitely works. Allow me a few suggestions for it as well:

    There is not need to create the time variable, each turtle can already access the ticks global variable

      set id-neighbors (sentence id-neighbors [who] of my-neighbors )
      set survey-time (sentence survey-time [ticks] of my-neighbors )
    

    As an alternative, this one creates a list of lists, with each item containing the turtle that was encountered and the time when it was encountered. Slightly harder to work with but clear at a glance which values belong together.

    set id-neighbors (sentence id-neighbors [list who ticks] of my-neighbors )