我正在尝试执行以下操作:
households
)检查是否有任何补丁具有(1)is-home = true
和(2)成本低于家庭的钱;my-home
;my-owner
变量;和我已经尝试了以下(并尝试了代码)并不断出错。
breed [ households household ]
globals [
homes
number-displaced
]
households-own
[
my-home
money
owner?
]
patches-own
[
is-home?
cost
my-owner
]
to-report random-exponential-in-bounds [mid mmin mmax]
let result random-exponential mid
if result < mmin or result > mmax
[ report random-exponential-in-bounds mid mmin mmax ]
report result
end
to setup-households
ask households
[ set money round random-exponential-in-bounds 50000 0 1000000 ]
end
to setup-patches
ask n-of 100 patches
[ set is-home? one-of [ true false ]
set cost random-normal 173000 43000 ]
end
to setup
ca
reset-ticks
set-default-shape households "person"
setup-households
setup-patches
end
to choose-house
ifelse any? patches with [ is-home? = true and cost <= [money] of myself ]
[ ask households [ move-to one-of patches with [ is-home? = true and cost <= [money] of myself ] ]
set owner? true
set my-home patch-here
set my-owner turtle-here
]
[ set number-displaced number-displaced + 1
die ]
end
to go
choose-house
end
我发现你的代码有一些问题。
setup-households
:你实际上从未创造过任何海龟。你只要求(不存在的)海龟换钱。
setup-patches
:我不确定你在这里做什么。你要求 100 个补丁可能成为一个家,也可能不会成为一个家。何不索要50块补丁就可以成为一个家呢?或者让所有的补丁都变成一个家,但根据你想要的家的数量调整你的世界大小?我还建议使用scale-color
,以可视化所有补丁的成本。
choose-house
:您正在尝试检查自己的钱,但实际上没有自己可用(目前由观察者调用)。将 ask households
移动到程序的开头可以解决此问题。我应该指出,目前多户人家可以搬进同一所房子。最后,你拼错了turtles-here
to choose-house
ask households [
ifelse any? patches with [ is-home? = true and cost <= [money] of myself ] [
move-to one-of patches with [ is-home? = true and cost <= [money] of myself ]
set owner? true
set my-home patch-here
set my-owner turtles-here
] [
set number-displaced number-displaced + 1
die
]
]
end