Netlogo:将海龟移动到未占用的补丁并打印



我想将海龟移动到一个未被完全占用的补丁上(n-job -to-fill != 0)

ask turtles [move-to one-of patches with [n-jobs-to-fill != 0]

在每次分配turtle-patch之后,n-jobs-to-fill(工作原理类似于计数器)少计算一个位置

ask patches [set n-jobs-to-fill n-jobs-to-fill - 1]

你能帮我迭代地(为每个tick)做这个,并通过打印观察者行中每个运动的海龟补丁来测试它吗?

谢谢

要求补丁更新变量的部分不应该用一般的ask patches完成,因为这将针对所有补丁,并且要么被调用得太少(如果你每次迭代go只做一次),要么太频繁(如果你每次特定补丁接收海龟时都这样做)。

为了轻松地瞄准刚刚接收到海龟的特定补丁,以及它接收海龟的确切次数,让移动的海龟请求它的新补丁执行更新。或者更好:考虑到海龟可以直接访问它们所在补丁的补丁自己的变量,你可以这样做:

patches-own [
n-jobs-to-fill
]
to setup
clear-all
reset-ticks

ask patches [
set n-jobs-to-fill random 10
]

create-turtles 10
end

to go
if (not any? patches with [n-jobs-to-fill != 0]) [stop]

ask turtles [
if (any? patches with [n-jobs-to-fill != 0]) [
move-to one-of patches with [n-jobs-to-fill != 0]
set n-jobs-to-fill n-jobs-to-fill - 1

show (word "I moved to " patch-here ", which now has " n-jobs-to-fill " jobs to fill")
]
]

tick
end

最新更新