Intro to NetLogo Programming for Educators

Jacob Kelter
Biology, Chemistry, Engineering, Environmental Science, Mathematics, Physics
1 hour
Teachers and Professors
v2

Overview

This is an introduction to NetLogo and agent-based modeling (ABM) for educators interested in learning ABM and how to use it pedagogically. The lessons starts with a short survey to assess prior experience with modeling and computer programming and then guides the learner through the basics of NetLogo and ABM. 

Standards

Computational Thinking in STEM
  • Computational Problem Solving Practices
    • Computer Programming
    • Troubleshooting and Debugging
  • Modeling and Simulation Practices
    • Constructing Computational Models

Activities

  • 1. Pre-Survey
  • 2. Creating and Moving Turtles
  • 3. Procedures
  • 4. Patches
  • 5. What are Agents

Student Directions and Resources


This is an introduction to NetLogo programming and agent-based modeling. You will first answer a few questions about your prior experience with programming and modeling. Then you will learn the basics of NetLogo.

1. Pre-Survey



Question 1.1

How much prior experience do you have with computer programming? For example, python, C, Java, Matlab or even HTML.

  None
  A little, but not recently
  A little, and it was recent
  Some, but not recently
  Some, and it was recent
  A lot


Question 1.2

How much experience do you have creating agent-based models?

  I don't know what an agent-based model is
  None (but I know what they are)
  A little
  Some
  A lot


Question 1.3

What are your expectations around learning NetLogo and agent-based modeling?



Question 1.4

Which NetLogo platform are you using?

Note: The core functionality of NetLogo Web and NetLogo Desktop are the same, so it doesn't really matter for the purpose of this lesson, but it makes sense to learn on the platform you intend to use with students:

  • If your students have chromebooks, you will want to use NetLogo Web. (Everything will be here in the browser so just continue ahead)

  • If your students have laptops that can download desktop applications, you may want to use the desktop version because it is faster and has some features that haven't been added to the web version yet. You can download NetLogo Desktop here.

 

  NetLogo Web
  NetLogo Desktop


2. Creating and Moving Turtles


This page will introduce you to turtles, one of the most important things in NetLogo. Follow along with the steps below.

 

Notes if you are using NetLogo Web:

  • You might want to open NetLogo Web in a new window here so that you don't have to keep scrolling between the NetLogo interface and the questions below. 
  • At the top of the NetLogo Web interface, there is text that say "Commands and Code:"  and then says either "Right Side", or "Bottom".  You can click that text to toggle between the two options. 


Question 2.1

Creating and Moving Turtles

Click the Command Center and create a turtle by typing create-turtles 1. This created 1 turtle. Because this is something you do a lot in NetLogo, there is an abbreviation: crt. Try typing crt 50. That created 50 turtles.

The turtles are all facing random directions by default. What do you think it will look like if they all move forward the same distance? Type your answer below and then we'll try it in the next question.

 

Note: if you are using NetLogo Web, the Command Center is the first horizontal purple bar. If you are using NetLogo Desktop it is at the bottom of the interface.

 



Question 2.2

Moving Turtles

Now let's ask the turtles to move. In the Command Center, type ask turtles [forward 10]. There is an abbreviation for this one too. You can just type ask turtles [fd 10], and it will do the same thing. To clear everything away, type clear-all, or ca for short (not every command has an abbreviation, but a lot of the most common ones do).

What does it look like when you create a bunch of turtles and ask them to all move forward the same distance? Why is this?

 



Question 2.3

What do you think it will look like if you create a bunch of turtles and then have them each move forward a random distance instead of the same distance?



Question 2.4

The command random number will produce a random integer greater than 0 and less than number. So random 10 will output a random integer greater than 0 and less than 10. The command random-float number does the same thing, except the output will be a decimal number instead of an integer.

Try creating a bunch of turtles and asking them to move forward a random distance.  What does it look like?  Does it look different if you use random vs random-float? Does it look different as you add more turtles? Try to explain why you see what you do for each question. 



Question 2.5

Emergence

The patterns you saw in the previous questions are a simple example of "emergent phenomena." You never told the turtles to make the shape they did. Each turtle just followed a simple command of moving forward, but a macro-level pattern emerged from all the individual actions. We will see many more examples of emergent phenomena. Can you think of an example of an emergent phenomenon? What about something that isn't an emergent phenomenon?



Question 2.6

