Search code examples
timernetlogointeractionagent-based-modeling

How to make turtles face each other, wait 3 ticks and then keep wandering?


I am new to both, Netlogo and stackoverflow, but your other posts have already helped me a lot.

I am currently trying to program a model, where agents randomly wander a space and have them stop whenever they meet. "Meeting" here means "passing each other in-radius 2". They should face each other, wait for 2 ticks and then keep moving until they find the next agent.

I tried to use NzHelen's question on a timer, but did not really succeed.

So far, I managed to have them face each other. I have trouble putting the tick-command at the right place in my code. (EDIT: This got solved by taking out the wait-command, thanks to Seth. --> And I don't want all turtles to stop moving, but only the ones which are meeting each other). One other thing which I am striving for is some kind of visual representation of them meeting, for instance have the patch blink for the time when they are meeting or a circle which shows up around them when they meet. With the wait-command, everything stops again, which I want to prevent.

Below the code so far.

to go 

  tick  

  ask turtles 
  [
   wander
   find-neighbourhood
  ] 

 ask turtles with [found-neighbour = "yes"]
  [
    face-each-other
  ]

 ask turtles with [found-neighbour = "no" or found-neighbour = "unknown"]
 [ wander ]

  end

;-------
;Go commands      

to wander
      right random 50
      left random 50
      forward 1   
end 

 to find-neighbourhood
     set neighbourhood other turtles in-radius 2
     if neighbourhood != nobody [wander]
     find-nearest-neighbour
  end 

  to find-nearest-neighbour
  set nearest-neighbour one-of neighbourhood with-min [distance myself]
  ifelse nearest-neighbour != nobody [set found-neighbour "yes"][set found-neighbour "no"]
            end 

to face-each-other                             ;;neighbour-procedure
  face nearest-neighbour
  set found-neighbour "no"
  ask patch-here [                             ;; patch-procedure
    set pcolor red + 2
    ;wait 0.2
    set pcolor grey + 2
        ]
    if nearest-neighbour != nobody [wander]
  rt 180
  jump 2

  ask nearest-neighbour 
[
    face myself 
    rt 180
    jump 2
    set found-neighbour "no"
  ]  
  end   

Solution

  • With the help of a colleague I managed to solve my timer-issue. As Seth pointed out wait was not the right command and too many to-end-loops confused my turtles as well. The code now looks like the following and works. The turtles get close to each other, face each other, change their shape to stars, wait three ticks and then jump in the opposite directions.

    to setup
    
      clear-all 
     ask turtles 
       [
         set count-down 3
         ]
    reset-ticks
    
    end 
    ;---------
    to go
    
     ask turtles 
      [    
       if occupied = "yes" [
         ifelse count-down > 0 
    [
           set count-down (count-down - 1)
           set shape "star"
         ][
           set shape "default"
           rt 180
           fd 2
           set occupied "no"
           set count-down 3
         ] 
       ]
    
       if occupied = "no" [
         ; Wandering around, ignoring occupied agents
    
         set neighbourhood other turtles in-radius 2
    
         ; If someone 'free' is near, communicate!
    
         set nearest-neighbour one-of neighbourhood with-min [distance myself]
         ifelse nearest-neighbour != nobody [
             if ([occupied] of nearest-neighbour = "no") [
                face nearest-neighbour            
                set occupied "yes"
                ask nearest-neighbour [ 
                  face myself              
                  set occupied "yes"
               ]]
         ][
           ; No one found, keep on wandering
           wander
         ]]] 
       tick 
       end
    ;-------
    ;Go commands      
    
    to wander
          right random 50
          left random 50
          forward 1   
    end