0

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)?

SC7
  • 3
  • 2

1 Answers1

1

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

Luke C
  • 10,081
  • 1
  • 14
  • 21
  • I appreciate this, Luke. It should prove very helpful. I'm hoping to find a way around their ability to 'jump' corners of patches, as I need to ensure they cannot cross barriers at all. I will ask this in a new question. – SC7 Apr 24 '19 at 17:03
  • I have asked a new question concerning the above, here: https://stackoverflow.com/questions/55835397/how-to-prevent-agents-ability-to-jump-barriers-in-netlogo – SC7 Apr 24 '19 at 17:20
  • @SC7 - No worries! I will point out that the corner jumping does not account for the problem shown in your second question- that is a separate issue likely to do with your movement procedure (which I don't really understand- you may want to have another look at the logic / intention there). If you run the code in my example, you should **never** see the turtles actually crossing the barrier as shown in your figure showing undesired behavior. – Luke C Apr 24 '19 at 18:30
  • @SC7 I've made an edit to my answer that may better demonstrate what I mean. – Luke C Apr 24 '19 at 18:43
  • this looks helpful indeed! Thank you kindly for sharing. Hopefully comparing and studying this will help me find the error in my own code approach. – SC7 Apr 24 '19 at 18:54