DISTANCE 期望输入成为 Netlogo 中的代理

DISTANCE expected input to be an agent in Netlogo

过了一会儿,当 运行 我的 netlogo 模型:

时,我收到一条典型的错误消息

DISTANCE 预期输入是代理,但得到的却是 NOBODY。 人类 18 运行 DISTANCE

时出错

到目前为止,我无法修复错误。 Netlogo 告诉我错误发生在源代码中的位置:

let dist-nearest-resource distance nearest-resource

我相信知道该消息意味着没有可用的绿色补丁。除了说代理人应该继续前进并随机四处走动之外,我确实知道还能编写什么代码。

在这里,下面是我的最小模型,可以让您更好地理解。有人知道如何解决这个问题吗?

breed [ humans human ]
humans-own [ energy ]

patches-own [ countdown ]

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

to setup
  ca

create-humans(population)
  [
    set shape "person"
    setxy random-xcor random-ycor
  ]

  ask patches [
      set pcolor one-of [green brown]
      ifelse pcolor = green
        [ set countdown 30 ]
        [ set countdown random 30 ]
    ]

  reset-ticks
    end

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

to go-people
  ask humans [ orientation ]
end

to orientation
   ifelse (energy < 4) [ ;if hungry
     let nearest-resource min-one-of (patches with [pcolor = green] in-cone 3 360 ) [ distance myself ] ;check distance between you and nearest resource (3 fields 360 degrees view)
     let dist-nearest-resource distance nearest-resource ;defines what is the shortest distance
     if is-patch? nearest-resource [ ;if green patch exist at all
       face nearest-resource fd distance nearest-resource ;face it and go directly to it
     ]
   ]
   [ walk ] ;otherwise just walk randomly around
end

to walk
  ask humans [
   rt random-float 30 - random-float 30 ;randomly wandering around
   if patch-at dx 0 = nobody [ ;humans get "bounced" away from the limits of the world
     set heading (- heading) ]
   if patch-at 0 dy = nobody [
     set heading (180 - heading) ]
  ]
end

to sustainability ;countdown on brown patches: if 0 is reached, grow resources again after the time set by user
  if pcolor = brown [
    ifelse countdown <= 0
      [ set pcolor green
        set countdown regrowth-time ] ;exhausted fields need 30 ticks to recover
      [ set countdown countdown - 1 ]
  ]
end

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

to go
  if not any? turtles [ stop ] ;defensive programming

  go-people

  ask patches [ sustainability ]
  set resources count patches with [pcolor = green]

  tick

  if count patches with [pcolor = green] = 0 [ stop ] ;model stops if food is no longer available
  if count turtles = 0 [ stop ] ;model stops if all humans died
end

在您的代码中,您从不使用 dist-nearest-resource 变量!除非您打算将 dist-nearest-resource 用于其他目的,否则您可以删除整行。

此外,face nearest-resource fd distance nearest-resource(顺便说一下,它不会崩溃,因为它在您的 if is-patch? nearest-resource 条件内)可以替换为简单的 move-to nearest-resource.

你对错误的看法完全正确。 nearest-resourcenobody。因为它被分配了:

let nearest-resource min-one-of (patches with [pcolor = green] in-cone 3 360 ) [ distance myself ]

patches with [pcolor = green] in-cone 3 360 为空。

实际上看起来您已经在尝试使用 if is-patch? nearest-resource 来处理距离检查下方的下一行。但是,距离检查也应该在 if 的主体内,如下所示:

to orientation
   ifelse (energy < 4) [ ;if hungry
     let nearest-resource min-one-of (patches with [pcolor = green] in-cone 3 360 ) [ distance myself ] ;check distance between you and nearest resource (3 fields 360 degrees view)
     if is-patch? nearest-resource [ ;if green patch exist at all
       let dist-nearest-resource distance nearest-resource ;defines what is the shortest distance
       face nearest-resource fd distance nearest-resource ;face it and go directly to it
     ]
   ]
   [ walk ] ;otherwise just walk randomly around
end

但是,使用这段代码,如果乌龟找不到资源,它就不会做任何事情。我们可以重新安排事情,以便 walk 在找不到任何东西时被调用,像这样:

to orientation
   let nearest-resource min-one-of (patches with [pcolor = green] in-cone 3 360 ) [ distance myself ]
   ifelse (energy < 4) and is-patch? nearest-resource [ ;if hungry and sees a resource
     let dist-nearest-resource distance nearest-resource ;defines what is the shortest distance
     face nearest-resource fd distance nearest-resource ;face it and go directly to it
   ]
   [ walk ] ;otherwise just walk randomly around
end

顺便说一下,in-cone 3 360 等同于 in-radius 3