Netlogo: Hello World

Netlogo: Hello World

NetLogo: Hello World

[Title Slide] Hi, this is Joel Castellanos of the University of New Mexico and this awesomely amazing CS4All video is on writing a Hello World program using Turtle Graphics and NetLogo. [Slide 1] We will be using NetLogo's integrated development environment to create a blank project and from scratch make a program and [write it 00:00:37]. We'll be using Turtle Graphics. The basic metaphor behind Turtle Graphics is kind of shown with this picture.

There is a turtle or some creature that's moving around on a flat surface and wherever it moves it can leave a trail. This picture shows a screen capture from NetLogo that's running from 5 turtles. NetLogo by default draws a turtle as an arrow head, so 5 arrow heads started out in the center with different headings and in different colors. The red turtle moved forward along this line and then it turned to the right by 45 degrees and moved forward again and it turned to the left and moved forward again and here's its final position.

This olive green turtle also started in the middle. It moved forward along its heading which is pointed downward and then it turned 45 degrees to its right. It was heading in this direction so its right was turning along this line and it moved forward along that new heading. Then it turned 45 degrees to its left and moved again straight ahead and this was its final position.

[Slide 2] Turtle Graphics are often used when people are first learning how to write a computer program because Turtle Graphics do a great job of illustrating 2 very key concepts in computer programming. First is that a computer program is a sequence of instructions that are executed or run by the computer. An analogy commonly used is a recipe. It's a list of instructions and the instructions have to be followed in a particular order. As you progress through the program, one instruction and then another instruction and then another instruction, et cetera.

The other key concept that is illustrated very nicely by Turtle Graphics is that as a computer executes or runs it has a state which is a function of time, that state changes as the program executes. One part of the state of a computer program as it executes is which instruction in the program will be executed next. The program is a sequence of instructions. As it's executing it is passing through time and at the current time which instruction will it be executing.

The other part of a computer program state as it runs, as it executes, is the values stored in various memory locations which are sometimes called variables or attributes. As the program runs, it executes statements and those statements can change the values stored in memory locations. It can change the value of variables. Then if the computer program loops back on itself 1 of its instructions is to go back to a previous step and execute the same code again. The same computer instruction could have different effects because the instructions use the values stored in these memory locations which other instructions have changed. All of this is illustrated and made very clear using Turtle Graphics, so we're going to play with some Turtle Graphics.

[Slide 3] This slide shows examples of some of the commands that NetLogo uses to manipulate the turtle. This first command pen-down is metaphorical. There is no physical pen that gets physically moved downward. What it does is tell the computer that any future move commands like the forward command should result in a line being drawn wherever the turtle moves and that will continue until there's pen up command. The set heading command takes a number between 0 and 360, 360 degrees. Both 0 and 360 degrees assess the heading of the turtle pointing upward and that's the picture that we're seeing right here.

Set heading 90 would have the turtle pointing to the left. Set heading 180 would have the turtle pointing downward. The forward 2 command tells the turtle to move forward at its current heading 2 steps and the line you can see drawn from here and it continues on to the middle of the turtle. The command right 90 tells the turtle to turn to its right by 90 degrees so now we see its heading has changed. The next command that gets executed is forward 4 so the turtle moves forward 4 spaces from its previous position onto its new position.

After the turtle has moved forward 4 now the next command is right 90 so it turns 90 degrees to its right. Notice that the right 90 over here the turtle had been going up and it turned to my right. The 90 degrees in this statement the turtle had been going to my right and now its turning downward on the screen. From the turtle's perspective both of those are right turns by 90 degrees. After it's turned 90 degrees it now moves forward 8 and that's its final position here. Now here the forward command caused the turtle to move up and down here the forward command caused the turtle to move downward. That's because the forward command is a command that is dependent on the state of the turtle.

The state of the turtle back here was its heading was upward direction so moving forward when it was headed upward caused it to move up and here the same command of moving forward causes it to go down because its heading is in the downward direction. [Slide 4] This slide lists some of the states that a turtle can have. Its shape which by default is this arrow shape. It can be changed to many other different shapes. The default value is the arrow shape. In computer science, we make frequent use of the word default.

