Specify neighbor in specific direction

Viewed 177

I am trying to create a rate of movement model for a turtle to an adjacent patch. Ultimately I want the turtle, given a certain condition, to hatch 8 potential other turtles each to travel specifically to one of their neighbors based on their direction. Basically I want to specify the neighbor patches as North-neighbor, South-neighbor, East neighbor, West neighbor, NW neighbor, and so on. From then I want to have each turtle created to move to a specific patch. Each patch variable has 8 rates of movement: rate to move north, rate to move south, and so on. So I want those 8 turtles to move to their specific patch at the rate of their neighbor patch. After it reaches the center of its North neighbor, I want the North neighbor variable to reset so that the new North neighbor is the North neighbor of the old North neighbor. Here is a code example of just specifying the North-neighbor (I plan on having separate procedures for North-neighbor, South-neighbor, etc.

patches-own[I-b-N heat-sink-N ROM-N ROM-S]  

to setup[
 ca
 reset-ticks
 crt 1 [
  set shape "triangle"
  set color red
  ]
 ask patches[
  set pcolor green
  set ROM-N 5
  set ROM-S 6 ;here there would be ROS-N ROS-S ROS-E ... for each patch
  set I-b-N 8
  set heat-sink-N 2
  ]
]  

to go
 spread-north
 ;spread-south
 tick
end

to spread-north ;duplicate and modify the same procedure for south, east, west,...
      ask turtles [
        if xcor = [pxcor] of patch-here and ycor = [pycor] of patch-here[
          set N-neighbor [patch-at-heading-and-distance 0 1] of self]
      ]
      ask turtles[ ;I use two ask turtles to make sure N-neighbor is created at the beginning of the procedure
        let goal N-neighbor ;here to store N-neighbor
        let parent patch-here
         if [I-b-N] of parent > [heat-sink-N] of goal[ ;If my patch's North-facing I-b > North heat-sink of North neighbor
          hatch 1 [ 
             face goal ;make sure it is facing towards North neighbor
                while [xcor != [pxcor] of goal and ycor != [pycor] of goal][
                  fd 1 /  ((ROM-N + [ROM-N] of goal) / 2) ;moves forward a fraction of the distance between patch-here and North neighbor until it has reached the center coordinates of North neighbor
                ]
            set pcolor black
           ]    
end
1 Answers

There were some typos in your code, I assume from copy/pasting and updating it for your StackOverflow question. I tried to fix those as best I could.

It took me a couple read-throughs of your description and the code, but this is the algorithm I think you want to implement:

  • A turtle at the center of a patch picks its north patch neighbor as its new goal
  • If the I-B-N of the current patch is less than the heat-sink-N, we hatch a new turtle to head towards that goal.
  • The hatchling proceeds to the goal at a rate determined by that goal's ROM-N.

The biggest issue I saw with the code was your use of while to control the turtle movement. In NetLogo, the concept of time is based on ticks, and if you want a turtle to slowly move a bit at a time towards a goal, you want it to move a little bit for each tick, not all at once in a for loop.

The second bit is I think your handling of the goal got a little confused, so I wanted to make it clearer and more explicit. As such I added a turtles-own variable goal to hold the target so we can use it between ticks. I also randomized the ROM-N, I-b-N, and head-sink-N values for the patches so we could get different results on each run for testing.

Another issue is that your movement function could have turtles go past their desired goal, so I added a check for that.

Here is that code. If you run it you'll see at least one hatchling spawn and move to the next patch to the north, then maybe repeating the process depending on the random data I generated to fill in the world for testing. I hope it makes sense and is helpful.

turtles-own [ goal ]
patches-own [ I-b-N heat-sink-N ROM-N ]

to setup
  clear-all
  reset-ticks
  create-turtles 1 [
    ; easier for me to see the direction of the default arrow shape
    ; set shape "triangle"
    set color red
    setxy 0 0
    set heading 0
    set goal patch 0 0 ; the first turtle needs its goal set so it'll spawn immediately
  ]
  ask patches [
    set pcolor green
    set ROM-N random 10
    set I-b-N random 10
    set heat-sink-N random 10
  ]

  ; this is required to make sure we always see at least one move
  ask patch 0 0 [ set I-b-N 10 ]
  ask patch 1 0 [ set heat-sink-N 0 ]
end

to go
  ; since `spread-north` asks the same group of turtles that we move below we could probably merge
  ; this with that and make it a turtle procedure, but I'll leave it as is for now
  spread-north

  ask turtles with [goal != nobody] [
    ; moves forward a fraction of the distance between patch-here and North neighbor until it has reached the center coordinates of North neighbor
    let max-move 1 / ((ROM-N + [ROM-N] of goal) / 2)
    fd min list max-move (distance goal)
  ]
  tick
end

to spread-north

  ; you didn't say what to do with turtles that were finished, so I'll just skip them
  ask turtles with [goal != nobody] [

    ; make sure to check against the final destination, and not the patch here
    ; (we might not be on the goal patch yet)
    if xcor = [pxcor] of goal and ycor = [pycor] of goal [

      ; instead of setting our goal for our hatchlings, let's just clear our goal
      ; and then handle our hatching
      set goal nobody
      set pcolor black

      let maybe-goal patch-at 0 1

      ; If my patch's North-facing I-b > North heat-sink of North neighbor
      if [I-b-N] of patch-here > [heat-sink-N] of maybe-goal [
        hatch 1 [

          ; all we do for the hatchlings is point them at a goal
          set goal maybe-goal

          ; make sure it is facing towards the goal and ready to move on the next tick
          face goal

        ]

      ]

    ]

  ]
end

You also mention copy/pasting the logic for other directions. I think you could generalize this to handle the goal being in any direction without too much trouble. It wasn't clear to me if you really needed each patch to have different I-b and heat-sink for each direction - that seemed odd to me. But if you have trouble getting that done, it'd be good as a second question here.

Related