Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

1025 lines (1010 sloc) 38.646 kB
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<title>
NetLogo User Manual: Tutorial #3: Procedures
</title>
<link rel="stylesheet" href="netlogo.css" type="text/css">
<meta http-equiv="Content-Type" content="text/html; charset=us-ascii">
<h1>
Tutorial #3: Procedures
</h1>
<div class="version">
NetLogo User Manual&nbsp;&nbsp;&nbsp;
</div>
<p>
This tutorial leads you through the process of building a complete
model, built up in stages, with every step explained along the way.
<h2>
Agents and procedures
</h2>
<p>
In Tutorial #2, you learned how to use the command center and agent
monitors to inspect and modify agents and make them do things. Now
you're ready to learn about the real heart of a NetLogo model:
the Code tab.
<p>
You've seen that agents in NetLogo are divided into patches,
turtles, links, and the observer. Patches are stationary and arranged
in a grid. Turtles move over that grid. Links connect two turtles.
The observer oversees everything that's going on and does
whatever the turtles, patches and links can't do for themselves.
<p>
All four types of agents can run NetLogo commands. All four can also
run &quot;procedures&quot;. A procedure combines a series of NetLogo
commands into a single new command that you define.
<p>
You will now learn to write procedures that make turtles move, eat,
reproduce, and die. You will also learn how to make monitors,
sliders, and plots. The model we'll build is a simple ecosystem
model not unlike Wolf Sheep Predation from Tutorial #1.
<h2>
Making the setup button
</h2>
<p>
To start a new model, select &quot;New&quot; from the File menu. Then
begin by creating a setup button:
<blockquote>
<ul>
<li>Click the &quot;Add&quot; icon in the toolbar at the top of the
Interface tab.
<li>On the menu next to Add, select Button (if it isn't already
selected).
<li>Click wherever you want the button to appear in the empty white
area of the Interface tab.
<li>A dialog box for editing the button opens. Type <tt>setup</tt>
in the box labeled &quot;Commands&quot;.
<li>Press the OK button when you're done; the dialog box
closes.
</ul>
</blockquote>
<p>
Now you have a setup button. Pressing the button runs a procedure
called &quot;setup&quot;. A procedure is a sequence of NetLogo
commands that we assign a new name. We'll define that procedure
soon, but we haven't yet. The button refers to a procedure that
doesn't exist, so the button turns red:
<p class="screenshot">
<img alt="screen shot" src="images/tutorial3/errors.png">
<p>
If you want to see the actual error message, click the button.
<p>
Now we'll create the &quot;setup&quot; procedure, so the error
message will go away:
<blockquote>
<ul>
<li>Switch to the Code tab.
<li>Type the following:
<pre>
to setup
clear-all
create-turtles 100 [ setxy random-xcor random-ycor ]
reset-ticks
end
</pre>
</ul>
</blockquote>
<p>
When you're done, the Code tab looks like this:
<p class="screenshot">
<img alt="screen shot" src="images/tutorial3/setupcode.png">
<p>
Note that some lines are indented. Most people find it helpful to
indent their code. It isn't mandatory, but it makes the code
easier to read and change.
<p>
Your procedure begins with <a href=
"dictionary.html#to"><tt>to</tt></a> and ends with <a href=
"dictionary.html#end"><tt>end</tt></a>. Every procedure begins and
ends with these words.
<p>
Let's look at what you typed in and see what each line of your
procedure does:
<ul>
<li>
<tt>to setup</tt> begins defining a procedure named
&quot;setup&quot;.
<li>
<a href="dictionary.html#clear-all"><tt>clear-all</tt></a> 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.
<li>
<tt>create-turtles 100</tt> creates 100 turtles. They start out
standing at the origin, that is, the center of patch 0,0.
<li>After <tt>create-turtles</tt> we can put commands for the new
turtles to run, enclosed by square brackets.
<li>
<tt>setxy random-xcor random-ycor</tt> is a command using
&quot;reporters&quot;. A reporter, as opposed to a command, reports
a result. First each turtle runs the reporter <a href=
"dictionary.html#random-xcor"><tt>random-xcor</tt></a> which will
report a random number from the allowable range of turtle
coordinates along the X axis. Then each turtle runs the reporter
<a href="dictionary.html#random-ycor"><tt>random-ycor</tt></a>,
same for the Y axis. Finally each turtle runs the <a href=
"dictionary.html#setxy"><tt>setxy</tt></a> command with those two
numbers as inputs. That makes the turtle move to the point with
those coordinates.
<li>
<a href="dictionary.html#reset-ticks"><tt>reset-ticks</tt></a>
starts the tick counter, now that setup is otherwise complete.
<li>
<a href="dictionary.html#end"><tt>end</tt></a> completes the
definition of the &quot;setup&quot; procedure.
</ul>
<p>
When you're done typing, switch to the Interface tab and press
the setup button you made before. You will see the turtles scattered
around the world:
<p class="screenshot">
<img alt="screen shot" src="images/tutorial3/scatter.gif">
<p>
Press setup a couple more times, and see how the arrangement of
turtles is different each time. Note that some turtles may be right
on top of each other.
<p>
Think a bit about what you needed to do to make this happen. You
needed to make a button in the interface and make a procedure that
the button uses. The button only worked once you completed both of
these separate steps. In the remainder of this tutorial, you will
often have to complete two or more similar steps to add another
feature to the model. If something doesn't appear to work after
you completed what you thought is the final step for that new
feature, continue to read ahead to see if there is still more to do.
After reading ahead for a couple of paragraphs, you should then go
back over the directions to see if there is any step you might have
missed.
<h2>
Switching to tick-based view updates
</h2>
<p>
Now that we're using the tick counter (with
<tt>reset-ticks</tt>), we should tell NetLogo that it only needs to
update the view once per tick, instead of continuously updating it.
<blockquote>
<ul>
<li>Find the view updates menu. It's above the view and by
default says &quot;continuous&quot;.
<li>Choose &quot;on ticks&quot; instead.
</ul>
</blockquote>
<p>
This makes your model run faster and ensures a consistent appearance
(since the updates will happen at consistent times). See the
Programming Guide for a fuller discussion of view updates.
<h2>
Making the go button
</h2>
<p>
Now make a button called &quot;go&quot;. Follow the same steps you
used to make the setup button, except:
<blockquote>
<ul>
<li>For Commands enter <tt>go</tt> instead of <tt>setup</tt>.
<li>Check the &quot;Forever&quot; checkbox in the edit dialog.
<li>Check the &quot;Disable until ticks start&quot; checkbox too.
</ul>
</blockquote>
<p class="screenshot">
<img alt="screen shot" src="images/tutorial3/gobutton.png">
<p>
The &quot;Forever&quot; checkbox makes the button stay down once
pressed, so its commands run over and over again, not just once.
<p>
The &quot;Disable until ticks start&quot; prevents you from pressing
go before setup.
<blockquote>
<ul>
<li>Then add a go procedure to the Code tab:
<pre>
to go
move-turtles
tick
end
</pre>
</ul>
</blockquote>
<p>
<a href="dictionary.html#tick"><tt>tick</tt></a> is a primitive that
advances the tick counter by one tick.
<p>
But what is <tt>move-turtles</tt>? Is it a primitive (in other words,
built-in to NetLogo)? No, it's another procedure that you're
about to add. So far, you have introduced two procedures that you
added yourself: <tt>setup</tt> and <tt>go</tt>.
<blockquote>
<ul>
<li>Add the <tt>move-turtles</tt> procedure after the
<tt>go</tt>procedure:
<pre>
to go
move-turtles
tick
end
to move-turtles
ask turtles [
right random 360
forward 1
]
end
</pre>
</ul>
</blockquote>
<p>
Note there are no spaces around the hyphen in <tt>move-turtles</tt>.
In Tutorial #2 we used <tt>red - 2</tt>, with spaces, in order to
subtract two numbers, but here we want <tt>move-turtles</tt>, without
spaces. The &quot;-&quot; combines &quot;move&quot; and
&quot;turtles&quot; into a single name.
<p>
Here is what each command in the <tt>move-turtles</tt> procedure
does:
<ul>
<li>
<tt>ask turtles [ ... ]</tt> says that each turtle should run the
commands in the brackets.
<li>
<tt>right random 360</tt> is another command that uses a reporter.
First, each turtle picks a random whole number between 0 and 359.
(<a href="dictionary.html#random"><tt>random</tt></a> doesn't
include the number you give it as a possible result.) Then the
turtle turns right this number of degrees.
<li>
<tt>forward 1</tt> makes the turtle move forward one step.
</ul>
<p>
Why couldn't we have just written all of these commands in
<tt>go</tt> instead of in a separate procedure? We could have, but
during the course of building your project, it's likely that
you'll add many other parts. We'd like to keep <tt>go</tt> 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.
<p>
The 'go' button you made in the Interface tab is a forever
button, meaning that it will continually run its commands until you
shut it off (by clicking on it again). After you have pressed
'setup' once, to create the turtles, press the 'go'
button. Watch what happens. Turn it off, and you'll see that all
the turtles stop in their tracks.
<p>
Note that if a turtle moves off the edge of the world, it
&quot;wraps&quot;, that is, it appears on the other side. (This is
the default behavior. It can be changed; see the <a href=
"programming.html#Topology">Topology</a> section of the Programming
Guide for more information.)
<h2>
Experimenting with commands
</h2>
<p>
We suggest you start experimenting with other turtle commands.
<p>
Type commands into the Command Center (like <tt>turtles&gt; set color
red</tt>), or add commands to <tt>setup</tt>, <tt>go</tt>, or
<tt>move-turtles</tt>.
<p>
Note that when you enter commands in the Command Center, you must
choose <tt>turtles&gt;</tt>, <tt>patches&gt;</tt>,
<tt>links&gt;</tt>, or <tt>observer&gt;</tt> in the popup menu on the
left, depending on which agents are going to run the commands.
It's just like using <tt>ask turtles</tt> or <tt>ask
patches</tt>, but saves typing. You can also use the tab key to
switch agent types, which you might find more convenient than using
the menu.
<p>
You might try typing <tt>turtles&gt;</tt> <tt>pen-down</tt> into the
Command Center and then pressing the go button.
<p>
Also, inside the <tt>move-turtles</tt> procedure you can try changing
<tt>right random 360</tt> to <tt>right random 45</tt>.
<p>
Play around. It's easy and the results are immediate and visible
-- one of NetLogo's many strengths.
<p>
When you feel you've done enough experimenting for now,
you're ready to continue improving the model you are building.
<h2>
Patches and variables
</h2>
<p>
Now we've got 100 turtles aimlessly moving around, completely
unaware of anything else around them. Let's make things a little
more interesting by giving these turtles a nice background against
which to move.
<blockquote>
<ul>
<li>Go back to the <tt>setup</tt> procedure. We can rewrite it as
follows:
<pre>
to setup
clear-all
setup-patches
setup-turtles
reset-ticks
end
</pre>
<li>The new definition of <tt>setup</tt> refers to two new
procedures. To define <tt>setup-patches</tt>, add this:
<pre>
to setup-patches
ask patches [ set pcolor green ]
end
</pre>
</ul>
<p>
The <tt>setup-patches</tt> procedure sets the color of every patch
to green to start with. (A turtle's color variable is <a href=
"dictionary.html#color"><tt>color</tt></a>; a patch's is
<a href="dictionary.html#pcolor"><tt>pcolor</tt></a>.)
<p>
The only part remaining in our new 'setup' that is still
undefined is <tt>setup-turtles</tt>.
<ul>
<li>Add this procedure too:
<pre>
to setup-turtles
create-turtles 100
ask turtles [ setxy random-xcor random-ycor ]
end
</pre>
</ul>
<p>
Did you notice that the new <tt>setup-turtles</tt> procedure has
most of the same commands as the old <tt>setup</tt> procedure?
<ul>
<li>Switch back to the Interface tab.
<li>Press the setup button.
</ul>
</blockquote>
<p>
Voila! A lush NetLogo landscape complete with turtles and green
patches appears:
<p class="screenshot">
<img alt="screen shot" src="images/tutorial3/scatter-on-green.gif">
<p>
After seeing the new <tt>setup</tt> procedure work a few times, you
may find it helpful to read through the procedure definitions again.
<h2>
Turtle variables
</h2>
<p>
So we have some turtles running around on a landscape, but they
aren't doing anything with it. Let's add some interaction
between the turtles and the patches.
<p>
We'll make the turtles eat &quot;grass&quot; (the green patches),
reproduce, and die. The grass will gradually grow back after it is
eaten.
<p>
We'll need a way of controlling when a turtle reproduces and
dies. We'll determine that by keeping track of how much
&quot;energy&quot; each turtle has. To do that we need to add a new
turtle variable.
<p>
You've already seen built-in turtle variables like <a href=
"dictionary.html#color"><tt>color</tt></a>. To make a new turtle
variable, we add a <a href=
"dictionary.html#turtles-own"><tt>turtles-own</tt></a> declaration at
the top of the Code tab, before all the procedures. Call it
<tt>energy</tt>:
<pre>
turtles-own [energy]
to go
move-turtles
eat-grass
tick
end
</pre>
<p>
Let's use this newly defined variable (<tt>energy</tt>) to allow
the turtles to eat.
<blockquote>
<ul>
<li>Switch to the Code tab.
<li>Rewrite the <tt>go</tt> procedure as follows:
<pre>
to go
move-turtles
eat-grass
tick
end
</pre>
<li>Add a new <tt>eat-grass</tt> procedure:
<pre>
to eat-grass
ask turtles [
if pcolor = green [
set pcolor black
set energy energy + 10
]
]
end
</pre>
</ul>
</blockquote>
<p>
We are using the <a href="dictionary.html#if"><tt>if</tt></a> command
for the first time. Look at the code carefully. Each turtle, when it
runs these commands, compares the value of the patch color it is on
(<a href="dictionary.html#pcolor"><tt>pcolor</tt></a>) to the value
for <tt>green</tt>. (A turtle has direct access to the variables of
the patch it is standing on.) If the patch color is green, the
comparison reports <tt>true</tt>, and only then will the turtle run
the commands inside the brackets (otherwise it skips them). The
commands make the turtle change the patch color to black and increase
its own energy by 10. The patch turns black to signify that the grass
at that spot has been eaten. And the turtle is given more energy,
from having just eaten.
<p>
Next, let's make the movement of turtles use up some of the
turtle's energy.
<blockquote>
<ul>
<li>Rewrite <tt>move-turtles</tt> as follows:
<pre>
to move-turtles
ask turtles [
right random 360
forward 1
set energy energy - 1
]
end
</pre>
</ul>
</blockquote>
<p>
As each turtle wanders, it will lose one unit of energy at each step.
<blockquote>
<ul>
<li>Switch to the Interface tab now and press the setup button and
the go button.
</ul>
<p>
You'll see the patches turn black as turtles travel over them.
<p class="screenshot">
<img alt="screen shot" src="images/tutorial3/eating-grass.gif">
</blockquote>
<h2>
Monitors
</h2>
<p>
Next you will create two monitors in the Interface tab with the
toolbar. (You make them just like buttons and sliders, using the Add
icon on the toolbar.) Let's make the first monitor now.
<blockquote>
<ul>
<li>Create a monitor by clicking the Add icon on the toolbar,
selecting Monitor next to it, and clicking on an open spot in the
Interface.
</ul>
<p class="question">
A dialog box will appear.
<ul>
<li>In the dialog type: <tt>count turtles</tt> (see image below).
<li>Press the OK button to close the dialog.
</ul>
</blockquote>
<p class="screenshot">
<img alt="screen shot" src="images/tutorial3/monitor-1.gif">
<p>
<a href="dictionary.html#turtles"><tt>turtles</tt></a> is an
&quot;agentset&quot;, the set of all turtles. <a href=
"dictionary.html#count"><tt>count</tt></a> tells us how many agents
are in that set.
<p>
Let's make the second monitor now:
<blockquote>
<ul>
<li>Create a monitor by clicking the Add icon on the toolbar,
selecting Monitor next to it, and clicking on an open spot in the
Interface.
</ul>
<p class="question">
A dialog box will appear.
<ul>
<li>In the Reporter section of the dialog box type: <tt>count
patches with [pcolor = green]</tt> (see image below).
<li>In the Display name section of the dialog box type: <tt>green
patches</tt>
<li>Press the OK button to close the dialog box.
</ul>
</blockquote>
<p class="screenshot">
<img alt="screen shot" src="images/tutorial3/monitor-2.gif">
<p>
Here we're using <a href=
"dictionary.html#count"><tt>count</tt></a> again to see how many
agents are in an agentset. <a href=
"dictionary.html#patches"><tt>patches</tt></a> is the set of all the
patches, but we don't just want to know how many patches there
are total, we want to know how many of them are green. That's
what <a href="dictionary.html#with"><tt>with</tt></a> does; it makes
a smaller agentset of just those agents for whom the condition in the
brackets is true. The condition is <tt>pcolor = green</tt>, so that
gives us just the green patches.
<p>
Now we have two monitors that will report how many turtles and green
patches we have, to help us track what's going on in our model.
As the model runs, the numbers in the monitors will automatically
change.
<blockquote>
<ul>
<li>Use the setup and go buttons and watch the numbers in the
monitors change.
</ul>
</blockquote>
<h2>
Switches and labels
</h2>
<p>
The turtles aren't just turning the patches black. They're
also gaining and losing energy. As the model runs, try using a turtle
monitor to watch one turtle's energy go up and down.
<p>
It would be nicer if we could see every turtle's energy all the
time. We will now do exactly that, and add a switch so we can turn
the extra visual information on and off.
<blockquote>
<ul>
<li>Click on the Add icon on the toolbar (in the Interface tab).
<li>Select Switch from the menu next to Add.
<li>Click on an open spot in the interface.
</ul>
<p class="question">
A dialog will appear.
<ul>
<li>Into the Global variable field, type <tt>show-energy?</tt>
Don't forget to include the question mark in the name. (See
image below.)
</ul>
</blockquote>
<p class="screenshot">
<img alt="screen shot" src="images/tutorial3/switch.gif">
<blockquote>
<ul>
<li>Now go back to the 'go' procedure using the Code tab
with the Toolbar.
<li>Rewrite the <tt>eat-grass</tt> procedure as follows:
<pre>
to eat-grass
ask turtles [
if pcolor = green [
set pcolor black
set energy energy + 10
]
ifelse show-energy?
[ set label energy ]
[ set label &quot;&quot; ]
]
end
</pre>
</ul>
</blockquote>
<p>
The <tt>eat-grass</tt> procedure introduces the <a href=
"dictionary.html#ifelse"><tt>ifelse</tt></a> command. Look at the
code carefully. Each turtle, when it runs these new commands, checks
the value of <tt>show-energy?</tt> (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).
<p>
(In NetLogo, a piece of text is called a &quot;string&quot;, short
for string of characters. A string is a sequence of letters or other
characters, written between double quotes. Here we have two double
quotes right next to each other, with nothing in between them.
That's an empty string. If a turtle's label is an empty
string, no text is attached to the turtle.)
<blockquote>
<ul>
<li>Test this in the Interface tab, by running the model (using the
setup and go buttons) switching the <tt>show-energy?</tt> switch
back and forth.
</ul>
</blockquote>
<p>
When the switch is on, you'll see the energy of each turtle go up
each time it eats grass. You'll also see its energy going down
whenever it moves.
<p class="screenshot">
<img alt="screen shot" src="images/tutorial3/show-energy.png">
<h2>
More procedures
</h2>
<p>
Now our turtles are eating. Let's make them reproduce and die,
too. And let's make the grass grow back. We'll add all three
of these of these behaviors now, by making three separate procedures,
one for each behavior.
<blockquote>
<ul>
<li>Go to the Code tab.
<li>Rewrite the <tt>go</tt> procedure as follows:
<pre>
to go
move-turtles
eat-grass
reproduce
check-death
regrow-grass
tick
end
</pre>
<li>Add the procedures for <tt>reproduce</tt>,
<tt>check-death</tt>, and <tt>regrow-grass</tt> as shown below:
<pre>
to reproduce
ask turtles [
if energy &gt; 50 [
set energy energy - 50
hatch 1 [ set energy 50 ]
]
]
end
to check-death
ask turtles [
if energy &lt;= 0 [ die ]
]
end
to regrow-grass
ask patches [
if random 100 &lt; 3 [ set pcolor green ]
]
end
</pre>
</ul>
</blockquote>
<p>
Each of these procedures uses the <a href=
"dictionary.html#if"><tt>if</tt></a> command. Each turtle, when it
runs <tt>check-death</tt> it will check to see if its energy is less
or equal to 0. If this is true, then the turtle is told to <a href=
"dictionary.html#die"><tt>die</tt></a> (die is a NetLogo primitive).
<p>
When each turtle runs <tt>reproduce</tt>, it checks the value of the
turtle's <tt>energy</tt> 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 <a href=
"dictionary.html#hatch"><tt>hatch</tt></a> command is a NetLogo
primitive which looks like this: <tt>hatch</tt> <i>number</i> [
<i>commands</i> ]. This turtle creates <i>number</i> new turtles,
each identical to its parent, and asks the new turtle(s) that have
been hatched to run <i>commands</i>. 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.
<p>
When each patch runs <tt>regrow-grass</tt> it will check to see if a
random integer from 0 to 99 is less than 3. If so, the patch color is
set to green. This will happen 3% of the time (on average) for each
patch, since there are three numbers (0, 1, and 2) out of 100
possible that are less than 3.
<blockquote>
<ul>
<li>Switch to the Interface tab now and press the setup and go
buttons.
</ul>
</blockquote>
<p>
You should see some interesting behavior in your model now. Some
turtles die off, some new turtles are created (hatched), and some
grass grows back. This is exactly what we set out to do.
<p>
If you continue to watch your monitors in your model, you will see
that the <b>count turtles</b> and <b>green patches</b> monitors both
fluctuate. Is this pattern of fluctuation predictable? Is there a
relationship between the variables?
<p>
It'd be nice if we had a easier way to track the changes in the
model behavior over time. NetLogo allows us to plot data as we go
along. That will be our next step.
<h2>
Plotting
</h2>
<p>
To make plotting work, we'll need to create a plot in the
Interface tab and put some commands inside it.
<p>
The commands we put in the plots will run automatically when our
<tt>setup</tt> procedure calls <tt>reset-ticks</tt> and when our
<tt>go</tt> procedure calls <tt>tick</tt>.
<blockquote>
<ul>
<li>Create a plot by clicking the Add icon on the toolbar,
selecting Plot next to it, and clicking on an open spot in the
Interface.
<li>Set its Name to &quot;Totals&quot; (see image below)
<li>Set the X axis label to &quot;time&quot;
<li>Set the Y axis label to &quot;totals&quot;
<li>Change the name of the &quot;default&quot; pen to
&quot;turtles&quot;.
<li>Enter <tt>plot count turtles</tt> under Pen Update Commands.
<li>Press the &quot;Add Pen&quot; button.
<li>Change the name of the new pen to &quot;grass&quot;.
<li>Enter <tt>plot count patches with [pcolor = green]</tt> under
Pen Update Commands.
</ul>
</blockquote>
<p>
When you're done, the dialog should look like this:
<p class="screenshot">
<img alt="screen shot" src="images/tutorial3/totals.png">
<blockquote>
<ul>
<li>Press OK in the Plot dialog to finish editing.
</ul>
</blockquote>
<p>
Note that 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
&quot;Auto Scale&quot; 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.
<p>
Note that we used the <a href=
"dictionary.html#plot"><tt>plot</tt></a> 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.
<blockquote>
<ul>
<li>Setup and run the model again.
</ul>
</blockquote>
<p>
You can now watch the plot being drawn as the model is running. Your
plot should have the general shape of the one below, though your plot
might not look exactly the same.
<p>
Remember that we left &quot;Auto Scale?&quot; on. This allows the
plot to readjust itself when it runs out of room.
<p class="screenshot">
<img alt="screen shot" src="images/tutorial3/plotwindow.png">
<p>
If you forget which pen is which, you can edit the plot and check the
&quot;Show legend?&quot; checkbox.
<p>
You might try running the model several times to see what aspects of
the plot are the same and which are different from run to run.
<h2>
Tick counter
</h2>
<p>
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. Learning how to stop or start an action at a specific time can
help make this happen by stopping the model at the same point each
model run. Keeping track of how many times the <tt>go</tt> procedure
is run is a useful way to cue these actions. That's what the tick
counter does.
<p>
You're already using the tick counter in your model, with the
<tt>reset-ticks</tt> and <tt>tick</tt> commands, which also trigger
plotting.
<p>
You can also use the tick counter for other things, such as to set a
limit on the total length of a run.
<blockquote>
<ul>
<li>Change the <tt>go</tt> procedure:
<pre>
to go
if ticks &gt;= 500 [ stop ]
move-turtles
eat-grass
check-death
reproduce
regrow-grass
tick
end
</pre>
<li>Now setup and run the model.
</ul>
</blockquote>
<p>
The graph and model won't keep running forever. They should stop
automatically when the tick counter in the Interface tab's
toolbar reaches 500.
<p>
The <a href="dictionary.html#tick"><tt>tick</tt></a> command advances
the tick counter by 1. <a href=
"dictionary.html#ticks"><tt>ticks</tt></a> is a reporter which
reports the current value of the tick counter. <a href=
"dictionary.html#reset-ticks"><tt>reset-ticks</tt></a>, in your
<tt>setup</tt> procedure, takes care of restarting the tick counter
at 0 when a new run is set up and ready to begin.
<h2>
Some more details
</h2>
<p>
First, instead of always using 100 turtles, you can have a varying
number of turtles.
<blockquote>
<ul>
<li>Create a slider named &quot;number&quot;: click the Add icon on
the toolbar, select Slider next to it, and click on an open spot in
the interface.
<li>Try changing the minimum and maximum values in the slider.
<li>Then inside of <tt>setup-turtles</tt>, instead of
<tt>create-turtles 100</tt> you can type:
<pre>
to setup-turtles
create-turtles number [ setxy random-xcor random-ycor ]
end
</pre>
</ul>
</blockquote>
<p>
Test this change and compare how having more or fewer turtles
initially affect the plots over time.
<p>
Second, wouldn't it be nice to adjust the energy the turtles gain
and lose as they eat grass and reproduce?
<blockquote>
<ul>
<li>Make a slider called <tt>energy-from-grass</tt>.
<li>Make another slider called <tt>birth-energy</tt>.
<li>Then, inside of <tt>eat-grass,</tt> make this change:
<pre>
to eat-grass
ask turtles [
if pcolor = green [
set pcolor black
set energy (energy + energy-from-grass)
]
ifelse show-energy?
[ set label energy ]
[ set label &quot;&quot; ]
]
end
</pre>
<li>And, inside of <tt>reproduce</tt>, make this change:
<pre>
to reproduce
ask turtles [
if energy &gt; birth-energy [
set energy energy - birth-energy
hatch 1 [ set energy birth-energy ]
]
]
end
</pre>
</ul>
</blockquote>
<p>
Finally, what other slider could you add to vary how often grass
grows back? Are there rules you can add to the movement of the
turtles or to the newly hatched turtles that happen only at certain
times? Try writing them.
<h2>
What's next?
</h2>
<p>
So now you have a simple model of an ecosystem. Patches grow grass.
Turtles wander, eat the grass, reproduce, and die.
<p>
You have created an interface containing buttons, sliders, switches,
monitors, and a plot. You've even written a series of procedures
to give the turtles something to do.
<p>
That's where this tutorial leaves off.
<p>
If you'd like to look at some more documentation about NetLogo,
the <a href="interface.html">Interface Guide</a> section of the
manual walks you through every element of the NetLogo interface in
order and explains its function. For a detailed description and
specifics about writing procedures, refer to the <a href=
"programming.html">Programming Guide</a>. All of the primitives are
listed and described in the the <a href="dictionary.html">NetLogo
Dictionary</a>.
<p>
Also, you can continue experimenting with and expanding this model if
you'd like, experimenting with different variables and behaviors
for the agents.
<p>
Alternatively, you may want to revisit the first model in the
tutorial, Wolf Sheep Predation. This is the model you used in
<tt>Tutorial #1</tt>. In the Wolf Sheep Predation model, you saw
sheep move around, consume resources that are replenished
occasionally (grass), reproduce under certain conditions, and die if
they ran out of resources. But that model had another type of
creature moving around -- wolves. The addition of wolves requires
some additional procedures and some new primitives. Wolves and sheep
are two different &quot;breeds&quot; of turtle. To see how to use
breeds, study Wolf Sheep Predation.
<p>
Alternatively, you can look at other models (including the many
models in the Code Examples section of the Models Library) or even go
ahead and build your own model. You don't even have to model
anything. It can be interesting just to watch patches and turtles
forming patterns, to try to create a game to play, or whatever.
<p>
Hopefully you have learned some things, both in terms of the NetLogo
language and about how to go about building a model. The entire set
of procedures that was created above is shown below.
<h2>
<a name="sample" id="sample">Appendix: Complete code</a>
</h2>
<p>
The complete model is also available in NetLogo's Models Library,
in the Code Examples section. It's called &quot;Tutorial 3&quot;.
<p>
Notice that this listing is full of &quot;comments&quot;, which begin
with semicolons. 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.
<p>
In the Code tab, comments are gray, so your eyes can pick them out
easily.
<pre>
turtles-own [energy] ;; for keeping track of when the turtle is ready
;; to reproduce and when it will die
to setup
clear-all
setup-patches
setup-turtles
reset-ticks
end
to setup-patches
ask patches [ set pcolor green ]
end
to setup-turtles
create-turtles number ;; uses the value of the number slider to create turtles
ask turtles [ setxy random-xcor random-ycor ]
end
to go
if ticks &gt;= 500 [ stop ] ;; stop after 500 ticks
move-turtles
eat-grass
check-death
reproduce
regrow-grass
tick ;; increase the tick counter by 1 each time through
end
to move-turtles
ask turtles [
right random 360
forward 1
set energy energy - 1 ;; when the turtle moves it looses one unit of energy
]
end
to eat-grass
ask turtles [
if pcolor = green [
set pcolor black
;; the value of energy-from-grass slider is added to energy
set energy energy + energy-from-grass
]
ifelse show-energy?
[ set label energy ] ;; the label is set to be the value of the energy
[ set label &quot;&quot; ] ;; the label is set to an empty text value
]
end
to reproduce
ask turtles [
if energy &gt; birth-energy [
set energy energy - birth-energy ;; take away birth-energy to give birth
hatch 1 [ set energy birth-energy ] ;; give this birth-energy to the offspring
]
]
end
to check-death
ask turtles [
if energy &lt;= 0 [ die ] ;; removes the turtle if it has no energy left
]
end
to regrow-grass
ask patches [ ;; 3 out of 100 times, the patch color is set to green
if random 100 &lt; 3 [ set pcolor green ]
]
end
</pre>
Jump to Line
Something went wrong with that request. Please try again.