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

Netlogo: move turtles to non-occupied patches and print it

我想将海龟移动到未完全占用的补丁之一 (n-jobs-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]

你能帮我迭代地(针对每个刻度)执行此操作并通过在观察者行中打印每个移动 turlte-patch 来测试它吗?

谢谢

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

为了轻松定位刚刚收到乌龟的特定补丁,以及它收到乌龟的确切次数,让移动的乌龟请求它的新补丁执行更新。甚至更好:鉴于海龟可以直接访问它们所站立的补丁的 patches-own 变量,您可以这样做:

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