Now that we've seen a bit about how Netlogo works, let's build our own simple model. This model will consist of one ant moving around randomly and that's it. So, let's start-up Netlogo. I am going to click on this Netlogo 5.3.1, you can click on whatever version that you have. Now the first thing I am going to do is create the Setup and the Go buttons. So, I go to the Interface Builder Menu, When I click on it, you can see all the different types of objects I can add to the interface. So, we're going to add a button. So, I click on the interface, I can fill-in that this button is for Setup, and I can get another button, Add another button for Go I can select this by right-clicking on it, Click Select, I can move it, I can change its size. If I drag it here, I can adjust the buttons however I like. So, now we have the Setup and Go buttons. They are red because they don't have any code associated with them yet. There is nothing that they actually do yet when they are clicked, so I am going to have to create the code. Now, let's create the code that go with the Setup and Go buttons. We do that by going to the code tab and writing the procedures. So, the first one we will do is Setup. So, we say to Setup here is what we do when the button is clicked, we clear all, here clear all is a command that clears the world, that is a place you see ants committing any actions, it clears that window and anything leftover from the previous run. We are going to reset ticks, that is, we are going to set the ticks, or the time set to zero, Now we are going to create an ant. Every agent in Netlogo is called a turtle. This is for historical reasons, coming from the original language, Logo, that Netlogo was based on, all the agents were turtles. In Logo you just had one turtle. Here you are eventually going to have many turtles. For now, we are going to say 'create turtles'. We are going to say how many turtles we want. For now, we are just going to say that we want one. So, we are going to create one turtle and then we are going to ask it to do some things. We say ask turtle and we are going to give it a list. We are going to ask it to set its shape to bug; that is the shape of an ant in Netlogo. We are going to set its size to 3. That is a good size in Netlogo for viewing it. We are going to set its color to red. Ok, I think that's all that we want to ask it to do, so we put End. There is this nice check mark here which checks the code for bugs. So, click on that, and it tells me there is a bug, and it tells me where the bug was, and I notice that I should have actually said ask "turtles" I missed an "s" there. The reason is that Netlogo assumes that there is a population of turtles or agents and whenever you ask to do something, you ask the entire population. We only have one turtle, but later we are going to ask all of them to do this. Check this again. It brings me back to the interface saying that everything seems to work, so let's click Setup, and we see our ant, right in the middle. The Go button is still red which means that we haven't written the code for that. Let's write the Go procedure. Let's go back to the code tab. We write "to go". We write "ask turtles" to do a few things. First, we ask them to turn their bodies to the right 30 degrees, then we are going to ask them to move forward 4 steps, write "forward 4", then we will increase the time steps by one, that's what the tick command does, we will reset the ticks to 0, and then type "tick" which increases the time step by one each time we go through the procedure and then we end with "end". Let's go to the interface, and hit "Go". You can see that the turtle turned it's body by 30 degrees and then moved forward by 4 steps. We keep hitting the "Go" button over and over again, and it goes around in a circle. I would like to have it go on forever, without me having to continually hit the "Go"button and I can do that by right-clicking on "Go"and going to Edit, and we see this option, forever, which I can click which means that it will keep repeating "Go" forever, so let's do that. If I slow it down a little bit, you can see the ant going around and around by itself, and I click on the "Go"button to stop it. I can click on "Go" to start it again. If I want to start from scratch, I click on Setup. It starts in the same place, in the center, but it points in a different direction each time. That's because it puts it in the center, but it points it in a random direction each time. We can save the model so far, by going to the File tab, and click on SaveAs. I am going to save it as, Ant1. You see it here on the desktop, Ant1.logo. Now we can go to Netlogo menu, quit Netlogo. If I want to start this up, I just double-click on this. Netlogo will start-up and load this model. There is only one thing wrong with our model, and that is that it is boring. The only thing that the ant does is go around in a circle, so let's make it a little more interesting. I can do that by going to the code and adding one simple command, Instead of going right 30 degrees, we will add the word, "random", so now the ant will choose a random number between 0 and 30 minus 1, and go to the right that number of degrees. We can do the same thing here, we can have the ant move forward a random number of steps, Let's go back to the interface, we click on Setup and Go, and we can see it makes the behavior a little more interesting. You might notice the ant appears to go through the wall over here and come out the other side. The wall is wrapped around. This makes it look a little strange. I can right-click on the black area, click on Edit. There is a lot of information here about the setup of this world. The number of patches, is the grid, the world is divided-up into. Uncheck them, apply, ok. Let's see what happens then. Now, when the ant hits the wall, it gets stuck there, until it gets turned around. Our ant still just turns to the right. Let's write code that allows it to choose at random whether it turns to the right or to the left. It essentially flips a coin to decide if it is going to turn to the right or to the left. I am going to implement the coin flip procedure. Something called "to-report" "coin-flip", "to-report" is different from a "to"procedure, "to-report" returns a value, here it will return true or false. We say "report random 2 = 0" "end" what this does is "random 2" "random" returns a random number between 0 and this number, 2, minus 1, so this returns 0 half of the time and 1 half of the time, in a random way. If this is zero, this statement returns true if the statement = 0, else it returns false. We can use the value of that statement, by saying, "if else" coin-flip, what that does is "if coin-flip is true" do this first thing that I am going to put in brackets, otherwise if it is false, do the second thing that I am going to put in brackets, "left random 30". Everytime we run the Go procedure, it will decide at random whether it will turn right or left. So, let's see how that works. Setup and Go. You can see it is turning a different direction, it is turning forward, right and left. The only problem is that it is getting stuck too often. We can make that better by changing these 30's to 60's Have a bigger turning radius, that is, it turns more each time. It seems to be getting stuck less often. Whenever you write a program, it is always good to put in comments. They are ignored by the computer, but they are good for people to read. In Netlogo, a comment is just text following a semicolon. I can write anything I want and the computer will ignore it. Let's write, returns true or false at random, which is what it does, Here we can write another comment, if coin-flip is true, turn right, else turn left, There you have little reminders, either for ourselves, or for others reading the program. Really helpful if you have a more complicated program. Especially, if you come back to it after a few days. Comments are absolutely essential, even if no one else is going to read your program. Now we are going to save our model. First, I am going to label it. That is an aesthetic thing, I am going to the Interface menu and choose Note, I am going to put up here a little bit of text that says the name of the model, Ant1. Let's make the text a little bigger, 18, and then I can select this and move them down a little bit, center them, do whatever I want to make them prettier, click Save, and the model is saved. I just wanted to mention that the purpose of teaching Netlogo is to give you a sense of how it all works. We are going to be using a lot of Netlogo models in this course. We're not going to be writing many. I wanted to give you a sense of how it all works. You could be doing some modifications of what we wrote. If you are more advanced, you can try and write some of your own. As an assignment, I would like you to implement that exact same model, as I did, Ant1, using the same commands, and get it to work. That is going to be ungraded, but it is to your benefit. Don't hesitate to ask questions on the student forum when they come up. That's why they're there.