Practical 2 - Full Version

Table of Contents

1. Creating the world
2. Buttons and Procedures
3. Sliders and Variables
4. Creating Turtles and Patches
5. Making the Model Go
print

Practical 2 - Your First NetLogo Model


In this practical, we'll bring everything you've learned together and build a new model from scratch. Lots of the content for this part has been adapted from directly from the official Netlogo tutorial: http://ccl.northwestern.edu/netlogo/4.1/docs/tutorial3.html (part 3).

The model we will create is very simple. It will have some turtles in it (well, sheep actually) who will wander around aimlessly eating grass. Practical 3 will then extend the model to make it more interesting .

To start, move on to the first part: 1. creating the world.

The final model.

Practical 2, part 1 - Creating the World


Before creating a model, we need to configure the world, or environment. We do this by specifying how large we would like the world to be (how many patches) and by specifying how the coordinate system works.

  1. Open NetLogo and create a new model (File - New)
  2. The first thing we need to do is to set the size of the environment that we want to use.

    Setting up the environment
  3. Right click on the display (the part that would usually show the agents) and choose 'Edit'.
  4. You should see a window that allows you to configure the model world.

  5. Put the location of the origin (0,0) to be at the edge of the world on the bottom.
  6. Set the minimum x coordinate (min-pxcor) to zero.
  7. Set the maximum x and y coordinates (max-pxcor and max-pycor) to 20. This will give us a 20x20 cell grid.
  8. Tick the buttons so that the world wraps both horizontally and vertically. This means that if a turtle leaves one side of the world they come back in at the other side).
  9. Leave the other variables as they are. See the right image to check you have configured the environment correctly.
  10. Click OK. Now the world is ready.

That's all you need to do to prepare the environment. Check that you understand the questions below, and then move on to part 2 to create a button.

Questions

