0

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

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

帮助?

4

1 回答 1

0

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

if distance goal < 3 

你在调用它之前没有初始化目标,所以这意味着if distance 0 < 3这会给你一个错误,如果你的第一个条件满足,目标只有一个值(如果 dest = patch 0 0 and distance dest < 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
于 2013-11-04T11:37:49.577 回答