Ora

How do you spawn turtles in NetLogo?

Published in NetLogo Turtle Creation 4 mins read

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:

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.

  1. Define the Breed: First, declare your breed at the beginning of your code using the breed keyword.

    Example:

  2. Create Breed-Specific Turtles: After defining the breed, you can use create-<breed> to spawn them, similar to create-turtles.

    • Syntax: create-<breed> <number>
    • Purpose: Generates a specified number of new turtles belonging to the custom breed.

Example:
To create 50 sheep and 5 wolves:

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:

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 the to 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 your setup procedure with clear-all to ensure a clean slate before creating new turtles.
  • Randomize Initial Positions: Unless specific initial placement is required, placing turtles at random x and y coordinates using random-pxcor and random-pycor (or random-xcor and random-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 a globals 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.