If you're not sure about any of the questions below, have a look at the NetLogo interface guide, particularly the section on views.

  1. If the world is a 20x20 grid, wow many patches will there be in the model?  
  2. If you had chosen to place the origin (0,0) in the centre of the world, what would the be the (x,y) coordinate of the patch in the bottom-left corner?  
  3. What about the patch in the top-right corner?  
  4. What do the 'wrap horizontally' and 'wrap vertically' boxes do? (If you're not sure about this, have a look at the NetLogo interface guide )  

Practical 2, part 2 - Buttons and Procedures

Creating a Button

Adding a button

In this section, you will create a button that will be used to set up the model.

  1. Click on the drop-down list next to 'Add' and select 'Button' (see left image)
  2. Move the mouse to where you would like the button to be and left-click to place it.
  3. In the new menu that appears you can specify what the button should do.

  4. Enter 'setup' in the commands box (without the apostrophes) - this will mean that a procedure called 'setup' is called when the button is clicked. You'll see what this means shortly.
  5. Adding a button
  6. The 'Display Name' is a label for the button. This will be displayed on the button and you can choose any name you like. I have chosen "Setup the Model".
  7. Click on OK to finish the button.
  8. You can see that the button is red which means it refers to a procedure (method, i.e. we will tell it to do something) that hasn't been created yet. We'll do that now.

    Procedures

    One thing that hasn't been covered yet is procedures. These are a way of grouping several commands together that perform a common task. One example is setting up the model: there will probably be some commands to create the turtles, some for the patches, and some to create other parts of the model. We can group these all together in a single procedure.

  9. Click on the 'Code' tab. This is where all the commands for our model go
  10. We are going to create a new procedure called 'setup'.

  11. Enter the following text into the 'Code' tab: to setup
     print "Setting Up Model"
    end
  12. Setup message

    The code above does three things:

    1. The first line (to setup) says whatever comes next will be part of a procedure called 'setup'.
    2. You should be familiar with the second line (print "Setting Up Model") - it will print a message to the command centre.
    3. The third line (end) says that we have come to the end of the 'setup' procedure. Any more commands that come afterwards are not part of 'setup'.
  13. Check this works by clicking on the 'Interface' tab. Has the button text gone from red to black?
  14. Click on the button. You should see a message displayed on the command centre (see right image).

Eh??

It's worth having another look at what is going on above, as this goes to the heart of how NetLogo works. The first thing that we did is create a button. When creating it, we told the button that if someone presses it, it should try to run a procedure called 'setup'.

Then, in the Code tab, we created a new procedure called 'setup'. So, if someone presses the button, all the commands that are part of the 'setup' procedure will run. The image below illustrates this graphically.

Pressing the setup button starts a procedure

Have a go at the activities below. When you're comfortable that you can answer them, move on to part 3.

Activity

  1. Write the NetLogo code that creates a new procedure called 'hello' to print the text "Hello World" to the Command Centre.  
     
     

Practical 2, part 3 - Sliders and Variables


You've now seen how buttons and procedures work together. Now, you will see how sliders connect up to variables (if you need to refresh your memory, there is information about variables in the lecture or in the previous section).

  1. Create a slider in the same way that you created a button.
  2. This slider is going to tell our model how many turtles should be created initially. Set the following values (as illustrated below)
  3. A new slider
  4. Press OK to create the slider. Note: if you would like to change its position, right click on it and choose 'Select'. You can then move it around.
  5. Now the slider is ready. Next we'll go back into the model code and make some improvements so that it does something more useful.

That was easy enough. In the next section we'll look more closely at how we can use the slider in the model. But first, have a go at the activities below.

Activities

These activities will check that you're familiar with how to write procedures. Before starting, type the following code into the Command Centre:

create-turtles 10 [ set xcor random 10 set ycor random 10 ]

This will temporarily create ten new turtles - we need these to see if your procedures are working properly. For the following questions, write the required code in the 'Code' section of NetLogo, and then run your commands from the Command Centre by simply typing the name of the procedure. For example, if you have created a procedure called do-something you simply type 'do-something' in the Command Centre to run it.

  1. Write the NetLogo code that creates a new procedure called 'hello' to print the text "Hello World". Once you have written the procedure, typing 'hello' into the Command Centre will run it.  
     
     
  2. Write a procedure called 'make-blue' that tells all the turtles to turn (blue). (For this, you will need to use the ask command as we did in the last practical). Once you have written the procedure in the 'Code' tab, type 'make-blue' into the Command Centre to run it.  
     
     
  3. Write a procedure called 'blue-then-move' that first makes all the turtles go blue, and then moves them forward 1 step. (Hint: the command fd 1 tells a turtle to go one step forward).  
     
     

Practical 2, part 4 - Creating Turtles and Patches


In this part of the practical, we will start to create the fundamental components of our model: the turtles and the patches.

  1. Click on the 'Code' tab. You should see the setup procedure. We will now improve it so that it does something more useful.
  2. Change the setup function so that it reads: to setup
     print "Setting Up Model"
     clear-all
     setup-patches
     setup-turtles
    end
  3. The clear-all command tells NetLogo to reset everything ready for us to create a new model. The other two lines (setup-patches and setup-turtles) call two new procedures that will contain commands to set up the turtles and patches. We could put these commands directly into the setup procedure, but by putting them into their own separate procedures it makes the model code easier to understand.

  4. Here is the code for the two new procedures. Copy it directly after the end of the setup function. to setup-patches
     ask patches [ set pcolor green ]
    end

    to setup-turtles
      create-turtles initial-number-of-turtles
      ask turtles [
       set shape "sheep"
       setxy (random 20) (random 20)
       set color blue
      ]
    end
  5. The code above should be fairly self-explanatory. The first procedure (setup-patches) does one thing: it asks all the patches to change their colour to green. This means that each patch will represent a square of grass, ready to be eaten by the turtles.

    The second procedure (setup-turtles) creates the turtles. There are some new commands in there that you wont be familiar with:

    After setting up the mode: turtles and some grass.

    Now, try going back to the 'Interface' tab. If you have made any mistakes with the model code, NetLogo will tell you about them. Otherwise you will see the main interface again.

  6. Press the setup button. You should see a few turtles created around the grid.
  7. Press the setup button again. What happens?

That's almost everything. To finish the basic model, move onto the final section to get the sheep moving around the environment.

Activities

  1. Write a procedure called 'make-white' that tells all the turtles to turn white. (For this, you will need to use the ask command as we did in the last practical).  
     
     
  2. Write a procedure called 'white-then-move' that first makes all the turtles go white, and then moves them forward 1 step. (Hint: the command fd 1 tells a turtle to go one step forward).  
     
     

Practical 2, part 5 - Making the Model 'Go'


To finish the model we need to create a go button. This will start the model running.

  1. Create a new button in the same way that you created the setup button. In the 'commands' section, write go. This will make the button start a procedure called 'go'. You can give the button any name you want (typically people use 'Go', but you could use 'Begin model' or whatever, it doesn't matter).
  2. Important: tick the 'Forever' box. This will mean that the procedure is run over and over again until you actually stop it. If you don't tick this box the button will make the model run for one iteration (time step) and then stop.
  3. Press 'OK' to create the button.
  4. Go back to the 'Code' tab and add the following procedure to the code. It doesn't matter where it goes, but you would usually put it after the procedures that set up the model. to go
      ask turtles [
       rt (random 360)
       fd 1
       if pcolor = green [
        set pcolor brown
       ]
      ]
    end
  5. There are some more new commands in there, but they are quite easy to explain.

    Built-in slider to change the speed of the model.
  6. Finally, go back to the main interface tab and click on the 'go' button. The model probably runs too quickly to see what is happening and the whole world suddenly turns brown. To stop this, move the slider at the top of the program (just below the 'Interface', 'Info' and 'Code' tabs) to the left. The reset the model and go again, you should be able to see the sheep moving around at a more reasonable pace.

That's it! You now have a very simple simulation with some turtles that will move around and eat grass. With a few small additions to the code it is possible to have grass that grows and can be eaten, turtles that die if they do not have enough to eat, and a graph to show how many turtles are alive. We'll look at these changes next time. If you have the energy, you can move straight on to practical 3. Or, if you are ready to either cry or destroy the computer in front of you, go and have another cup of tea.

The final model.