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

Hello Turtle

Now you have the basic idea of how programs are entered into NetLogo and how they are run and controlled. Notice that there is no run button apart from the one you create and customize.

Now let's move on to a simple program which uses an agent.

An agent is an object that can have a visible presence on the display. NetLogo has four types of agent - turtles, patches, links and the observer.

At the moment let's focus on the turtle agent. These are agents that move around the world and they have a position, color and other properties. If you have encountered turtles perhaps as part of turtle geometry you will be familiar with how turtles move. They have a set of methods that can be used to make the turtle turn though various angles and move forward a specified amount. 

It is easier to see how this all works with a simple example.

First we need a function that initializes or sets up the program:

to setup
 clear-all
 create-turtles 1
end

The clear-all sets everything back to its initial condition. It is important to realize that NetLogo like the original Logo is a persistent environment. That is if you define a variable or make changes then these changes persist between runs of the any function. In fact it is better not to think in terms of running a program. You simply execute functions one after another and these change things.

To get rid of any earlier changes you use the clear-all command and then we create a single turtle. This creates a turtle in the middle of the display area but with a random orientation.

Now we have to find a way to run the program. The simplest way is to type setup in the command center. A more usual way is to create a button that runs setup. If you do this you will see that each time you click the button you see new turtle with a random orientation and color. If you take out the clear-all command they you will see that the turtles accumulate - i.e. you get one more each time you click the button.

The next step is to use some turtle methods to move the turtle. This is where the nature of NetLogo shows itself again. In a standard object oriented language you would start using individual turtle methods. In NetLogo you ask the turtle to carry out a set of commands. So for example:

to setup
  clear-all
  create-turtles 1
  ask turtles[
    pen-down
    fd 5
    rt 90
    fd 5
  ]
end

This asks the turtle to leave a trail and move forward 5, turn right and move forward 5. The result is fairly obvious:

 

turtle2

 

One Turtle Good - Many Turtles Better

Now we come to the clever and powerful part of NetLogo. You may have wondered by the ask command uses the term turtles in the plural. The reason is very simple, you can ask all of the turtles you have created to perform the same set of instructions.

To see this in action we just need to create multiple turtles. So change the program to read:

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

Notice now that we are creating 10 turtles not just one. When the ask command is executed all of the turtles obey the instructions which produces:

 

turtle10

 

You can now begin to see why NetLogo is different and more powerful than you might expect. As well as being a complete programming language of the sort you may have used before it has agents as an additional construct. You can create large numbers of agents and give them all a program to execute. Agents can breed, collide with obstacles and generally interact with each other.

You can monitor the state of the system and gather statistics which you can display on a plot to show the user what is happening. You can add controls, buttons, sliders and so on to allow the user to interact with the agents and see what the outcome is.

In short NetLogo gives you what you need to create agent based simulations.

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.



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.