过了一会儿,我在运行 netlogo 模型时收到一条典型的错误消息:
DISTANCE 期望输入是代理,但得到的是 NOBODY。人类 18 跑步距离时出错
到目前为止,我还无法纠正这个错误。Netlogo 显示了源代码中发生错误的位置:
let dist-nearest-resource distance nearest-resource
Run Code Online (Sandbox Code Playgroud)
我相信知道该消息意味着没有可用的绿色补丁。除了说代理应该继续前进并随机走动之外,我确实知道还要编写什么代码。
下面是我的最小模型,以便您更好地理解。有人知道如何解决这个问题吗?
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
Run Code Online (Sandbox Code Playgroud)
在您的代码中,您永远不会使用该dist-nearest-resource
变量!除非您打算用于dist-nearest-resource
其他目的,否则您可以删除整条线。
此外,face nearest-resource fd distance nearest-resource
(顺便说一下,它不会崩溃,因为它在你的if is-patch? nearest-resource
条件之内)可以用一个简单的move-to nearest-resource
.
归档时间: |
|
查看次数: |
1332 次 |
最近记录: |