1

I am trying to write code in Netlogo to ask turtles wait for a certain time (eg:2 seconds) if his neighbours are less than a certain distance. after the distance between him and his neighbours is more than that distance, this turtle can start to move. Here's my pieces of code:

My initial setup for turtles:

;;send people back to where they come from
  ask turtles [
    move-to one-of road with [not any? other turtles in-radius 4]
  ]

Ask turtles to move:

  to move
           face best-way-to goal
           ifelse patch-here != goal
            [ 
              ifelse how-close < 2
              [wait 2 fd 1]
              [fd 1]
            ]
            [stop]
    end

to-report keep-distance
   set close-turtles other turtles-on (patch-set patch-here neighbors4)
   ifelse any? close-turtles
       [set how-close distance min-one-of close-turtles [distance myself]]
       [set how-close 100] ;this means cannot find neighbours around this people
   report how-close
end

but this doesnt give me what I want. Does anybody have any idea how to realise this in Netlogo? Any help is really appreaciated. Thanks!

DJJKZ
  • 175
  • 12

1 Answers1

1

The problem with using wait is that the model will pause, so the other turtles also don't move and can't move away. If you want a turtle to only move if when there's nobody close, try replacing:

to move
  face best-way-to goal
  ifelse patch-here != goal
  [ 
    ifelse how-close < 2
    [wait 2 fd 1]
    [fd 1]
  ]
  [stop]
end

with

to move
  face best-way-to goal
  if patch-here != goal and how-close >= 2
  [ forward 1
  ]
end

That is, you don't need the ifelse construction, you can use if and simply move only when the conditions are appropriate to move.

JenB
  • 17,620
  • 2
  • 17
  • 45
  • Many thanks for your reply. But when I pasted your code into Netlogo, the turtles just didn't forward anymore. I suppose it's because their distances between each other are initally less than 2 so they wont walk? do you have any idea about this? Thanks! – DJJKZ May 15 '20 at 08:22
  • If how-close starts with a value of <2 then yes, nothing will ever move. It sounds like you need to think more deeply about the conditions under which you want the turtles to move or not. I suspect you need a new question that shows the calculation for how-close and explains what you are trying to do. – JenB May 15 '20 at 08:34
  • Thanks! I have revised my question and added in my code for turtle setup. so the turtles should in theory be sprouted with nobody in radius 4? but i run the code still doesnt move at all. does that help explain my question? – DJJKZ May 15 '20 at 08:56
  • 1
    (1) Please make this a NEW question. (2) When you do, include the code that calls the keep-distance procedure and also whether how-close is a turtle or global variable – JenB May 15 '20 at 09:35