Beginners Interactive NetLogo Dictionary
Farsi / Persian
NetLogo Models Library:
This example shows how to divide the world in a variable number of regions and keep turtles confined to the region they start in.
The world is separated in regions divided by vertical lines. When a turtle reaches the left or right boundary of a region, it crosses to the other side, thereby always staying in the same region.
Choose the NUMBER-OF-REGIONS and the NUMBER-OF-TURTLES-PER-REGION that you want, click SETUP, and then GO.
watch one of the turtles and see it jumping to the other edge of the region when it reaches its boundary. Running the model at slower speed makes this easier to see.
The NUMBER-OF-REGIONS slider is currently capped at a maximum of 32 regions. This number creates narrow regions: only one patch wide! There is still enough room for all of them, however. What do you think would happen if you increased the maximum to a bigger number? Try it. Does the model still work? Why?
When a turtle reaches the left or right boundary of a region, they "wrap" to the other side. That's not the only possible behavior: they could also just stop there.
The current model only allows vertical regions. Changing to use horizontal regions instead should be fairly easy, but how about a making "grid" of regions, where you would specify a number of rows and a number of columns?
Look at the Bug Hunt models in the library: they use a special breed of turtles called "dividers", with a custom turtle shape, to cover the region divisions and make the wrapping animation appear smoother. You can try to do the same here.
map primitive is used in the
region-definitions procedure to loop through two copies of the divisions list at once, thereby scanning through "pairs" of divisions.
foreach primitive is used in various places to loop through regions and their indices (generated with
n-values). This advantageously replaces a
while loop, as it is both shorter and less error-prone.
The Bug Hunt models in the ModelSim folder of the library use a version of this code to separate the bugs in two distinct regions.
This example was developed by Nicolas Payette, inspired by Michael Novak's work on various ModelSim models.