Gamgam
Gamgam

Reputation: 13

Netlogo: can I set the distance between turtles?

Netlogo: can I set the distance between turtles?

Hello, I’m trying to create a model in which on each tick a turtle randomly chooses another turtle as a partner, and jumps to a specified distance of their partner (the distance that it’s given is based on a probability). It does not matter where it moves to, as long as the turtles are the specified distance apart. I have tried to model this by creating a ‘jump-with-probabilities’ procedure, and defining distance the turtle jumps in the two ‘IID’ procedures:

to jump-with-probabilities                 ;; adds behaviours depending on how a random number compares with the odds. 
   ask turtles [
   let random-fraction 
       random-float 1.0                            
   if-else random-fraction <= 0.4 
           [ IID_10 ] 
           [ IID_50 ] 
   ]
end


to IID_10                                
  ifelse distance partner  >= 10                  ;; if the distance to their partner is larger than or equal to 10
      [ jump (distance partner - 10) ]            ;; TRUE - jump forward by the difference of distance partner & 10, so that the distance is now 10
      [ jump (-1 * (10 - distance partner)) ]     ;; FALSE - jump backward by the difference of distance partner & 10, so that the distance is now 10
end


to IID_50                         
  ifelse distance partner  >= 50                   ;; if the distance to their partner is larger than or equal to 50
      [ jump (distance partner - 50) ]            ;; TRUE - jump forward by the difference of distance partner & 10, so that the distance is now 50
      [ jump (-1 * (50 - distance partner)) ]     ;; FALSE - jump backward by the difference of distance partner & 10, so that the distance is now 50
end

The problem with using this is that the distances between the turtles in the end are not the same as the distances that I specified. For example, Turtle 0 may jump towards Turtle 5 so that their distance is the specified 20. But, Turtle 5 will also jump towards its partner, which will change the distance between Turtle 0 and Turtle 5. I considered using ‘ask-concurrent’ instead of ask, but the problem remains, because I am telling the turtles to move a certain distance, rather than to move to a certain distance of their partner.

So my question is; is there a way that I can tell a turtle to be within a specified distance of another turtle? So that if the partner moves the turtle will move too to keep the distance at the specified length.
I thought it may be possible to use ‘move-to’ and add the specified distance somehow. Or alternatively, use ‘distance’ to set this between 2 turtles. It seems rather basic, but I have not been able to figure out how to do it!

Any help would be much appreciated!

Upvotes: 1

Views: 1342

Answers (1)

JenB
JenB

Reputation: 17678

There's possibly a better way, but I would do this by moving turtle B to where turtle A is (move-to turtleA), then giving it a random heading (set heading random 360) then moving it forward 10 (forward 10). You could also hide turtle B until you have finished moving it and then unhide it to make the visualisation neater. That sets up the relative position, then use Alan's suggestion of tie to hold the relative position.

Upvotes: 1

Related Questions