此代码无法通过补丁运行

时间:2013-06-06 18:33:17

标签: simulation netlogo

当我尝试确定最接近将龟带到目的地的补丁时,会出现此错误。

this code can't be run by a patch
error while turtle 0 running DISTANCE
    called by procedure GO
    called by Button 'go'

这是有问题的代码:

let nearest-patch min-one-of patches in-cone 1 180 [distance dest]
        if not any? other turtles-on nearest-patch 
        [ face nearest-patch
          fd 1 ]

完整代码:

to go
  reset-ticks
  ask turtles
  [  
     ; going to the center, then goal
     if dest = patch 0 0 and distance patch 0 0 < 3 
     [ repeat 5 [ fd 1 ]
       set dest goal ]

     ; wrapping around and going to the center, then goal
     if distance goal < 3 
     [ repeat 5 [ fd 1 ]
       set dest patch 0 0 ]

       face dest

    let nearest-patch min-one-of patches in-cone 1 180 [distance dest]
    if not any? other turtles-on nearest-patch 
    [ face nearest-patch
      fd 1 ]

  ]
  tick
end

帮助?

1 个答案:

答案 0 :(得分:0)

我不明白为什么你使用dest和目标,但无论如何我认为你的代码有两个问题,第一个:

if distance goal < 3 

你没有在调用之前初始化目标所以它意味着if distance 0 < 3会给你一个错误,如果满足你的第一个条件,目标只有一个值(如果dest = patch 0 0和distance dest&lt; 3)

this code can't be run by a patch的错误是在补丁想要使用乌龟变量时引起的,我通常将乌龟变量保存在局部变量中并间接调用它,但可能有更好的方法我不知道我也是netlogo的新手;)

turtles-own [dest goal]
to setup
  clear-all
  create-turtles 10 [
    move-to patch random 15 random 15
  ]
end
to go
  reset-ticks
  ask turtles
  [  
    ; going to the center, then goal
    set dest patch 0 0

    if dest = patch 0 0 and distance dest < 3 
      [ repeat 5 [ fd 1 ]
        set goal dest ]

    ; wrapping around and going to the center, then goal

    if distance goal < 3 
      [ repeat 5 [ fd 1 ]
        set dest patch 0 0 ]

    face dest
    let d dest
    let nearest-patch min-one-of patches in-cone 1 180 [distance d]
    if not any? other turtles-on nearest-patch 
    [ face nearest-patch
      fd 1 ]

  ]
  tick
end