一段时间后,我在运行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
答案 0 :(得分:4)
在您的代码中,您永远不会使用dist-nearest-resource
变量!除非您计划将dist-nearest-resource
用于其他目的,否则您可以摆脱整行。
此外,face nearest-resource fd distance nearest-resource
(顺便说一下,因为它在if is-patch? nearest-resource
条件中而不会崩溃)可以用简单的move-to nearest-resource
替换。
答案 1 :(得分:4)
你对错误的看法是完全正确的。 nearest-resource
是nobody
。由于它被分配:
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
。