我在NetLogo中为家庭范围选择建模。我想要一只乌龟来识别与其当前家庭范围相邻的补丁子集。然后,乌龟应该确定哪个邻近的补丁是最好的(现在定义为具有最高食物益处的补丁)并移动到该补丁来声明它。它继续这些步骤(确定与家庭范围相邻的可用补丁,选择最佳选项,添加到homerange,重复),直到它在家庭范围内有足够的食物。
部分代码如下,但我得到的错误是“FACE预期输入为代理但是取而代之的是NOBODY”。我认为记者或我对它的使用有些不对(我是记者的新手;我正在努力使代码模块化以最小化程序长度)。
知道我哪里出错了吗?谢谢!
patches-own [
owner
benefit
used?
]
to-report edge-patches-of [my-homerange] ;; reporter to find patches along edge of homerange
report my-homerange with [
any? neighbors with [
owner = [owner] of myself
]
with [used? = false] ;; (only want neighboring patches not already owned)
]
end
to pick-homerange
ifelse food < food-needed ;; (turtle builds homerange until it has enough food)
[ let my-homerange patches with [owner = myself]
ask edge-patches-of my-homerange [set pcolor red ] ;; (this to just see the options)
;; now that we know the options, pick the best:
let available-destinations edge-patches-of my-homerange
set destination max-one-of available-destinations [([benefit] of patches)]
face destination
forward 1
if patch-here = destination
[ add-destination ]
]
[stop]
end
答案 0 :(得分:1)
您没有提供足够的代码或说明以确定,但似乎您的逻辑过于复杂并且包含概念错误。试试这个作为起点。特别要注意一组非候选边缘补丁的测试。
globals [food-needed]
patches-own [owner capacity]
turtles-own [homerange]
to setup ;make sure all turtles have an initial homerange
ca
ask patches [set owner nobody]
crt 10 [
set-initial-ownership
;do the following just once, not over and over
set homerange (patches with [owner = myself])
]
;etc
end
to-report benefit [_t] ;patch proc
;patch reports benefit to turtle _t
report 1 ;or whatever
end
to-report food ;turtle proc
report sum [capacity] of homerange
end
to pick-homerange ;turtle proc
;; (turtle builds homerange until it has enough food)
if (food >= food-needed) [stop]
let _candidates edge-patches
if any? _candidates [ ;you almost certainly need to add this
let _destination max-one-of _candidates [benefit myself]
face _destination forward 1
if (patch-here = _destination) [augment-homerange _destination]
]
end
to augment-homerange [_p] ;turtle proc
ask _p [set owner myself]
;just add to the homerange as needed; don't keep recreating it
set homerange (patch-set homerange _p)
end
to-report edge-patches ;turtle proc
;; (only want neighboring patches not already owned)
report (patch-set [neighbors] of homerange) with [owner = nobody]
]
end