Moving individual turtles and turning

  • You can also ask individual turtles to do things. Try typing ask turtle 1 [fd 5]. Each turtle has a number. Go ahead and move a few turtles individually. 
  • To have turtles turn, you have to ask them to turn a number of degrees. For example, rt 90  to turn right by 90 degrees, or lt 30 to turn left by 30 degrees (rt stands for right turn and lt for left turn). Try asking some turtles individually and as a group to turn and move.

Using just the commands you've learned, how might you make a turtle move in a circle?



Question 2.7

If you had any issues on this page, describe them below. If not, type something like "no problems" and continue on.



3. Procedures


This page will introduce you to procedures, one of the most important ideas in NetLogo and programming in general. Follow along with the steps below.

 


Question 3.1

Creating Procedures

If you had to type every command each time you wanted it to run, it would take forever to run computer programs. Instead, we can put a bunch of commands together into a procedure. Open up the NetLogo Code tab and we'll go through the steps of writing a procedure.

  • You start a procedure by typing the keyword to and then the name of the procedure. You end the procedure with the keyword endLet's make a procedure called move-turtles. First write to move-turtles. Then on the next few lines, ask the turtles to move forward 0.175 and turn right 1. (use these exact numbers for now to prepare for a later question). Then on a new line write end.
  • If you are using NetLogo Web, click the recompile code button at the top of the NetLogo Code tab when you finish writing the procedure; if you are using NetLogo Desktop you don't have to do anything.
  • Now open the command center back up and type move-turtles. Each time you call move, all the commands inside the move procedure will get run.

Copy and paste your move procedure below. 



Question 3.2

Buttons

To make things even easier, we can create a button to call a procedure. 

  • If you are using NetLogo Web, first click the lock button that looks like this:  so that you enter authoring mode. Then you can right click on the green area and click Create Button. A window for creating a button will appear. In the commands box type move-turtles and press okay.
  • If you are using NetLogo Desktop, you can just right click on the white area next to the model "view" and click Button. A window for creating a button will appear. In the commands box type move-turtle and press okay. 

Now you can click the button to call the procedure. 

 



Question 3.3

The Setup Procedure

