'I want turtles to avoid walls in Netlogo

I am trying to move my turtles in a pink path avoiding all the walls in patch colors grey and red. Green or doors which they can pass through, and pink is the path which they need to move through ultimately. Here is my code

 globals [
     t
     contacts
    ]
    breed [students student]
    breed [staffs staff]
    students-own [ eat target ]
    staffs-own [ name ]
    patches-own [ wall? ]
    to clear
     clear-all
    end
    to setup
     clear-all
     setup-hall
     setup-students
     reset-ticks
    end

    to setup-students
     set contacts 0
     create-students num-students [
     ;    setxy random-pxcor random-pycor
     let choice random 4
    (ifelse choice = 0
    [setxy random-pxcor -11]
     choice = 1
    [setxy  random-pxcor -12]
     choice = 2
    [setxy random-pxcor -13]
    [setxy random-pxcor -14])
    ]

    ask students[
    set size 1.5
    set shape "person"
    set color white
    let choice  random 2
    (ifelse choice = 0
      [set eat 0]
      [set eat 1]

      )
      ]

    end

    to setup-hall

    ask patches with [ pycor = -16  or pycor = 16 or pycor = -9]
    [ set pcolor grey ]
    ask patches with [ pxcor = -32 or pxcor = 32 ]
    [ set pcolor grey ]
    ask patches with [ pxcor <= -21 and pycor >= 8]
    [set pcolor grey]
    ask patches with [ pxcor = 12 and pycor >= -9 ]
    [ set pcolor grey ]
    ask patches with [ pxcor <= -15 and pycor >= 12 ]
    [ set pcolor grey ]
    
    ask patches with [ pycor = -9 and (pxcor >= -23 and pxcor <= 
    -21)]
    [set pcolor green ]
    ask patches with [ pycor = -9 and (pxcor >= 5 and pxcor <= 7)]
    [set pcolor green ]
    ask patches with [ pxcor = 12 and (pycor >= 7 and pycor <= 9)]
    [set pcolor red]
    ask patches with [ pxcor = 12 and (pycor >= -3 and pycor <= -1)]
    [set pcolor red]
    ask patches with [ pxcor = -32 and (pycor >= -15 and pycor <= 
   -10)]
  [set pcolor black]
  ask patches with [ pxcor = 32 and (pycor >= -15 and pycor <= -10)]
  [set pcolor black]

  ask patches with [ pxcor = 10 and pycor <= 9 and pycor >= -2] [set pcolor pink ]
  ask patches with [ pxcor = 6 and pycor <= 7 and pycor >= -2] [set pcolor pink ]
  ask patches with [ pxcor = 2 and pycor <= 7 and pycor >= -2] [set pcolor pink ]
  ask patches with [ pxcor = -2 and pycor <= 7 and pycor >= -2] [set pcolor pink ]
  ask patches with [ pxcor = -6 and pycor <= 7 and pycor >= -2] [set pcolor pink ]
  ask patches with [ pycor = 7 and pxcor <= -2 and pxcor >= -6] [set pcolor pink ]
  ask patches with [ pycor = 7 and pxcor <= 6 and pxcor >= 2] [set pcolor pink ]
  ask patches with [ pycor = -2 and pxcor <= 10 and pxcor >= 6] [set pcolor pink ]
  ask patches with [ pycor = -2 and pxcor <= 2 and pxcor >= -2] [set pcolor pink ]

end

to avoid-walls
      if ([pcolor] of patch-ahead 1 = grey or [pcolor] of patch-ahead 1 = red or [pcolor] of patch-ahead 1 = brown - 2 )
      [lt random-float 270 ]
;      [fd 0.1]
end

to move-people
if ( pycor >= -8 and pycor <= -4 and [pcolor] of patch-here != pink)[
  facexy -6 -2
  fd 0.1 
  ]

end

