检查龟的附近一些补丁

时间:2018-11-22 11:21:26

标签: netlogo

我正在努力进行基于补丁属性识别扩展邻域的编码。我需要检查乌龟的视线中是否有一堵墙,如果是这种情况,我的乌龟应该无法看穿这堵墙。

当前,我的递归代码仅在视觉距离为1(对应于邻居)的情况下起作用,但是超过2时,我得到此错误:如果不指定哪个代理,补丁将无法访问turtle或link变量。

我不知道如何与代理人联系,有人会这样做吗?

breed[robots robot]
robots-own[step]

globals [ max-dist]
patches-own [ dist ]

to setup
  ca
  init-environement
  create-robots 1 [init-robots]
end

to init-robots
  set shape "person"
  set size 4
  move-to one-of patches with [no-wall? and (not any? turtles-here)]
  set step 0
end

to init-environement
  ask patches with [ (abs pxcor = max-pxcor) or (abs pycor = max-pycor) ]
    [ set pcolor brown ]
  ask patches with [ (abs pxcor = 20 and abs pycor > 15)
    or (abs pycor = 10 and pxcor > 25)
    or (pycor = 0 and pxcor < 1)][ set pcolor brown ]
  ask n-of nbObstacles patches [ask patches in-radius random-float 2 [ set 
  pcolor brown ]]
end

to move-robot
  let k 0
  let v (neighbors with [no-wall? and (not any? turtles-here)])
  if (any? v)[ move-to min-one-of v [dist] paint-agents k neighbors]
  set step (step + 1)
  output-show step
end

to paint-agents [k case]
  let w ([neighbors] of case with [no-wall? and (not any? turtles-here)])
  if (k  < radius) [
    set k k + 1
    foreach w [
      [x] ->
      ask neighbors with [pcolor != brown][ set pcolor [color] of myself - 2 
paint-agents k x]
    ]
  ]

end

to go
  propagate
  if any? patches with [pcolor = black] [ clear-output ask robots [move-robot] ]
end

to propagate
  ask patches with [ no-wall? ][ set dist -1]
  let p (patch-set patches with [pcolor = black])
  let d 0
  while [ any? p ]
    [ ask p [ set dist d ]
      set d d + 1
      set p (patch-set [ neighbors with [no-wall? and ((dist = -1) or (dist > d))]] of p)
    ]
  set max-dist max [ dist ] of patches
  if (max-dist < 0) [ set max-dist 0 ]
    ifelse (show-labels?)
    [ ask patches with [no-wall?]
        [ set plabel-color white
          set plabel dist]
    ]
  []
end

to-report no-wall?
  report pcolor != brown
end

那里,包含此问题的函数是“ paint-agents”

1 个答案:

答案 0 :(得分:1)

一种方法是消除与墙相交的视线中的斑块。您可以想象,如果在每个贴片之间画一条线,并且墙壁位于所画的线上,那么应该从视觉中移除该贴片。

此链接可能对实现有用:Not see through the walls