# How Multi-Agent System Simulation Works in NetLogo: Lesson 23 Deep Dive

> Explore multi-agent system simulation in NetLogo lesson 23. Learn how autonomous agents create emergent behaviors like flocking and traffic flow with Microsoft's AI for Beginners.

- Repository: [Microsoft/AI-For-Beginners](https://github.com/microsoft/AI-For-Beginners)
- Tags: deep-dive
- Published: 2026-08-23

---

**Lesson 23 of Microsoft's AI for Beginners curriculum teaches multi-agent system simulation through NetLogo's three-layer architecture, where autonomous "turtle" agents execute commands in parallel to produce emergent behaviors like flocking, traffic flow, and viral spread.**

The `microsoft/AI-For-Beginners` repository provides a comprehensive introduction to artificial intelligence concepts, dedicating Lesson 23 specifically to multi-agent modeling using the NetLogo environment. Understanding this implementation provides foundational knowledge for designing autonomous systems where complex global patterns emerge from simple local rules. The lesson demonstrates how NetLogo's unique agent-based paradigm differs from traditional imperative programming by treating turtles, patches, and the observer as distinct computational entities.

## The Three-Layer Architecture of NetLogo Models

According to the source code in [`lessons/6-Other/23-MultiagentSystems/README.md`](https://github.com/microsoft/AI-For-Beginners/blob/main/lessons/6-Other/23-MultiagentSystems/README.md), every NetLogo simulation consists of three distinct conceptual layers that separate concerns between visualization, logic, and agent state.

### Interface Layer

The interface layer provides the visual environment where users interact with running simulations. As documented at lines 70-73, this layer includes the world grid display, interactive control widgets such as sliders and buttons, and real-time monitors or graphs tracking population metrics. This is where users click **Setup** and **Go** to control simulation execution.

### Code Layer

The code layer contains all procedural logic written in NetLogo's specialized language. Lines 78-82 define the handlers for standard control buttons, typically implementing `to setup [...]` for initialization routines and `to go [...]` for the main simulation loop that advances the model by one tick. This layer translates user interface actions into agent commands.

### World Layer

The world layer represents the actual simulation environment containing all agents. This layer operates as the computational engine, executing the logic defined in the code layer independently of the visual representation displayed in the interface.

## Understanding Agents in the World Layer

The world layer contains three distinct agent types, each serving specific computational functions within the multi-agent system simulation.

### Turtles

Turtles are mobile agents that navigate the world grid and execute autonomous behaviors. As shown at lines 37-41, you instantiate turtles using `create-turtles 10`, and command them collectively using `ask turtles [...]` blocks. When code executes inside an `ask turtles` block, every turtle in the agent set runs the instructions simultaneously, enabling true parallel processing across the population.

### Patches

Patches represent the static grid cells that comprise the world floor. Lines 86-88 describe how patches maintain state variables and can be queried or modified using `ask patches [...]` commands. Each patch occupies a fixed coordinate position and can store properties like color, resource levels, or terrain information that mobile turtles detect and respond to during simulation.

### Observer

The observer is a unique, omniscient agent that exists outside the spatial world grid. It executes button procedures like **Setup** and **Go**, manages global variables that persist across ticks, and can issue commands to control the entire population of turtles and patches simultaneously.

## Parallel Execution and Emergent Behavior

The distinguishing characteristic of multi-agent system simulation in NetLogo is its parallel execution model. When procedures run inside `ask` blocks, NetLogo executes the code simultaneously for every eligible agent rather than sequentially, creating emergent system-level patterns from simple individual rules.

### The Flocking Example

Lines 94-99 of the README describe the classic Flocking model that demonstrates how emergent behavior arises from parallel execution. Each bird (turtle) follows three simple rules implemented in the code layer:

- **Alignment**: Match the average heading of nearby turtles within a specified radius.
- **Cohesion**: Move toward the center of mass of neighboring turtles.
- **Separation**: Maintain minimum distance by turning away from turtles that are too close.

When these rules execute in parallel across hundreds of turtles each simulation tick, the individual agents self-organize into coherent, flocking groups without centralized coordination.

## Implementing Agent Behaviors

The lesson provides concrete code examples showing how to instantiate agents and define their behavioral logic using NetLogo's domain-specific language.

### Creating and Commanding Turtles

To initialize a multi-agent simulation, use the `create-turtles` primitive followed by command blocks that set initial properties:

```netlogo
create-turtles 10                 ; spawn 10 agents
ask turtles [
  forward 10                     ; each turtle moves 10 steps
]

```

This excerpt from lines 37-41 demonstrates how the observer creates the agent population and issues parallel movement commands that execute simultaneously across all ten turtles.

### Implementing Flocking Rules

The three flocking principles translate directly into procedural code that runs each simulation tick:

```netlogo
to flock
  ask turtles [
    ; Alignment: match heading of nearby turtles
    set heading mean [heading] of turtles in-radius 2
    
    ; Cohesion: move toward center of nearby turtles
    facex mean [xcor] of turtles in-radius 2 mean [ycor] of turtles in-radius 2
    
    ; Separation: avoid crowding
    if any? other turtles in-radius 1 [
      rt 180
    ]
    forward 1
  ]
end

```

This procedure demonstrates how each turtle queries its local neighborhood using `in-radius` reporters, calculates vector averages, and adjusts its state accordingly.

## Running the Simulation

Executing a NetLogo model follows a standardized two-step workflow defined in the Interface layer controls.

1. **Press Setup**: Calls the `setup` procedure to initialize the world state by clearing patches, resetting global variables, and creating the initial turtle population using `create-turtles`.
2. **Press Go**: Repeatedly invokes the `go` procedure, which typically calls behavior procedures like `flock` each simulation tick until the user stops the model.

## Summary

- NetLogo models consist of three layers: **Interface** (visual controls), **Code** (procedures), and **World** (agents).
- The World layer contains three agent types: mobile **turtles**, static **patches**, and the omniscient **observer**.
- Agent commands execute in parallel using `ask` blocks, enabling emergent behaviors from simple local rules.
- The Flocking example demonstrates **Alignment**, **Cohesion**, and **Separation** rules that produce complex group dynamics.
- Simulations run through standardized **Setup** and **Go** button procedures defined in [`lessons/6-Other/23-MultiagentSystems/README.md`](https://github.com/microsoft/AI-For-Beginners/blob/main/lessons/6-Other/23-MultiagentSystems/README.md).

## Frequently Asked Questions

### What is the difference between turtles and patches in NetLogo?

Turtles are mobile agents that move continuously around the world and execute autonomous behaviors, created with `create-turtles` and commanded via `ask turtles`. Patches are the stationary grid squares that comprise the world floor, each occupying a specific coordinate position (`pxcor`, `pycor`). While turtles navigate the environment and process behavioral logic, patches typically represent immutable terrain properties or resource distributions that turtles query using reporters like `patch-ahead` or `patch-here`.

### How does parallel execution work in NetLogo multi-agent simulations?

When code runs inside an `ask turtles [...]` block, NetLogo executes the instructions simultaneously for every member of the agent set rather than processing them sequentially. This parallel execution model, described in the context of lines 38-41, allows hundreds of agents to evaluate their individual rules and update their states simultaneously each simulation tick. This architecture is essential for producing the emergent system behaviors characteristic of multi-agent system simulation, as it prevents order-of-execution artifacts that would occur in sequential processing.

### What are the three rules of the Flocking model in Lesson 23?

According to lines 94-99 of the README, the Flocking model implements three behavioral rules executed in parallel: **Alignment** (matching the average heading of nearby turtles), **Cohesion** (steering toward the average position of neighboring turtles), and **Separation** (maintaining minimum distance by turning away from turtles within a critical radius). These simple vector calculations, when applied simultaneously across the entire population, generate realistic flocking behavior without any centralized coordination or leader election.

### Where can I find the assignment for Lesson 23?

The practical assignment for modifying NetLogo multi-agent models is located at [`lessons/6-Other/23-MultiagentSystems/assignment.md`](https://github.com/microsoft/AI-For-Beginners/blob/main/lessons/6-Other/23-MultiagentSystems/assignment.md) in the microsoft/AI-For-Beginners repository. This file contains exercises prompting learners to experiment with agent behaviors, such as modifying parameters in a virus spread simulation or adjusting the weights of the three flocking rules to observe how emergent patterns change with different local interaction rules.