Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

649 lines (578 sloc) 25.154 kb
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<title>
NetLogo User Manual: Tutorial #2: Commands
</title>
<link rel="stylesheet" href="netlogo.css" type="text/css">
<meta http-equiv="Content-Type" content="text/html; charset=us-ascii">
<h1>
Tutorial #2: Commands
</h1>
<div class="version">
NetLogo User Manual&nbsp;&nbsp;&nbsp;
</div>
<p>
So far you've successfully navigated your way through opening and
running models, pressing buttons, changing sliders and switches, and
gathering information from a model using plots and monitors.
<p>
In this section, the focus will start to shift from observing models
to manipulating models. You will start to see the inner workings of
the models and be able to change how they look.
<h2>
Sample Model: Traffic Basic
</h2>
<blockquote>
<ul>
<li>Open the Models Library (from the File menu).
<li>Open Traffic Basic, found in the &quot;Social Science&quot;
section.
<li>Run the model for a while to get a feel for it.
<li>Consult the Info tab for any questions you may have.
</ul>
</blockquote>
<p>
In this model, you will notice one red car in a stream of blue cars.
The stream of cars are all moving in the same direction. Every so
often they &quot;pile up&quot; and stop moving. This is modeling how
traffic jams can form without a specific cause like an accident.
<p>
You may alter the settings and observe a few runs to get a fuller
understanding of the model.
<blockquote>
<p class="question">
As you are using the Traffic Basic model, have you noticed any
additions you would like to make to the model?
</blockquote>
<p>
Looking at the Traffic Basic model, you may notice the environment is
fairly simple; a black background with a white street and number of
blue cars and one red car. Changes that could be made to the model
include: changing the color and shape of the cars, adding a house or
street light, creating a stop light, or even creating another lane of
traffic. Some of these suggested changes are visual, to enhance the
look of the model, while others are more behavioral. We will be
focusing more on the simpler or cosmetic changes throughout most of
this tutorial. (<a href="tutorial3.html">Tutorial #3</a> will go into
greater detail about behavioral changes, which require changing the
Code tab.)
<p>
To make these simple changes we will be using the Command Center.
<h2>
Command Center
</h2>
<p>
The Command Center is found in the Interface tab. It allows you to
enter commands or directions to a model. Commands are instructions
you can give to NetLogo's agents: turtles, patches, links, and
the observer.
<blockquote>
<p>
In Traffic Basic:
<ul>
<li>Press the &quot;setup&quot; button.
<li>Locate the Command Center.
<li>Click the mouse in the white box at the bottom of the Command
Center.
<li>Type the text shown here:
</ul>
<p class="screenshot">
<img alt="screen shot" src="images/tutorial2/image001.gif">
<ul>
<li>Press the return key.
</ul>
<p class="question">
What happened to the View?
<p>
You may have noticed the background of the View has turned all
yellow and the street has disappeared.
<p class="question">
Why didn't the cars turn yellow too?
<p>
Looking back at the command that was written, we asked only the
patches to change their color. In this model, the cars are
represented by a different kind of agent, called
&quot;turtles&quot;. Therefore, the cars did not receive these
instructions and thus did not change.
<p class="question">
What happened in the Command Center?
<p>
You may have noticed that the command you just typed is now
displayed in the Command Center as shown below:
<p class="screenshot">
<img alt="screen shot" src=
"images/tutorial2/commandcenterhistory.gif">
<ul>
<li>Type in the bottom of the Command Center the text shown below:
</ul>
<p class="screenshot">
<img alt="screen shot" src="images/tutorial2/image002.gif">
<p class="question">
Was the result what you expected?
<p>
The view should have a yellow background with a line of brown cars
in the middle:
<p class="screenshot">
<img alt="screen shot" width="90%" src=
"images/tutorial2/image003.gif">
</blockquote>
<p>
The NetLogo world is a two dimensional world that is made up of
turtles, patches, links, and an observer. The patches are the ground
over which the turtles move. Links are connections between turtles.
And the observer is a being that oversees everything that is going
on. (For more specifics, refer to the <a href=
"programming.html">NetLogo Programming Guide</a>.)
<p>
In the Command Center you can give commands to any of these types of
agents. You choose which type to talk to by using the popup menu
located in the bottom left corner. You can also use the tab key on
your keyboard to cycle through the different types.
<blockquote>
<ul>
<li>In the Command Center, click on the &quot;observer&gt;&quot; in
the bottom left corner:
</ul>
<p class="screenshot">
<img alt="screen shot" src="images/tutorial2/image004.gif">
<ul>
<li>Choose &quot;turtles&quot; from the popup menu.
<li>Type <tt>set color pink</tt> and press return.
<li>Press the tab key until you see &quot;patches&gt;&quot; in the
bottom left corner.
<li>Type <tt>set pcolor white</tt> and press return.
</ul>
<p class="question">
What does the View look like now?
<p class="question">
Do you notice any differences between these two commands and the
observer commands from earlier?
</blockquote>
<p>
The observer oversees the world and therefore can give a command to
the patches or turtles using <a href=
"dictionary.html#ask"><tt>ask</tt></a>. Like in the first example
(observer&gt; <tt>ask patches [set pcolor yellow]</tt>), the observer
has to ask the patches to set their pcolor to yellow. But when a
command is directly given to a group of agents like in the second
example (patches&gt; <tt>set pcolor white</tt>), you only have to
give the command itself.
<blockquote>
<ul>
<li>Press &quot;setup&quot;.
</ul>
<p class="question">
What happened?
</blockquote>
<p>
Why did the View revert back to the old version, with the black
background and white road? Upon pressing the &quot;setup&quot;
button, the model will reconfigure itself back to the settings
outlined in the Code tab. The Command Center doesn't permanently
change the model. It allows you to manipulate the NetLogo world
directly to further answer those &quot;What if&quot; questions that
pop up as you are investigating the models. (The Code tab is
explained in the next tutorial, and in the <a href=
"programming.html">Programming Guide</a>.)
<p>
Now that we have familiarized ourselves with the Command Center,
let's look at some more details about how colors work in NetLogo.
<h2>
Working with colors
</h2>
<p>
You may have noticed in the previous section that we used two
different words for changing color: <a href=
"dictionary.html#color"><tt>color</tt></a> and <a href=
"dictionary.html#pcolor"><tt>pcolor</tt></a>.
<blockquote>
<p class="question">
What is the difference between color and pcolor?
<ul>
<li>Choose &quot;turtles&quot; from the popup menu in the Command
Center (or use the tab key).
<li>Type <tt>set color blue</tt> and press return.
</ul>
<p class="question">
What happened to the cars?
<p>
Think about what you did to make the cars turn blue, and try to
make the patches turn red.
<p>
If you try to ask the patches to <tt>set color red</tt>, an error
message occurs:
<p class="screenshot">
<img alt="screen shot" src="images/tutorial2/image005.gif">
<ul>
<li>Type <tt>set pcolor red</tt> instead and press return.
</ul>
</blockquote>
<p>
We call <a href="dictionary.html#color"><tt>color</tt></a> and
<a href="dictionary.html#pcolor"><tt>pcolor</tt></a>
&quot;variables&quot;. Some commands and variables are specific to
turtles and some are specific to patches. For example, the <a href=
"dictionary.html#color"><tt>color</tt></a> variable is a turtle
variable, while the <a href=
"dictionary.html#pcolor"><tt>pcolor</tt></a> variable is a patch
variable.
<p>
Go ahead and practice altering the colors of the turtles and patches
using the <a href="dictionary.html#set"><tt>set</tt></a> command and
these two variables.
<p>
To be able to make more changes to the colors of turtles and patches,
or shall we say cars and backgrounds, we need to gain a little
insight into how NetLogo deals with colors.
<!--<p align=center style='text-align:center'><img alt="screen shot"
src="images/tutorial2/image006.jpg">-->
<p>
In NetLogo, colors have a numeric value. In all of the exercises we
have been using the name of the color. This is because NetLogo
recognizes 16 different color names. This does not mean that NetLogo
only recognizes 16 colors. There are many shades in between these
colors that can be used too. Here's a chart that shows the whole
NetLogo color space:
<p class="screenshot">
<img alt="[color chart]" src="images/colors.jpg">
<p>
To get an intermediate shade, you refer to it by a number instead, or
by adding or subtracting a number from a name. For example, when you
type <tt>set color red</tt>, this does the same thing as if you had
typed <tt>set color 15</tt>. And you can get a lighter or darker
version of the same color by using a number that is a little larger
or a little smaller, as follows. <!--How would you
communicate that you would like all the patches a light pale pink
color. It would be very difficult for NetLogo to filter through and
decipher what light and pale mean in reference to pink.
What would be
a good way to communicate that all patches turn a pale pink. Hint: If
pink is equal to the value of 135, then what would be the value of a
pale pink color?
<h2 style='margin-left:1.0in'>++++What would you set your patch color at to get
a pale pink color?</h2>
-->
<blockquote>
<ul>
<li>Choose &quot;patches&quot; from the popup menu in the Command
Center (or use the tab key).
<li>Type <tt>set pcolor red - 2</tt> (The spacing around the
&quot;-&quot; is important.)
</ul>
<p>
By subtracting from red, you make it darker.
<ul>
<li>Type <tt>set pcolor red + 2</tt>
</ul>
<p>
By adding to red, you make it lighter.
</blockquote>
<!--<p>The charts above contains 14 different colors each
correlating with a number. There are variations or shades of each of those
colors that are not listed in the chart. Take a color like blue and experience
the color variations by increasing it's color number by one.
<p>&nbsp;
<p style='margin-left:1.0in;text-indent:-.25in;tab-stops:list 58.5pt'>
****Example, &amp;&amp;&amp;&amp;P&gt;
set pcolor 105
<p style='margin-left:1.0in;text-indent:-.25in;tab-stops:list 58.5pt'>
****
&amp;&amp;&amp;&amp;P&gt;set pcolor 106
<p style='margin-left:1.0in;text-indent:-.25in;tab-stops:list 58.5pt'>
****
&amp;&amp;&amp;&amp;P&gt;set pcolor 107
<p style='margin-left:1.0in;text-indent:-.25in;tab-stops:list 58.5pt'>
****
&amp;&amp;&amp;&amp;P&gt;set pcolor 108 and so forth.
-->
<p>
You can use this technique on any of the colors listed in the chart.
<!--
And for more interesting and creative things you can do with
colors, see the section on color in the <A HREF="programming.html">Programming Guide</A>.
-->
<!--
<blockquote>
<p><B>Challenge:</B>
<p>Before starting this challenge, press "clear" and "clear history"
in the Command Center and press the "setup" button.
<p class="question">Can you alter the Traffic Basic model to have an
all green background and all black cars?
<p>Use the picture below as a reference.
<p class=MsoBodyText3 align=center style='text-align:center'><img alt="screen shot"
src="images/tutorial2/image007.gif">
<p class=MsoBodyText3 align=center style='text-align:center'>&nbsp;
<p class=MsoBodyText3 style='text-align:justify'>You
will notice that by changing all the cars to black we have lost our red
"marker" car. If you recall, this car has a monitor on the screen that shows
its speed to you.
<p class=MsoBodyText3 style='text-align:justify'>&nbsp;
<p class=MsoBodyText3 style='margin-left:1.0in;text-align:justify;text-indent:
-.25in;tab-stops:list 1.0in'>
****Press "drive"
<p class=MsoBodyText3 style='text-align:justify'>&nbsp;
<p class=MsoBodyText3 style='text-align:justify'>The
monitor is still displaying the red car's speed even though the car is not
present. Or is it?
<p class=MsoBodyText3 style='text-align:justify'>&nbsp;
<p class=MsoBodyText3 style='margin-left:1.0in;text-align:justify'><i>++++Is the red car still present in the Traffic
Basic model?</i>
<p class=MsoBodyText3 style='text-align:justify'>&nbsp;
-->
<h2>
Agent Monitors and Agent Commanders
</h2>
<p>
In the previous activity, we used the <tt>set</tt> command to change
the colors of all the cars. But if you recall, the original model
contained one red car amongst a group of blue cars. Let's look at
how to change only one car's color.
<blockquote>
<ul>
<li>Press &quot;setup&quot; to get the red car to reappear.
<li>Right-click on the red car.
<li>If there is another turtle close to the red turtle you'll
see more than one turtle listed at the bottom of the menu. Move
your mouse over the turtle selections, notice when your mouse
highlights a turtle menu item that turtle is highlighted in the
view. Select &quot;inspect turtle&quot; from the sub-menu for the
red turtle.
</ul>
<p>
A turtle monitor for that car will appear:
<p class="screenshot">
<img alt="screen shot" src="images/tutorial2/image009.gif">
</blockquote>
<p>
The mini-view at the top of the agent monitor will always stay
centered on this agent. You can zoom the view in and out using the
slider below the view and you can watch this turtle in the main view
by pressing the &quot;watch-me&quot; button.
<p>
Taking a closer look at this turtle monitor, we can see all of the
variables that belong to the red car. A variable is a location that
holds a value.
<p>
Let's take a closer look at the turtle monitor:
<blockquote>
<p class="question">
What is this turtle's who number?
<p class="question">
What color is this turtle?
<p class="question">
What shape is this turtle?
</blockquote>
<p>
This turtle monitor is showing a turtle who that has a who number of
0, a color of 15 (red -- see chart above), and the shape of a car.
<p>
There are two other ways to open a turtle monitor besides
right-clicking. One way is to choose &quot;Turtle Monitor&quot; from
the Tools menu, then type the who number of the turtle you want to
inspect into the &quot;who&quot; field and press return. The other
way is to type <tt>inspect turtle 0</tt> (or other who number) into
the Command Center.
<p>
You close a turtle monitor by clicking the close box in the upper
left hand corner (Mac) or upper right hand corner (other operating
systems).
<p>
Now that we know more about Agent Monitors, we have three ways to
change an individual turtle's color.
<p>
One way is to use the box called an Agent Commander found at the
bottom of an Agent Monitor. You type commands here, just like in the
Command Center, but the commands you type here are only done by this
particular turtle.
<blockquote>
<ul>
<li>In the Agent Commander of the Turtle Monitor for turtle 0, type
<tt>set color pink</tt>.
</ul>
<p class="question">
What happens in the View?
<p class="question">
Did anything change in the Turtle Monitor?
</blockquote>
<p>
A second way to change one turtle's color is to go directly to
the color variable in the Turtle Monitor and change the value.
<blockquote>
<ul>
<li>Select the text to the right of &quot;color&quot; in the Turtle
Monitor.
<li>Type in a new color such as <tt>green + 2</tt>.
</ul>
<p class="question">
What happened?
</blockquote>
<p>
The third way to change an individual turtle's or patch's
color is to use the observer. Since, the observer oversees the
NetLogo world, it can give commands that affect individual turtles,
as well as groups of turtles.
<blockquote>
<ul>
<li>In the Command Center, select &quot;observer&quot; from the
popup menu (or use the tab key).
<li>Type <tt>ask turtle 0 [set color blue]</tt> and press return.
</ul>
<p class="question">
What happens?
</blockquote>
<blockquote>
<p>
Just as there are Turtle Monitors, there are also Patch Monitors.
Patch monitors work very similarly to Turtle Monitors.
<p class="question">
Can you make a patch monitor and use it to change the color of a
single patch?
</blockquote>
<p>
If you try to have the observer <tt>ask patch 0 [set pcolor
blue]</tt>, you'll get an error message:
<p class="screenshot">
<img alt="screen shot" src="images/tutorial2/image010.gif">
<p>
To ask an individual turtle to do something, we use its who number.
But patches don't have who numbers, therefore we need to refer to
them some other way.
<p>
Remember, patches are arranged on a coordinate system. Two numbers
are needed to plot a point on a graph: an x-axis value and a y-axis
value. Patch locations are designated in the same way as plotting a
point.
<blockquote>
<ul>
<li>Open a patch monitor for any patch.
</ul>
<p class="screenshot">
<img alt="screen shot" src="images/tutorial2/image011.gif">
<p>
The monitor shows that for the patch in the picture, its <a href=
"dictionary.html#pxcor"><tt>pxcor</tt></a> variable is -19 and its
<a href="dictionary.html#pycor"><tt>pycor</tt></a> variable is 0.
If we go back to the analogy of the coordinate plane and wanted to
plot this point, the point would be found on the x axis left of the
origin, where x=-19 and y=0.
<p>
To tell this particular patch to change color:
<ul>
<li>In the bottom of the patch monitor, enter <tt>set pcolor
blue</tt> and press return.
</ul>
<p>
Typing a command in a turtle or patch monitor addresses only that
turtle or patch.
<p>
You can also talk to a single patch from the Command Center:
<ul>
<li>In the Command Center, enter <tt>ask patch -19 0 [set pcolor
green]</tt> and press return.
</ul>
</blockquote><!--
<h3>!!!!!Challenge</h3>
<p><b>!!!!Using the Traffic Basic model, alter the background
to be green with a white street, black cars while still keeping the red "pace"
car. Use the picture below as a reference.</b>
<p><b>&nbsp;</b>
<p align=center style='text-align:center'><b><img alt="screen shot"
src="images/tutorial2/image012.gif"></b>
<p align=center style='text-align:center'><b>&nbsp;</b>
<p class=MsoBodyText3>There are many ways you
can go about solving this challenge, some of them are a little laborious then
others. In the next section, we will look at procedures that and commands,
that simplify this activity.
<p>&nbsp;
-->
<!--
<h2>Changing Shapes</h2>
<p>Another part of customizing models is altering or changing the
shapes of the turtles. The commands for changing shapes are very
similar to those for changing colors.
<p>amp;&amp;&amp;&amp;T&gt;set shape "default"
<p>&nbsp;
<h4 style='margin-left:1.0in'>++++Try changing the patches to the "default"
shape.</h4>
<p>&nbsp;
<p>If you tried &amp;&amp;&amp;&amp;P&gt;set shape "default", an error message was probably
expressed like the one below. This is because patches have only one
shape-square. The shape primitive is only for turtles.
<p align=center style='text-align:center'><img alt="screen shot"
src="images/tutorial2/image013.gif">
<p align=center style='text-align:center'>&nbsp;
<p style='margin-left:1.0in'><i>++++How would you change a
shape of a turtle using the observer?</i>
<p align=center style='margin-left:1.0in;text-align:center'><i>&nbsp;</i>
<p style='margin-left:1.0in'><i>++++How do you know which
shapes are usable within each model? </i>
<p>&nbsp;
<p>
****Open "Shapes Editor" , found under "Tools" in the menu bar.
<p>&nbsp;
<p>The Shapes Editor is a feature that can change the shape of
the turtles, edit or alter existing shapes or import a shape from a
previous model. When you open the Traffic Basic model's Shapes Editor you will
see three shapes displayed: the default shape, a car and truck-right. In order
to change the shape within the model, you have to write a command within the
Command Center that sets the shape primitive to one of the three shapes found
in the shape editor. Just highlighting one of the shapes in the Shapes Editor
does not change the shape.
<p>&nbsp;
<p align=center style='text-align:center'><img alt="screen shot"
src="images/tutorial2/image014.gif">
<p>&nbsp;
<p>If there is a shape that is not displayed here that you
want, you can do one of two things. You can either create your own shape
within the edit function of this window; or you can import a pre-existing shape
from another model. For more information on creating and designing shapes see
the %%%%Shapes Editor in the Advanced Features Section of the manual.
<p>&nbsp;
<p>To import a shape into the Traffic Basic model:
<p>&nbsp;
<p>
****Select "import" in the Shapes Editor window.
<p>
****A new window will open and select the "models" folder.
<p>
****Go to "Sample Models" and open the "Sheep Wolf
Predation.nlogo" model in the "Biology" folder.
<p>&nbsp;
<p align=center style='text-align:center'><img alt="screen shot"
src="images/tutorial2/image015.gif">
<p align=center style='text-align:center'>&nbsp;
<p>
****Pick one of these shapes and press "import".
<p>&nbsp;
<p>You will notice the new shape will appear in Shapes Editor
window for the Traffic Basic model. It may be used in the Command Center in
the same manor as "truck-right". Make sure to save when prompted to ensure
that the new shape will be available for that model.
<p>&nbsp;
<p>When creating new models, there is a wider selection of
shapes to chose from within the Shapes Editor. The newer models will contain
all of these shapes, while older models tend to be sparse. If you are looking
to see all of the shapes already stored in the Shapes Editor, open a new model
under file and then open the Shapes Editor.
<p><b>&nbsp;</b>
-->
<!--
<h3>!!!!Ultimate Challenge</h3>
<p>&nbsp;
<p class=MsoBodyText3>!!!!Using the Traffic Basic model, change the background
to "green" grass, include a white one lane street down the middle of the
View. On this street include four houses each with their own street
light; two on one side of the street and two on the other side of the street.
The traffic flow can contain a mixture of black cars and trucks making sure to
keep the "red" pace car. If you would like to add a person you may do so.
-->
<h2>
What's Next?
</h2>
<p>
At this point, you may want to take some time to try out the
techniques you've learned on some of the other models in the
Models Library.
<p>
In <a href="tutorial3.html">Tutorial #3: Procedures</a> you can learn
how to alter and extend existing models and build your own models.
Jump to Line
Something went wrong with that request. Please try again.