'Netlogo: move turtles to non-occupied patches and print it
I want to move turtles to one of patches not fully occupied (n-jobs-to-fill != 0). The code is
ask turtles [move-to one-of patches with [n-jobs-to-fill != 0]
After each allocation turtle-patch, n-jobs-to-fill (that works like a counter) counts one place less
ask patches [set n-jobs-to-fill n-jobs-to-fill - 1]
Can you help me to do that iteratively (for each tick) and to test it by printing each movement turlte-patch in the observer line?
Thank you
Solution 1:[1]
The part where you ask patches to update their variable should not be done with a general ask patches
, as this will target all patches and either be called too seldom (if you do it just once per iteration of go
) or too often (if you do it every time a specific patch receives a turtle).
To easily target the specific patch that just received a turtle, and exactly the amount of times that it receives a turtle, make the moving turtle ask its new patch to perform the update. Or even better: given that turtles can directly access the patches-own variables of the patch they are standing on, you can do:
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
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Matteo |