Agents and Agenset
Agenset
In NetLogo, an agentset is a collection of agents that can be either turtles, patches, or links. An agent is an individual entity that can interact with others based on the rules of the model. Agentsets provide a powerful way to group agents so that you can perform operations on multiple agents at once.
Agent vs. Agentset
- Agent:
- An agent is a single entity within the NetLogo world. The types of agents include turtles, patches, and links.
- Each agent has its own variables (e.g.,
color
,heading
,xcor
for turtles;pcolor
for patches) and can execute commands.
- Agentset:
- An agentset is a collection or group of agents of the same type.
- Unlike lists, agentsets are unordered, meaning they do not preserve a specific sequence of agents.
- Agentsets can be empty or contain any number of agents, and they allow for operations to be performed on multiple agents simultaneously.
Common Operations with Agentsets
- Creation:
Agentsets can be dynamically created using primitives like
turtles
,patches
,links
, or filtering expressions.let all-turtles turtles let green-patches patches with [pcolor = green] let nearby-turtles turtles in-radius 5
- Iteration:
Use
ask
to perform operations on every member of an agentset.ask turtles with [color = blue] [ set color red ]
- Random Selection:
Use
one-of
to select a random agent from an agentset.let random-turtle one-of turtles
- Counting:
Use
count
to determine the number of agents in an agentset.let num-green-patches count patches with [pcolor = green]
- Boolean Checks:
Use predicates like
any?
andall?
to test conditions across agentsets.if any? turtles with [size > 2] [ ; do something ]
Example of Using an Agentset
Here’s a simple example where turtles are created and a specific operation is performed on a subset based on an agentset:
to setup
clear-all
create-turtles 100 [
setxy random-xcor random-ycor
]
end
to color-green-patches
let green-patches patches with [pxcor mod 2 = 0 and pycor mod 2 = 0]
ask green-patches [
set pcolor green
]
end
In this example: - An agentset green-patches
is created consisting of patches with even x and y coordinates. - All patches in this created agentset are then changed to green using ask
.
Agentsets allow for efficient management and manipulation of agents in NetLogo, making them indispensable for simulations involving multiple interacting entities.
Self vs Myself
One of the features that make NetLogo robust is its agent context flexibility, enabled by constructs like self
and myself
. Understanding how to use these constructs properly can significantly enhance your model’s precision and behavior.
What is self
in NetLogo?
self
refers to the agent executing the current command or block of commands. It’s context-specific, which means its reference changes based on the currently active agent in an ask
block.
When to Use self
- Within
ask
Blocks: Useself
to refer to the properties or variables of the current agent. - Clarity:
self
can help make your code more readable, clarifying which agent’s variables you are accessing.
Example of self
to setup
clear-all
create-turtles 5
ask turtles [
; Set each turtle's color based on its 'who' number
set color [ who ] of self
]
end
In this example, each turtle sets its color based on its unique identifier (who
). Here, self
simply makes it clear that we’re looking at the property of the turtle currently executing the commands.
What is myself
in NetLogo?
myself
is used to refer to the agent that initiated an ask
block containing another ask
. It’s particularly useful in nested ask
scenarios where you’re working with multiple layers of agent interactions.
When to Use myself
- Nested
ask
Blocks: Usemyself
when you have nested actions and need to reference the original agent from an outer loop or block. - Maintaining Context:
myself
is ideal for referring back to the initiator agent in complex, layered interactions.
Example of myself
to setup
clear-all
create-turtles 10 [
setxy random-xcor random-ycor
]
ask turtles [
ask patches in-radius 2 [
; Change patch color based on the who number of the turtle originating the ask
set pcolor [ who ] of myself
]
]
end
In this example, each turtle changes the color of nearby patches based on its who
number. The usage of myself
ensures that we refer back to the initiating turtle, even though the innermost commands are executed by patches.
Key Differences Between self
and myself
- Agent Context:
self
is always the agent executing the current block, whilemyself
reverts to the originating agent in nested contexts. - Use Cases:
self
is straightforward for single-layer contexts, whereasmyself
is indispensable for multi-layer interactions involving one agent acting upon others.
Conclusion
Understanding self
and myself
helps maintain clarity and proper context in NetLogo simulations, especially as your models grow in complexity. By mastering these constructs, you enhance your ability to create accurate and efficient simulations, allowing you to better explore and understand the phenomena you’re modeling.
Whether you’re a beginner trying to grasp the basics or an experienced modeler optimizing your code, recognizing when to use self
versus myself
is fundamental. Experiment with these examples in your projects to solidify your understanding and see the difference they can make!
Feel free to experiment with the concepts and examples given above to see how they can fit into your particular NetLogo projects. Enjoy your modeling adventure!