To spawn turtles in NetLogo, you use specific commands that create new agent entities within your model. The primary way to do this is by employing the create-turtles
command for generic turtles or create-<breed>
commands for custom-defined turtle breeds. These creation commands are typically executed by the observer, often within a setup
procedure.
Understanding Turtle Creation in NetLogo
Spawning turtles is a fundamental step in building most NetLogo models, as turtles are the active agents that move and interact within the simulated environment. You dictate how many turtles appear and what their initial characteristics are.
Basic Turtle Creation: create-turtles
The most straightforward way to create turtles is using the create-turtles
command. You simply specify the number of turtles you want to create.
- Syntax:
create-turtles <number>
- Purpose: Generates a specified number of new, default turtles.
Example:
To create 100 generic turtles in your model, you would use:
to setup
clear-all ; Clears all agents and resets the world
create-turtles 100 ; Creates 100 new turtles
reset-ticks ; Resets the tick counter
end
By default, these newly created turtles will appear at the center of the world (patch 0 0) and face "north" (upwards). Their initial color will be white
.
Creating Custom Breed Turtles: create-<breed>
NetLogo allows you to define different "breeds" of turtles, each with its own unique characteristics and behaviors. Once you've defined a breed, you can create instances of that specific breed using a create-<breed>
command.
-
Define the Breed: First, declare your breed at the beginning of your code using the
breed
keyword.Example:
breed [sheep a-sheep] ; Declares a breed called 'sheep' (singular 'a-sheep') breed [wolves a-wolf] ; Declares a breed called 'wolves' (singular 'a-wolf')
-
Create Breed-Specific Turtles: After defining the breed, you can use
create-<breed>
to spawn them, similar tocreate-turtles
.- Syntax:
create-<breed> <number>
- Purpose: Generates a specified number of new turtles belonging to the custom breed.
- Syntax:
Example:
To create 50 sheep
and 5 wolves
:
breed [sheep a-sheep]
breed [wolves a-wolf]
to setup
clear-all
create-sheep 50 [ ; Creates 50 sheep
set color green
setxy random-pxcor random-pycor ; Place them randomly
]
create-wolves 5 [ ; Creates 5 wolves
set color red
setxy random-pxcor random-pycor ; Place them randomly
]
reset-ticks
end
Initializing Turtles with Specific Properties
When you create turtles, you often want to give them initial properties like color, size, position, or specific variable values. You can do this immediately after the create-turtles
or create-<breed>
command by enclosing a block of commands in square brackets []
.
Example:
Creating 20 turtles and setting their color, size, and random position:
to setup
clear-all
create-turtles 20 [
set color blue ; Sets their color to blue
set size 1.5 ; Makes them slightly larger
setxy random-pxcor random-pycor ; Places each turtle at a random x,y coordinate
set heading random 360 ; Gives them a random initial heading
]
reset-ticks
end
This method is highly efficient as it applies the commands to each newly created turtle before any other model logic runs.
Who Can Create Turtles? The Observer's Role
In NetLogo, only the observer can create new turtles. This means that commands like create-turtles
and create-<breed>
must be executed from the observer's context.
- Common Scenarios:
setup
procedure: Most frequently, turtle creation happens within theto setup
block, which is always run by the observer.- Observer Command Center: You can type these commands directly into the observer's command center to create turtles manually during model execution or debugging.
Turtles themselves, or patches, cannot directly create other turtles using these commands.
Summary of Turtle Creation Commands
Here's a quick reference for spawning turtles:
Command | Description | Example |
---|---|---|
create-turtles <number> |
Creates a specified number of default turtles. | create-turtles 100 |
create-<breed> <number> |
Creates a number of turtles of a specific custom breed. | create-dogs 50 |
... [commands] |
Initializes properties for newly created turtles. | create-turtles 20 [set color green setxy 0 0] |
Best Practices for Spawning Turtles
- Always
clear-all
: Start yoursetup
procedure withclear-all
to ensure a clean slate before creating new turtles. - Randomize Initial Positions: Unless specific initial placement is required, placing turtles at random
x
andy
coordinates usingrandom-pxcor
andrandom-pycor
(orrandom-xcor
andrandom-ycor
) is a common practice to avoid overcrowding. - Define
globals
for initial states: If many turtles share a common initial state or a variable that determines initial state, consider using aglobals
variable. - NetLogo Dictionary: For more detailed information on specific commands, refer to the NetLogo Dictionary.
By mastering these commands, you gain precise control over the initial population and characteristics of agents in your NetLogo simulations.