In the banking industry when someone takes out a loan, they are making a promise to be paying that back and if they don't make any payments and they don't contact the bank and make some other arrangements that is if the person does nothing then the loan goes into default. Default is what happens when the person does nothing. Similarly in computer science, the default is what happens when the programmer or the user doesn't tell the computer specifically what to do. The default is what happens automatically when no special action is taken on the part of the programmer.

When you create a turtle, by default it has this arrow shape. You can change that if you want to. If you are happy with the arrow shape, you can just leave it. By default when you create a turtle, its given a random color, its given a random heading. It's set with its pen up. It has a pen size of 1 and there's a number of other properties that turtles have which are set by default and the defaults values are values that the person who wrote NetLogo in this case thought people would commonly want to use.

[Slide 5] This is our last slide before going into NetLogo and actually writing a program. This is a list of Turtle Graphics commands in order from top to bottom. Before I show you what it actually draws try and draw it in your mind's eye and here it is. It makes a spiral moving outward. Each time it turns 90 degrees and each step it makes larger, a step twice as large as its previous step always turning 90 degrees to the right, so it spirals.

Okay let's get into NetLogo. All right we start with a blank NetLogo project. Our interface tab is blank and our code tab is blank. All NetLogo programs must have at least 1 graphical user interface or GUI component because code that we enter in this code tab won't get called unless something in the interface tab is clicked or activated by the user.

We'll start with the interface tab and I want to add 2 buttons. There are a number of graphical user interface items that can be selected from [on these buttons 00:08:30] and click add and add a button. This first button I'm going to name setup. I could name it anything, but I'm going to use this button to set up my program. I'm going to use it to create the turtles that we're going to work with in our program and to give them any initial values so setup is a good name for it. Click okay.

Now this is colored red and the tab is red. This is because my program now contains an error. If I click on setup, I can get some information about that error. I have a message saying nothing named setup has been defined. I have a button named setup. What that means in that logo is that when I run the program and click the setup button NetLogo is going to look in the code tab for a procedure named setup and I haven't created that procedure. I'm going to leave that error for now though and make another button so we just click okay.

I'll click the add again and add another button. This one I'm going to call go because I'm going to use the setup button to create my turtle and I'm going to use the go button to move it around and I can call the go button perhaps multiple times to keep moving it. In NetLogo when a programmer creates a button the programmer can name it pretty much what he or she wants. I chose to call this button go. I could have called it Bob or Mary or Sue, but there are some things that I couldn't have called it.

If I called it setup for example, there would have been a name clash because I already have a button named setup. Each button has to be named something different. If I called it run, I would have had another problem because run is a reserved word in NetLogo. In the programming language reserved words are words that in that language have special meaning. Run for example is a reserved word in NetLogo, but go is not and so I can name this button go.

After creating these buttons, I can resize them and move them around by dragging across them to select them. If I click in the corners, the black box areas I can make them bigger. If I can click in the gray area, I can move them, so I'll make both of these buttons the same size. Drag across this setup and have them nicely aligned. Now I want to add some code that will get executed when these buttons are clicked, so I'm going to go over to the code tab.

I have a button named setup so I have to have a procedure named setup. To which is a reserved word in NetLogo to say I'm defining a procedure setup. Every procedure has to end with the word end. This is another reserved word in NetLogo. Notice that when I first typed end it was indented and when I finished typing it NetLogo put it to the edge. Indentation is used to show structure in the language. All the code that goes between the to and the end will be code that's inside the setup procedure and so all of that code will be indented. Since end is ending the setup procedure it's at the same level of indenting as the to.

I'm going to come down, skip a few lines and create a to go for my go button and also have an end. Now notice that I have this red on my interface tab. This check means to check the syntax so I click this and the red went away and if I go to my interface tab I'll see that my setup button is no longer red and my go button is no longer red because there is a procedure named setup and go. Right now those procedures are empty. They don't do anything, but they do exist so that program no longer has an error.

