Is there a way to create impassable barriers in NetLogo?

烈酒焚心 提交于 2019-12-25 01:58:00

问题


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


回答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:

After 13300 ticks:



来源:https://stackoverflow.com/questions/55817689/is-there-a-way-to-create-impassable-barriers-in-netlogo

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!