我正在研究一个以森林世界为特色的土地利用模型,其中海龟(小农和公司)有能力将森林转变为农田。我想介绍一个功能,海龟“拥有”它们转换的补丁,并且能够稍后重新访问它们以获得这些补丁的认证。主要问题是,当海龟为了获得认证而迁移到农田时,它们不仅会迁移到自己“拥有”的农田,而且还会跨越世界前往其他海龟的农田并对其进行认证。我尝试了几种不同的解决方法,但最终似乎遇到了相同的两个问题:
#1 - 错误:无法在补丁上下文中使用 who
我想使用“who”变量将农田块标记为属于转换该块的海龟,例如,海龟 0 进入森林,将其转换为农田,并且该块农田应该“拥有”由海龟 0 拥有,即变量拥有的补丁应该等于海龟的“谁”。这里的问题是“谁”是海龟自己的变量。因此,当我在补丁上下文中使用它时,它会产生错误。例如,ask smallholders [move-to one-of patches with [[owner = who]]--> 错误。
#2 - 无法设置全局变量 = 'who'
第二,我尝试通过使用代理变量来解决这个问题:一个名为“owner-ID”的全局变量。我会用set owner-ID who将海龟的个人编号印在主人 ID 上。这似乎在某种程度上起作用,即补丁的“所有者”变量对应于转换补丁的海龟。它在计算海龟拥有的经过认证和传统农田的面积时也适用(请参见set-land-ownership下面的命令)。然而,当smallholders-certify-crop-land命令被触发时,海龟不会坚持自己拥有的补丁,而是“跳跃”到世界各地。当通过命令中心提示海龟时,ask turtles [print owner-ID]它们都会返回相同的所有者 ID 值。我觉得我的移动到命令行可能有错误,但我就是找不到它。
总结和问题 我希望农田块由转换它们的海龟“拥有”,并且希望海龟在认证农田块时仅移动到它们“拥有”的块,而不是移动到它们不拥有的块。我想我的问题围绕着是否可以在补丁上下文中以某种方式使用“who”变量。如果没有,那么解决这个问题的一个好的解决方法可能是什么样的。
相关代码如下(我希望)!
globals [owner-ID]
turtles-own [conventional-land-ownership certified-land-ownership]
patches-own [owned-by owner certified?]
to setup [
ask patches [
set pcolor green ;; green = forest
set certified? "no"
set owner "nobody"
]
]
to go
ask turtles [set-land-ownership]
ask smallholders [check-smallholder-status]
tick
end
to set-land-ownership
ask smallholders [
set owner-ID who
set conventional-land-ownership count patches with [owner = owner-ID and certified? = "no"]
set certified-land-ownership count patches with [owner = owner-ID and certified? = "yes"]
]
end
to check-smallholder-status
if wealth >= 0 and (conventional-land-ownership + certified-land-ownership) < SH_max-land-ownership [
smallholders-choose-activity
]
if wealth < 0 [
set color red
set shape "cow skull"
]
if (conventional-land-ownership + certified-land-ownership) >= SH_max-land-ownership [
set color orange + 2
]
end
;; smallholders-choose-activities is a reporter-based command where turtles choose the most economical option available. One of the outcomes is: smallholders-certify-crop-land
to smallholders-certify-crop-land
let available-patch max-one-of patches with [owner = owner-ID and certified? = "no"] [count neighbors with [certified? = "yes"]]
ifelse not any? turtles-on available-patch [
move-to available-patch
]
[]
set wealth wealth - smallholder-certification-cost
set pcolor brown + 1
set certified? "yes"
end
Run Code Online (Sandbox Code Playgroud)
您的第一种方法绝对是可行的方法,并且可以通过一个小的调整来修复。
ask smallholders [move-to one-of patches with [owner = who]]
Run Code Online (Sandbox Code Playgroud)
应该
ask smallholders [move-to one-of patches with [owner = [who] of myself]]
Run Code Online (Sandbox Code Playgroud)
在 after 块中with,变量位于补丁的上下文中,但myself指的是要求补丁检查其所有者的代理,在本例中为每个小农。那么全局变量owner-ID就不再需要了。如果您将这一点贯穿到代码的其余部分,您的第二个问题可能会自行解决。
但是,一般来说最好不要使用who数字,而是直接参考代理。(实际上,当您初始化owner为时nobody,您已经隐式地采取了这种方法,即“无代理”。)我不知道您在哪里要求补丁设置其所有者,但如果 asmallholder在补丁上,smallholder则会
ask patch-here [set owner myself]
Run Code Online (Sandbox Code Playgroud)
上面的行现在将显示为
ask smallholders [move-to one-of patches with [owner = myself]]
Run Code Online (Sandbox Code Playgroud)
NetLogo 专家建议我们who仅在没有其他方法时才使用数字。