As a convention, almost every NetLogo model has a procedure called setup with an accompanying button. This procedure is to set up the "world" of the model.

  • Write a procedure called setup. For now, just clear everything and then create some turtles. Ask them to move forward 10 and turn right 90. Use these exact numbers for now to prepare for the next questions. (If you are using NetLogo web, don't forget to recompile code afterwards)
  • Create a button for you setup procedure.

Each time you click the setup button, all the existing turtles should be cleared and new ones created.

Copy and paste your setup procedure below.



Question 3.4

The Go Procedure

As a convention, almost every NetLogo model also has a go procedure and an accompanying button. This is generally the procedure used to make a model run continuously. It makes sense to have a unit of time in a model when it runs continuously. In NetLogo this unit is called a tick.

  • To reset the clock in our model add the command reset-ticks  to your setup procedure.
  • Create a procedure called go. For now just call your move-turtles procedure and the tick command. 
  • Create a button for your go procedure. In the window for creating the button, check the box that says "forever" and also check the box that says "disable until ticks start". (If you closed the box, you can right click on the button and click "edit" to open it again).

When you click the go button, it should now call the go procedure repeatedly. Each time the go procedure is executed, the ticks increase by 1, because you called the tick command inside the go procedure. 

What behavior do you see? Were you surprised by it? Why is this behavior occurring? Answer in the box below.  



Question 3.5

Predicting effects of small changes

What do you think will happen if you change the code of your move-turtles procedure so that the turtles move forward a little bit more each tick? Write your prediction below. 

 



Question 3.6

Integrative Understanding

Change your move-turtles procedure so that turtles move forward slightly more each tick (something like 0.22). What pattern emerges? (You can use the Model Speed slider at the top of the model to make the model run faster or slower).

Most people do not predict the resulting pattern. Some predict that the turtles will move in a smaller or larger pattern, but rarely the actual emergent pattern of a pulsating circle. This illustrates one of the difficulties in understanding emergent systems. It is difficult to predict how a small change enacted by many agents will "integrate" to form a larger pattern. This is sometimes called "integrative understanding," as it parallels the cumulative integration of small differences in calculus.

Can you think of a real world system in which integrative understanding is difficult?



Question 3.7

Changing Colors and Drawing Paths

You can change turtles' color with  ask turtles [set color color]where color can be any of: grey, red, orange, brown, yellow, green, turquoise, cyan, sky, blue, violet, magenta and pink. There are other ways of representing colors with more flexibility described here in the NetLogo Programming Guide

  • Using the command center, ask some turtles to set their color.

 

Try asking the turtles to pen-down. What happens? Try pen-erase and pen-up as well. If you want you can read about these commands here.

  • Try asking the turtles to pen-down and then run your model. What do you see? Does this change your understanding of the emergent pulsing-circle pattern?


Question 3.8

If you are using NetLogo Web, copy your code here, because when you leave this page the code you entered in the NetLogo Code tab won't be saved. If you are using NetLogo Desktop, still copy your code here, but you can also save your model on your computer if you want to.



Question 3.9

If you had any issues on this page, describe them below. If not, type something like "no problems" and continue on.



4. Patches


This section will introduce you to patches, the second main element of NetLogo.


Question 4.1

Changing Patch Color: pcolor

Patches form the background of any NetLogo model. Right now they are all black, so the "view" just looks like a big black box, but it is actually made of many patches. You can ask patches to do things just like you can ask turtles to.

  • Patches have a color property that can be changed, similar to turtles, but it is called pcolor. Ask the patches to change their color like this ask patches [set pcolor color].  Individual patches are identified by x and y coordinates. For instance you could ask patch 0 0 [set pcolor color].

 

  • Another way to represent colors in NetLogo is as a number between 0 and 140. For example, you could ask a patch to [set pcolor 20].

 

  • Each patch has a property called pxcor and pycor that refers to its x and y coordinates. Try writing a procedure to ask patches to set their pcolor based on their pxcor and/or pycor. Copy your procedure below.

 



Question 4.2

Asking Neighbors for Things

Patches can ask other patches to do things (Turtles can too). This is one way to have patches interact with on another.

  • Ask a specific patch to ask its neighbors to set their pcolor like this ask patch 5 3 [ask neighbors [set pcolor color]]. (where color is a specific color). 
  • Whenever you ask a patch to do something, any properties inside the brackets (like pcolor) belong to the patch being asked. So, in the code in the previous bullet point, each neighbor of patch 5 3 was asked to set its own pcolor. But what if patch 5 3 wants to ask its neighbors to change their pcolor to match its own? For this, it can ask the neighbors to set their pcolor to [pcolor] of myself. This is like saying "set (your) pcolor to the pcolor of me." 
  • Try asking a specific patch to ask its neighbors to set their pcolor to its pcolor. Copy and paste the code you used to accomplish this.

 



Question 4.3

Procedures with Patches

Now you will write some procedures for patches.

  • Write a setup procedure, and create a button for it, that sets the world up with patches of at least two different colors. You could do this by setting pcolor based on pxcor/pycor,  by using a random number, or any other way you think of. 

Copy and paste your setup procedure below. 



Question 4.4

Go procedure

  • Now write a go procedure that asks patches to ask their neighbors to change their color. You can ask all 8 neighbors with neighbors, or just the east, west, north and south neighbors with neighbors4. You can also ask just one of these neighbors by asking one-of neighbors to do something.
  • Make a button for the go procedure. Have it run forever, and also disable it until ticks start.

Copy the code of your go procedure below.

What behavior do you see? Why does this happen? Describe below.

 



Question 4.5

If you had any issues on this page, describe them below. If not, type something like "no problems" and continue on.



5. What are Agents


The previous pages introduced you to turtles and patches and some of the things they can do. Turtles and patches are two of the types of agents in NetLogo. Agents are beings (in this case computational beings) that can follow instructions. The other two types of agents in NetLogo are links and the observer. We won't discuss links in this lesson. You can think of the observer as seeing things from your perspective. When you were typing commands into the command center, you were asking the observer to ask other agents to do things. Read the short section in the Programming Guide on agents to fill in a few pieces of information we haven't covered. You will also start getting familiarized with the Programming Guide, which will be very useful moving forward.

 

If you have time and want to, try creating a new NetLogo program using what you've learned. It doesn't have to be modeling anything necessarily, it could just be to make a cool visual effect. In fact, there is a whole branch of art that uses processes and algorithms to create artwork.

Next lesson we will start making models of natural phenomena. Whether you write a new program or not, please answer the last few questions below.

 


Question 5.1

If you wrote a new program using what you learned, copy and paste your code below and describe in a couple sentences what you did. If not, just say why you didn't (e.g. busy, didn't think it would be valuable etc.)



Question 5.2

Approximately how long did you spend on this entire lesson? How many sessions did you do it in?



Question 5.3

Write a couple of sentences describing your experience with this lesson. What did you like and what do you think could be improved?