Understanding the Setup Procedure

clear-all resets the world to an initial, empty state. All the patches turn black and any turtles you might have created disappear. Basically, it wipes the slate clean for a new model run.

create-turtles 100 creates 100 turtles. They start out standing at the origin, that is, the center of patch 0,0.

After create-turtles we can put commands for the new turtles to run, enclosed by square brackets.

setxy random-xcor random-ycor is a command using “reporters”. A reporter, as opposed to a command, reports a result. First each turtle runs the reporter random-xcor which will report a random number from the allowable range of turtle coordinates along the X axis. Then each turtle runs the reporter random-ycor, same for the Y axis. Finally each turtle runs the setxy command with those two numbers as inputs. That makes the turtle move to the point with those coordinates.

reset-ticks starts the tick counter, now that setup is otherwise complete.

end completes the definition of the “setup” procedure.

Switch to the Interface tab and press the setup button you made before. Repeat to see the effects of the random initialization.

Control the View

Now that we’re using the tick counter (automatically created with reset-ticks), we should tell NetLogo that it only needs to update the view once per tick, instead of continuously updating it.

Make the Go Button

Now make a button called “go”. Follow the same steps you used to make the setup button, except:

For Commands enter go instead of setup.

Check the “Forever” checkbox in the edit dialog.

Check the “Disable until ticks start” checkbox too.

The “Forever” checkbox makes the button stay down once pressed, so its commands run over and over again, not just once.

The “Disable until ticks start” prevents you from pressing go before setup.

Add the Go Procedure

to go
move-turtles
tick
end

tick is a primitive (i.e., built-in to NetLogo) that advances the tick counter by one tick.

move-turtles is not, and we need to add another procedure. Note that it is common that one procedure might have (many) others nested within (i.e., dependencies).

Add the Move Procedure

Add the move-turtles procedure after the go procedure:

to move-turtles
ask turtles [
right random 360
forward 1
]
end

Note there are no spaces around the hyphen in move-turtles. Earlier, we used red - 2, with spaces, in order to subtract two numbers, but here we want move-turtles, without spaces. The “-“ combines “move” and “turtles” into a single name.

Once finished adding the code, go back to the Interface, click setup to initialize and go to make your agents move.

Green sticky notes when you’ve gotten this to work.

Coding Best Practices

Why couldn’t we have just written all of these commands in go instead of in a separate procedure?

We could have, but during the course of building your model, it’s likely that you’ll add many other parts. We’d like to keep go as simple as possible, so that it is easy to understand.

Eventually, it will include many other things you want to have happen as the model runs, such as calculating something or plotting the results.

Each of these things to do will have its own procedure and each procedure will have its own unique name.

Adding Turtle Attributes and Behavior

Thus far, our turtles are just running around. Let’s add some interaction between the turtles and the patches.

We’ll make the turtles eat “grass” (the green patches), reproduce, and die. The grass will change color and gradually grow back after it is eaten.

We’ll need a way of controlling when a turtle reproduces and dies. We’ll determine that by keeping track of how much “energy” each turtle has. To do that we need to add a new turtle variable.

You’ve already seen built-in turtle variables like color. To make a new turtle variable, we add a turtles-own declaration at the top of the Code tab, before all the procedures.

Call it energy:

turtles-own [energy]

Rewrite the Go procedure

Let’s use this newly defined variable (energy) to allow the turtles to eat. Go down to the go procedure and rewrite as:

to go
move-turtles
eat-grass
tick
end

The if statement

A turtle has direct access to the variables of the patch it is standing on. We want each turtle to determine whether the value of the patch color it is on (pcolor) is “green”, and if so, to gain energy by eating grass.

Add a new eat-grass procedure below the move-turtle procedure:

to eat-grass
ask turtles [
...
]
end

An “if statement” allows the program to exectue one procedure if and only if the patch color is green. Only then will the turtle run commands given inside brackets and following the test:

Go back to the Interface, toggle the energy switch to “on”, and click the “setup” and “go” buttons.

Green sticky notes when you get this to work.

How Does ifelse Work?

The eat-grass procedure introduces the ifelse command.

Each turtle, when it runs these new commands, checks the value of show-energy? (determined by the switch).

If the switch is on, comparison is true, and the turtle will run the commands inside the first set of brackets. In this case, it assigns the value for the energy to the label of the turtle.

If the comparison is false (the switch is off) then the turtle runs the commands inside the second set of brackets. In this case, it removes the text labels (by setting the label of the turtle to be nothing with an empty text string).

Interpreting the reproduce procedure

When each turtle runs reproduce, it checks the value of the turtle’s energy variable.

If it is greater than 50, then the turtle runs the commands inside the first set of brackets.

In this case, it decreases the turtle’s energy by 50, then “hatches” a new turtle with an energy of 50.

The hatch command is a NetLogo primitive which looks like this: hatch number [ commands ]. This turtle creates number new turtles, each identical to its parent, and asks the new turtle(s) that have been hatched to run commands. You can use the commands to give the new turtles different colors, headings, or whatever. In our case we run one command. We set the energy for the newly hatched turtle to be 50.

Procedure for death

Add the following code for check-death:

to check-death
ask turtles [
if energy <= 0 [ die ]
]
end

Interpreting the check-death procedure

Each turtle, when it runs check-death it will check to see if its energy is less than or equal to 0.

If this is true, then the turtle is told to die (die is a NetLogo primitive).

Plotting notes

There are not labels for the lines in the plot. Mouse over the Interface and you’ll notice that your cursor turns to cross-hairs. Click and hold while dragging over some part of the plot until it is highlighted. Now the plot is selected. Notice that the Edit and Delete buttons are no longer greyed-out. Click Edit to open the plot dialog, and then click the “Show Legend?” box.

When you create the plot you can also set the minimum and maximum values on the X and Y axes. You’ll want to leave the “Auto Scale” checkbox checked, so that if anything you plot exceeds the minimum and maximum values for the axes, the axes will automatically grow so you can see all the data.

The term “pen” is a little weird, right? But here’s the raionale … we used the plot command to add the next point to a plot. This command moves the current plot pen to the point that has an X coordinate equal to 1 greater than the previously plotted X coordinate and a Y coordinate equal to the value given in the plot command (in the first case, the number of turtles, and in the second case, the number of green patches). As the pens move they each draw a line.

Tick counter

To make comparisons between plots from one model run and another, it is often useful to do the comparison for the same length of model run. This can be done by stopping or starting an action at a specific time by stopping the model at the same point each model run.

Keeping track of how many times the go procedure is run is a useful way to cue these actions. That’s what the tick counter does. You’re already using the tick counter in your model, with the reset-ticks and tick commands, which also trigger plotting.

You can also use the tick counter for other things, such as to set a limit on the total length of a run.

Challenge

Final Note

Don’t forget to comment your code! It has been said that best practice is at least 50% of your key strokes should be comments. Comments let you mix an explanation the code right in with the code itself. You might use comments to help others understand your model, or you might use them as notes to yourself.

Comments begin with “;;” and continue to the end of the line. In the Code tab, comments are gray, so your eyes can pick them out easily.

Acknowledgements & Support

Portions of the instructional materials are adopted from Tutorial #3 in the NetLogo User Manual.

If you need to catch-up before a section of code will work, just squish it's
🍅 to copy code above it into your clipboard. Then paste into your interpreter's
console, run, and you'll be ready to start in on that section. Code copied by
both 🍅 and 📋 will also appear below, where you can edit first, and then copy,
paste, and run again.