Getting Started With NetLogo
Written by Mike James   
Thursday, 31 October 2019
Article Index
Getting Started With NetLogo
IDE
Ticks And Go
Variables

Ticks And Go

So far we have just used a single button to setup and run our program. In most cases there are two distinct parts to a simulation - setup and go. The setup routine initializes everything ready for the simulation to start and the go does the actual running of the simulation.

The setup part is generally easy in that it just has to happen once and so you can associate it with a button exactly as we have been doing.

The go part is a bit more complicated because generally what we have is a single step of the simulation that has to be run more than once to allow things to develop. Now you could do this by writing the code for a single step of the simulation and enclosing it in a loop so that it repeats.  However this is such a standard requirement for a simulation that NetLogo has a special built in mechanism.

First we separate out the setup and go functions:

to setup
  clear-all
  create-turtles 10
   reset-ticks
end
to go
    ask turtles[
    pen-down
    fd 5
    rt 90
    fd 5
  ]
tick
end

Notice that we have now got two extra commands - reset-ticks and tick. The system keeps count of each step of the simulation and shows it at the top of the window. The reset-ticks command sets the count to zero and tick increments it - you don't have to use this but it is very useful in most simulations.

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

<ASIN:1871962536>

<ASIN:1871962587>

<ASIN:1871962544>

<ASIN:1871962552>



Last Updated ( Thursday, 31 October 2019 )