1

I am trying to write some code in Netlogo to ask my turtles to go to the second destination after reaching the first destination. Here's my code and hope it will help explain my question.

SETUP:

First, the patch will generate turtles, and I will give each turtle a variable called my-patch:

ask n-of 40 patches with [ pxcor > -23 and pxcor < -5 and pycor < 21 and pycor > -6]
  [ 
    set quad-patch 1

    sprout 1 [
      set origin-turtle 1
      set color yellow
      set my-patches patch-here
      set goal one-of patches with [pxcor = 0 and pycor = 12]
      set speed 0.1 + random-float 0.9
      set size 0.5
      set shape "person"]
  ]

Secondly I will ask these turtles to move to the road which is outside the building:

  set road patches with [
    (pxcor > 45  and pycor <= 8 and pycor >= -18)
  ]
  ask turtles [
    move-to one-of road with [not any? other turtles]
  ]

GO:

This is the tricky part. Now I have set up a path to ask my turtles walk along that path and go into the building. So first I set a goal which is like a rest point in the building. All the turtles will head off to that rest point first and then they will start to find their origins (which I saved as a variable as mentioned before called my-patches).

to go
  ask turtles with [should-stay = false]  [
    move
  ]
  tick
end

to move
  face best-way-to goal
  avoid-walls
  ifelse patch-here != goal
          [fd 1]
          [find-origin]
end

to avoid-walls
  if [wall] of patch-ahead 1 = 1
     [set heading heading - 180]
end

to find-origin

  face my-patches
  fd 1
end

to-report best-way-to [ destination ]

  ; of all the visible route patches, select the ones
  ; that would take me closer to my destination
  let visible-routes patches with [ path = 1 ]
  set routes-that-take-me-closer visible-routes with [
    distance destination < [ distance destination - 1 ] of myself
  ]

  ifelse any? routes-that-take-me-closer [
    ; from those route patches, choose the one that is the closest to me
    report min-one-of routes-that-take-me-closer [ distance myself ]
  ] [
    ; if there are no nearby routes to my destination
    report destination
  ]

end

So my problem is after turtles move along the walking path that I have set, they seem to stop at the rest point instead of moving on to find their origins (my-patch). I know this is because the code I set here:

ifelse patch-here != goal
          [fd 1]
          [find-origin]

But I am not sure how to ask these turtles go to find their origins next after they already reached at this rest point? Any help is really appreciated. Thanks!

DJJKZ
  • 175
  • 12

0 Answers0