Getting Started With NetLogo
Written by Mike James   
Monday, 11 March 2013
Article Index
Getting Started With NetLogo
Many Agents
Patches and Agent Sets
Variables

 

Now we place two buttons on the interface and set the first to the setup function and the second to the go function. Now when we click setup the system, i.e. the simulation, is reset and then then turtles are created. When we click the go button a step of the simulation is computed and displayed. By manually clicking the go button we can step thought the simulation but by changing the buttons type to Forever - simply click the Forever box - then the go function is called repeatedly until the button is clicked a second time. You can control the rate at which the go function is called using the speed slider at the top.

 

running

 

The result is quite a pretty pattern for such a simple program.

If you slow down the program you can also see the turtles each taking a turn to move. This is an important point. The simulation is not a true parallel algorithm where all the turtles move at the same time - they each take a turn. This can be important in some algorithms. By default each agent runs the complete list of instructions in the ask before the next turtle gets a go. You can change this so that each turtle obeys one instruction and then gives the next turtle a turn - this is ask-concurrent.

Also notice that the tick doesn't move on. i.e. simulation time doesn't advance until all of the moves have been completed.

Patches and Agent Sets

Turtles provide the all-purpose moving agent. They don't have to look like turtles, you can create any graphic you want for them. In this way turtles can be people, atoms, ants - whatever you need within the simulation.

Patches are parts of the simulation that don't move and they can be the most difficult idea for the beginner to come to terms with. You don't create patches like you create turtles. They are already part of the simulation. They form a rectangular grid that the turtles move over. You can set the number of patches and hence their size using commands in the setup function. You can also ask the patches to execute some code and this is the difficult part. 

For example to turn all the patches green you could use:

ask patches [
     set pcolor green
]

 

Suppose you want some of the patches. not all, to display as green how could you do this as all of the patches are going to obey the same code. There are a number of ways to do this but one interesting one uses an agent set.

So far we have used complete agent sets like turtles or patches but you can build smaller subsets of agents and patches and work with then using ask. For example

ask (patch-set patch 1 1 patch 2 2 patch 3 3)[
  set pcolor green
]

uses a patch set to select just three patches at positions 1,1,2,2 and 3,3 and sets them to green. 

As another example, this sets three random patches to green:

ask (patch-set one-of patches
             one-of patches one-of patches)[
     set pcolor green
]

This uses the one-of operator which selects a random member of an agent set.

As you might guess, there are lots of way of creating and manipulating agent sets and it would take far too long to cover them all here. However, once you know that agent sets exist and what they are for the documentation is quite good at explaining things.

Turtle Patch Interaction

The whole point of patches is that they provide an environment which the turtles can interact with. As a simple example suppose we have three random green patches then we could make the turtles bounce off:

ask turtles[
    pen-down
    if pcolor = green[ rt 180]
    fd 1
   ]

The if statement checks the patch color that the turtle is on and if it is green it turns the turtle through 180 degrees. 

You can use the patch color to form barriers and allow agents to bounce off for kinetics simulations and so on. Different colored patches can also be used to signify food or resources for the agents. There are also a range of keywords that let you retrieve the patch in front of a turtle and at given positions and so on. For example:

ask turtles[
 pen-down
 if [pcolor] of patch-ahead 1 = green [rt 90]
 fd 1
]

The of operator gets the value of the property for the particular agent. So in this case it retrieves the color of the patch 1 unit ahead of the turtle. 

bounce



Last Updated ( Monday, 11 March 2013 )
 
 

   
RSS feed of all content
I Programmer - full contents
Copyright © 2014 i-programmer.info. All Rights Reserved.
Joomla! is Free Software released under the GNU/GPL License.