尝试移动到最近的品种时,是什么导致了 "Nobody" 错误?
When trying to move to nearest breed, what causes the "Nobody" error?
我做了一个老虎吃狼的模型。
当老虎的能量大于狼能量总和的一半时,老虎就会找到离它最近的狼,吃掉它并获得能量。
所以我写道:
to tigers-feed
ask tigers [
let WE-sum sum [WEnergy] of wolves
if TEnergy > WE-sum * 0.5 [
move-to min-one-of wolves [distance myself] ;;it reports bugs here.
ask one-of wolves-here [die]
set TEnergy Wt * tiger-gain-from-food
]
]
但是当运行执行to go命令时,程序会警告我:
MOVE-TO expected input to be an agent but got NOBODY instead.
这是我的完整代码:
globals[
Ww
Wt
wolf-gain-from-food
tiger-gain-from-food
]
breed [wolves wolf]
breed [tigers tiger]
wolves-own [WEnergy]
tigers-own [TEnergy]
;;probably nothing wrong with here, you can jump them.
to setup
clear-all
parameter-initialization
setup-breed
reset-ticks
end
to parameter-initialization
set Ww random 2
set Wt random 4
set wolf-gain-from-food 100
set tiger-gain-from-food 200
end
to setup-breed
set-default-shape wolves "wolf"
create-wolves 100 [setxy random-xcor random-ycor]
ask wolves [
set WEnergy Ww * wolf-gain-from-food
set size 4
set color grey
]
set-default-shape tigers "x"
create-tigers 1 [setxy random-xcor random-ycor]
ask tigers [
set TEnergy Wt * tiger-gain-from-food
set size 6
set color yellow
]
end
to go
ask tigers [set TEnergy TEnergy + 2]
tigers-feed
tick
end
to tigers-feed
ask tigers [
let WE-sum sum [WEnergy] of wolves
if TEnergy > WE-sum * 0.5 [
move-to min-one-of wolves [distance myself]
ask one-of wolves-here [die]
set TEnergy Wt * tiger-gain-from-food
]
]
end
事实上,当我尝试运行只执行一次时,程序并没有报错。所以我想这不是导致错误的命令 move-to min-one-of wolves [distance myself]
。那么究竟是什么导致了错误呢?
给你错误的那一行实际上是 move-to min-one-of wolves [distance myself]
.
如您所见,只有当模型中没有狐狸时才会出现错误。这是因为您没有包含停止条件或任何其他阻止模型在 none.
处搜索狼的条件
假设当老虎吃掉所有的狼时你很高兴让模型停止,你可以这样做:
to go
if (not any? wolves) [stop]
ask tigers [set TEnergy TEnergy + 2]
tigers-feed
tick
end
请注意,写 any? <agentset>
等同于写 count <agentset> > 0
但是,正如 NetLogo 词典中正确建议的那样,it is more efficient and readable for this purpose(在我们的例子中,我们必须添加 not
在它之前)。
我做了一个老虎吃狼的模型。 当老虎的能量大于狼能量总和的一半时,老虎就会找到离它最近的狼,吃掉它并获得能量。 所以我写道:
to tigers-feed
ask tigers [
let WE-sum sum [WEnergy] of wolves
if TEnergy > WE-sum * 0.5 [
move-to min-one-of wolves [distance myself] ;;it reports bugs here.
ask one-of wolves-here [die]
set TEnergy Wt * tiger-gain-from-food
]
]
但是当运行执行to go命令时,程序会警告我:
MOVE-TO expected input to be an agent but got NOBODY instead.
这是我的完整代码:
globals[
Ww
Wt
wolf-gain-from-food
tiger-gain-from-food
]
breed [wolves wolf]
breed [tigers tiger]
wolves-own [WEnergy]
tigers-own [TEnergy]
;;probably nothing wrong with here, you can jump them.
to setup
clear-all
parameter-initialization
setup-breed
reset-ticks
end
to parameter-initialization
set Ww random 2
set Wt random 4
set wolf-gain-from-food 100
set tiger-gain-from-food 200
end
to setup-breed
set-default-shape wolves "wolf"
create-wolves 100 [setxy random-xcor random-ycor]
ask wolves [
set WEnergy Ww * wolf-gain-from-food
set size 4
set color grey
]
set-default-shape tigers "x"
create-tigers 1 [setxy random-xcor random-ycor]
ask tigers [
set TEnergy Wt * tiger-gain-from-food
set size 6
set color yellow
]
end
to go
ask tigers [set TEnergy TEnergy + 2]
tigers-feed
tick
end
to tigers-feed
ask tigers [
let WE-sum sum [WEnergy] of wolves
if TEnergy > WE-sum * 0.5 [
move-to min-one-of wolves [distance myself]
ask one-of wolves-here [die]
set TEnergy Wt * tiger-gain-from-food
]
]
end
事实上,当我尝试运行只执行一次时,程序并没有报错。所以我想这不是导致错误的命令 move-to min-one-of wolves [distance myself]
。那么究竟是什么导致了错误呢?
给你错误的那一行实际上是 move-to min-one-of wolves [distance myself]
.
如您所见,只有当模型中没有狐狸时才会出现错误。这是因为您没有包含停止条件或任何其他阻止模型在 none.
处搜索狼的条件假设当老虎吃掉所有的狼时你很高兴让模型停止,你可以这样做:
to go
if (not any? wolves) [stop]
ask tigers [set TEnergy TEnergy + 2]
tigers-feed
tick
end
请注意,写 any? <agentset>
等同于写 count <agentset> > 0
但是,正如 NetLogo 词典中正确建议的那样,it is more efficient and readable for this purpose(在我们的例子中,我们必须添加 not
在它之前)。