I'll go back to the code tab and add some content to my setup. I'm going to first clear everything out from previous runs and the command in NetLogo for that is clear-all. Clear all with a dash. Notice I have 3 different colors in the editor here. To and end are reserved words in the language and they are colored green. Clear all is a procedure that's built into the language, so it's colored blue. Setup is a procedure that I made up. I made up the word setup to ... I decided to name my button setup. I could have named it something else, because I defined it it's colored black, so reserved words in the language are green, procedures that are predefined are blue, and names, identifiers, variables, and procedure names that I create are colored black.

Now, the next thing I want to do in setup I clear all and I'm also going to create a turtle. Create TURTLES and I'm only going to create 1 of them. It still has to say create-turtles because the procedure's name is create-turtles and I'm going to pass [it an argument 00:13:32] of 1, so it will create 1 turtle, still need the plural though because that's the name of the function and I can click check and every thing's not showing ... nothing is showing in error.

Now I can go to the interface tab and run my program. This button setup is selected so I click somewhere else to get it unselected and now I can click on setup and it executes the code. Notice that it drew a turtle. A random colored turtle in a random heading. If I click set up again each time I click setup, it calls the code in setup which clears all, so it erases the previous turtles. It creates a new turtle and it's going to by default create that turtle with a random heading and in a random color and always starting in the center at 00.

[inaudible 00:14:23] setup and I get these different turtles. [Wow 00:14:26]. Go still it does nothing, so now I'm going to go to my code and add some content to the go procedure. Within the go procedure before I can start using turtle commands such as forward and right I have to tell NetLogo what it is that I want to be moving forward and right. In this little program all I have are turtles, but in other programs I might have many agents. Many different types of things that could be moved forward or right, so I use the NetLogo command ask turtles and now I have an open bracket and a closed bracket.

This is going to look through each of the turtles. I only have 1 right now, but it will look through the list of size 1 and this open and closed bracket defines the structure, everything that goes inside these brackets are the things that are going to happen to each turtle, so I'm going to move the turtle forward 1, have it turn right by 45 degrees, then have it turn left by 45 degrees and then have it move forward again by 1. Okay and now I can check the syntax to make sure I don't have any errors, no error problems, so I can go to my interface and click setup and I get my turtle initialized and click go. I saw no line. That's because I never put the pen down.

I'm going to go back to my code tab. The first thing before moving forward is I'm going to say pen-down. I have to do this inside the turtle block because pen-down is one of the attributes of a turtle. If I created many turtles, some of them could have their pens down and some could not have their pens down. If I put this command outside of the ask turtles NetLogo wouldn't know which turtle that I was telling to put its pen down. Inside this structure, this block of code gets executed for each turtle in my list of 1, so check syntax again.

I'll go back to interface and click setup, so it puts it back in the center and I'll click go. Now I can see a line that it made. If I click go again without hitting setup it's not going to choose a new turtle. It's not going to put it back to the center. It will continue moving the turtle from where it is right now, so I click go and see it just continued and each time I click go it's going to continue. If I click setup, it erases everything, gives me a new turtle in a new random heading with a new random color and now if I click go it moves that turtle from that start position and each time I click go it goes forward.

Now we have a working program, so it's time to change it up a little bit. See what we can do in modifications. I'm going to come over to code tab, the most obvious thing I think to change is we have create-turtles 1, but what if I change that to a 5. Obviously it's going to make 5 turtles. When I click run, try and visualize in your mind's eye what you'll see on the screen. First I'll click check and this checks for syntax errors. I don't have any. This only check for syntax errors not semantic errors. Syntax errors are errors like grammatical errors. For example if I had 2 Ts in create and I go to check syntax, I'm going to get an error because NetLogo doesn't know what this command is with 2 Ts, so I'll have to take that extra T out and now it checks.

[With checks 00:18:29] I find out that I have no syntax errors, but I might still have semantic errors or logic errors. For example this draws a zig zag line and maybe I wanted to draw a house shape so this would contain an error because it doesn't draw what I want it do draw, but it doesn't contain in syntax errors. Anyway, let's go to interface and I'll click setup again. Now we see a big jumble here. There were 5 turtles created all on top of each other with random colors with random headings and when I click go they each move, click go again and again.