|Getting Started With NetLogo|
|Written by Mike James|
|Thursday, 31 October 2019|
Page 2 of 4
Now we come to the first unusual feature of NetLogo. The IDE that you use to develop the program is part of the user interface. To allow the user to control the program you have created you place buttons and other controls onto the interface region and define what they do.
For example, to run the greeting function you would place a button on the interface area and in the dialog box that appears you simply enter the name of the function or NetLogo code you want to run when the button is clicked:
There are other important elements in the dialog box which we will return to but notice the Agents drop down This alters where the button sends its command - in this case to the observer which is the command line. So clicking this button has the effect of typing greet on the command line and hence the function runs.
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:
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:
This asks the turtle to leave a trail and move forward 5, turn right and move forward 5. The result is fairly obvious:
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:
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:
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.
|Last Updated ( Thursday, 31 October 2019 )|