to move-in-path
      if([pcolor] of patch-ahead 1 = pink and [pcolor] of patch-here = pink)
      [forward 1]
      if ([pcolor] of patch-here  = pink and pxcor = -6 and pycor = -2)
      [set heading 0 forward 1]
      if (pxcor = -6 and pycor = 7 and [pcolor] of patch-here = pink )
      [ set heading 90 forward 1]
      if (pxcor = -2 and pycor = 7 and [pcolor] of patch-here = pink )
      [ set heading 180 forward 1]
      if (pxcor = -2 and pycor = -2 and [pcolor] of patch-here = pink )
      [ set heading 90 forward 1]
      if (pxcor = 2 and pycor = -2 and [pcolor] of patch-here = pink )
      [ set heading 0 forward 1]
      if (pxcor = 2 and pycor = 7 and [pcolor] of patch-here = pink )
      [ set heading 90 forward 1]
       if (pxcor = 6 and pycor = 7 and [pcolor] of patch-here = pink )
      [ set heading 180 forward 1]
       if (pxcor = 6 and pycor = -2 and [pcolor] of patch-here = pink )
      [ set heading 90 forward 1]
       if (pxcor = 10 and pycor = -2 and [pcolor] of patch-here = pink )
      [ set heading 0 forward 1]
       if (pxcor = 10 and pycor = 9 and [pcolor] of patch-here = pink )
      [ forward 3 left 90]
end

to go
    ;let hall patches with [pycor <= 0 and pycor >= -25 and pxcor <= 0 and pxcor >= -25 ]

     ask students [
     create-links-with other students-here [ hide-link ]
     set contacts count links
     fd 0.1
     ifelse ( eat = 0)[
     ifelse ( pycor <= -9)
     [ avoid-walls]
     [
     avoid-walls
     move-people
     move-in-path]
  ][set heading 90 fd 1]
  ]
   tick
end
    

I have an issue as a few turtles pass through the walls and move to the right side pl[![enter image description here][1]][1]ot. I need help in correcting this issue



Solution 1:[1]

Edit to answer your further question: From what I can tell, there are two reasons

  • The first one is that world wrapping is on, so there is nothing stopping the turtles from walking around to the other part of the room. This can be helped by adding a wall on the left side of the dining hall (or the right side of the side room)
  • The second one is more subtle, but you can replicate it if you put a turtle with eat = 0 on patch 9 9 with heading 90. It will then step onto the end of your pink path and execute the following part of the move-in-path procedure : if (pxcor = 10 and pycor = 9 and [pcolor] of patch-here = pink ) [ forward 3 left 90] This will let it teleport through the wall. Lowering the distance, or adding set heading 0 to the relevant part of the move-in-path procedure should solve this.
  • Lastly, I found out that turtles who do get trapped (after I had added the side walls) sometimes manage to escape. This happens when they randomly walk far enough that the move-people prodedure applies to them. This changes their heading back after avoid-walls has redirected them, so it lets them walk through walls. It is not currently relevant since its initial conditions only appear through a bug, but something to keep in mind should you change the layout further

Lastly a quick remark. I was wondering why in the avoid-walls procedure, you are using [lt random-float 270 ]. The 270 seems pretty arbitrary to me. Why not 360?


Below the first set of answers, that have been used in the edited code


A few suggestions upon just seeing the code without being able to execute it:

  • Is your avoid-walls procedure supposed to have an ifelse command? As it is now it does not work.

  • You define the local variable hall in go but don't use it anywhere. Either comment it out, use it, or remove it.

  • It is quite annoying to try to guess what patches have what coordinates, especially without having the entire code. It would be helpful, both for you and for anyone else reading the code, to add comments to your code describing what patches the coordinates are referring to. You might remember it now but you will not necessarily when you reopen the code in half a year.

  • The fd 0.1 in your go procedure seems to be the one causing the problems with the walls here. It does not check if the patch they are about to move onto is a wall or not. Simply removing it should be fine, since you already have the turtles move forward in the avoid-walls procedure (assuming it was supposed to be an ifelse), which you call in go as well.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1