Ok, now we're going to write the compete for space procedure. First of all, we're going to say if there is any ... we're going to have to ask the turtles to do this. If there are any other turtles here die. If you're sitting on any space with another turtle, then just die. That's the extreme form of over-crowding. Now we need to write a procedure for a condition, turtles will die, due to over-crowding. We need a parameter that we set that is related to the over-crowding. Let's call that parameter, space. I have added a slider here called space. We know that around every turtle there is a neighborhood of eight spaces, North, South, East, West, Northeast, Northwest, etc. We made a slider that goes from 1 to 8. I am going to ask, We can easily count the turtles and the neighbors. If the count of turtles on neighbors is eight minus the count of turtles. That should gives us number of empty spaces. If the empty spaces is less than the amount of spaces, the turtle needs to live, the turtle needs to die. We can set the space any number from 1 to 8 spaces. If the turtle needs eight spaces to live, it is going to have to be very thinly populated. You can think of that as a resource that constrains the growth of this population. I am going to make a note here, Constraint to pop growth. We should add comments so other people can understand our code. If we come back to this later, we can understand what parts of the code do. Now, we have to write our disperse procedure. That will be our last procedure. To disperse, just ask the turtles to move to one of the patches with not any turtles here. This language is not at all obvious. It reads pretty well as English, but it certainly is not easy to come up with when you are beginning to program. This is one of the sample code you can look at to see how to code. We're talking about patches, a subset of patches those with this conditional met, not any turtles here. If a patch has no turtles on it, we're going to move to one of those patches. We have to only do this, a little tricky thing here. If we were going to ask turtles to try to do this. and there weren't any of these patches available, turtles could continue to look, and this code would never terminate, so we need to add conditions, and we only do this is there are patches. We only do this if the number of turtles the screen is less than or equal to number of patches. We could have more turtles than patches, then this would be hopeless. The code would just keep running. We would not know what is going on. I speak from experience there. I think that should work. Let's see if this whole thing can run now. There are still some questions. We set a large initial population. We still have these things changing color. Let's delete what I added to do an artificial disperse. It looks like something is happening. I am not sure what. Let's get higher density. When we step, we should have, if we require them to need a lot of space, let's see if that results in a thinner population, indeed it does. With less space, we get a higher density. Look here, looks to me, like we have a model that is roughly working. Let's see what occurs here, when we look at the population. I am going to make a simple plot. By default, they set a plot that counts the turtles. In order to get the plot to actually plot, we have to do something to the model. This will happen by default everytime we have a tick. Every time we step, we're going to see a tick. This is going to give us an error the first time. Let's see what kind of error it gives us. If we run setup, and then step. We have to use this command reset ticks in the setup. Reset ticks. So, setup, ok, Not sure that was exactly what we wanted. Let's try starting with a lower population and having a kind of a generous Let's see how that works. Initial population 12. Am I doing this right? It looks like our reproduction function isn't working. Let's figure out what's going on here. Inspect this turtle. 320. Generation 13. Show you how I debug this. Let's comment this out, turn off death, and see what happens for a second. The death isn't happening since we turned it off, we should have exponential growth. There is something about my compete for space. Comment this out. I am guessing we will still have the same problem. Hmmm. I guessed wrong. Let's go back to look here. This is the rule causing the problem. If any other turtles here die. Ok, oh, I see the problem. We need to have ... this rule here is killing off the offspring. I need to have the offspring disperse. So, we reproduce and then we disperse. Then we compete for space. These programs almost never work right out of the box. I am guessing this works. What I have done is say, after you reproduce, disperse, then parents from previous generation die, then there is a compete for space. Setup, step, now we get exponential growth and we get a leveling off. I am not seeing, ..., step, step, step, Huh, not quite what I expected, That's more like I was expecting. It's like a sigmoidal curve. I am going to compete for a lot of space. So, slower growth, and more variation. Ok, I think that the model, I would say this model is working, we have exponential growth, and then a kind of leveling off. We have a kind of frozen configuration. These are the kinds of questions that can come up. Why that works that way? With a different disperse procedure we would probably see something else. One of the things I would want to change is disperse, to make it physically more realistic. Have them migrate from current location until they find a new location. It would be a little more code, but ... Create different colors to see different families and watch them over time. A neat project. Ok, finally, to close the gap here, We have a step button, that goes through one full route here. The Go button can be a Step button with the forever attribute turned on. We should call this, this is an important point to notice, the display doesn't have to be the same as the command, but if you don't put the display name in, the command name is used as the display name. We have a nice little model that has constraints. At certain space settings, it does an ok job of modeling exponential growth and kind of a rounding off to a carrying capacity.