Search code examples
netlogoagent-based-modeling

Is there a way to create impassable barriers in NetLogo?


I am attempting to code a path-finding behavior wherein agents will locate an optimal patch in the environment and navigate their way around fences to reach said patch. I've created a patch variable 'f', which is set to 1 to indicate fences and 0 for any other patch.

I want to make these fences impassable (i.e. I want them to be patches the agents will not use for movement), but agents still seem to be able to travel on them to some extent and in some cases are even able to fully cross them.

Here is a picture of an agent crossing a barrier I don't want it to cross

Relevant decision-making code for the agents is as follows:

{let moveset patches in-radius 30 with [f = 0 and n > 0]

let target max-one-of moveset [n]

 ifelse patch-here != target 
 [ 
  set heading towards target

  ]
 []

let chance random-float 10
if chance >= 5 [let pick -145]
if chance < 5 [let pick 145] 

ask patches in-radius 1 
[if f = 1 
[ask myself

  [set heading towards min-one-of patches [distance myself] + 180 - random 10 + random 10 ]

]
]

fd 1}

For clarity, 'n' is simply a variable to denote the patch I want my agent to locate and venture to.

Is anyone aware of a simple way in NetLogo to exclude certain patches as viable zones for movement in the decision making process (i.e. hard barriers)?


Solution

  • If you haven't yet, have a look at the "Look Ahead" example in the Models Library- it's a simple demonstration of using patch color to control turtle movement. Some code based on that model is below. With this setup:

    breed [ seekers seeker ]
    breed [ goals goal ]
    patches-own [ steps-from-goal ]
    
    to setup
      ca
      ask patches [ 
        set steps-from-goal 999
      ]
      ask patches with [ pxcor mod 10 = 0 ] [
        set pcolor red
      ]
      ask patches with [ pycor mod 10 = 0 ] [
        set pcolor black
      ]
      ask one-of patches with [ pcolor = black ] [
        sprout-seekers 1 [
          set color blue
          pd
        ]
      ]
      ask one-of patches with [ pcolor = black ] [
        sprout-goals 1 [
          set color white
          set shape "circle"
        ]
      ] 
      reset-ticks
    end
    

    You can have the seekers breed wander around the black squares until they share a patch with a goal turtle:

    to random-wander 
      ask seekers [
        if any? goals-here [
          stop
        ]
        rt random 61 - 30
        ifelse can-move? 1 and [pcolor] of patch-ahead 1 = black [ 
          fd 1
        ] [
          rt one-of [ 90 -90 ]
        ]
      ]
      tick
    end
    

    However, note that turtles can still 'jump' corners of patches using this method, because they are able to assess the patch-ahead 1 at any angle- so, a patch one space ahead of a turtle may be assessed across the corner of another patch. The turtle should never actually land on the forbidden patch, but you may notice that their path can cross those blocked patches.

    Edit:

    See simplified code that "traps" a turtle in a square cage:

    to setup
      ca
      crt 1 [ 
        setxy 5 5
        set heading 180
        repeat 4 [
          repeat 10 [
            ask patch-here [ set pcolor red ]
            fd 1 
          ]
          rt 90
        ]
        die
      ]
    
      crt 1 [ pd ]
      reset-ticks
    end
    
    to go
      ask turtles [
        rt random 61 - 30
        ifelse can-move? 1 and [pcolor] of patch-ahead 1 = black [
          fd 1
        ] [
          rt one-of [ 90 -90 ]
        ]
      ]
      tick
    end
    

    After 1100 ticks:

    enter image description here

    After 13300 ticks:

    enter image description here