In NetLogo, sprout
is a fundamental patch-only primitive that allows patches to create new turtles directly on themselves. It's a powerful command primarily used when you want the environment (patches) to initiate the creation of agents, such as for population growth, seeding, or initializing models where objects appear from specific locations.
Understanding the sprout
Command
sprout
is unique because it must always be used within the context of an ask patches [...]
block. This ensures that the command is executed by individual patches, each potentially creating one or more new turtles at its own location.
- Patch-Exclusive: Only patches can execute the
sprout
command. You cannotsprout
from a turtle or the observer directly. - Turtle Creation: When a patch sprouts, it creates new turtles on its own coordinates (
xcor
,ycor
). - Initial Properties: Newly sprouted turtles automatically inherit their
xcor
andycor
from the patch that created them. By default, they are white and have a random heading. You can easily set their other properties immediately after creation.
How sprout
Works and Its Syntax
The sprout
command offers flexibility, allowing you to specify how many turtles a patch should create and to assign initial properties to these new turtles.
Basic Syntax
The simplest form of sprout
creates one new turtle on the executing patch.
ask patches [
sprout 1 [
set color green
set size 1.5
]
]
Creating Multiple Turtles
You can specify the number of turtles a patch should sprout.
ask patch 0 0 [
sprout 5 [
set color red
set shape "circle"
]
]
This example tells the patch at (0,0)
to create five new red circle turtles on itself.
Setting Initial Turtle Properties
Within the square brackets [...]
following sprout
, you can define properties for the newly created turtles. This is crucial for giving your agents distinct characteristics from the moment they are born.
to setup
clear-all
ask patches [
; Patches with certain conditions sprout specific turtles
if pxcor > 0 [
sprout 1 [
set color blue
set size random 2 + 1 ; Size between 1 and 3
set breed soldiers ; Assign to a specific breed
]
]
]
reset-ticks
end
Key Characteristics of sprout
Feature | Description |
---|---|
Agent Type | A primitive specifically for patches. |
Action | Creates new turtles. |
Location | New turtles are created directly on the sprouting patch's coordinates. |
Context | Must be called within an ask patches [...] block or by a patch itself (e.g., to go ask one-of patches [ sprout 1 ] ). |
Default State | New turtles are white, have a random heading, and are positioned at the creating patch's xcor and ycor . |
Primary Use | Ideal for initializing environments with agents, simulating birth/reproduction tied to specific locations, or dynamic population growth based on environmental conditions. |
Practical Applications and Examples
sprout
is incredibly versatile for various modeling scenarios:
- Initial Population Seeding: To quickly populate your world with agents at the start of a simulation.
to setup-world clear-all ask patches [ if random 100 < 10 [ ; 10% of patches sprout a tree sprout 1 [ set color brown set shape "tree" set size 2 ] ] ] reset-ticks end
- Resource Generation: Imagine patches that represent fertile ground, continuously sprouting "food" turtles.
to grow-food ask patches with [pcolor = green] [ if count turtles-on self < 5 [ ; If less than 5 food items on this patch sprout 1 [ set color yellow set shape "dot" set size 0.5 set breed food-items ] ] ] end
- Reproduction and Growth: When a "mother" patch (or a patch containing a "mother" agent) is responsible for creating new offspring.
to reproduce-plants ask patches [ if pcolor = 75 and random 100 < 5 [ ; Green patches have a 5% chance to sprout a new plant sprout 1 [ set color green + 2 set shape "flower" set size 1 set breed plants ] ] ] end
Why Use sprout
Instead of create-turtles
?
While create-turtles
can also create new turtles, sprout
is distinct because it always ties the creation to a specific patch.
create-turtles
: Creates turtles from the perspective of the observer (or a turtle), typically at(0,0)
by default, and then you have to move them. It's more about global creation.sprout
: Creates turtles from the perspective of the patch itself, directly at that patch's location. This makes it ideal for localized creation events where the environment's state dictates where and what new agents appear.
By understanding and utilizing sprout
, you can build more complex and spatially-aware NetLogo models where the environment plays an active role in shaping the agent population. For more details, you can refer to the NetLogo Dictionary.