Sunteți pe pagina 1din 15

CS151L Fall 2013

Week 1: Netlogo Command Cheat Sheet


Command Command Description
Statement used to name and start a procedure
to <name>
Example: to setup
end Statement used to end a procedure
clear-all Deletes all turtles and resets all patches to default values
Creates the specified number of turtles at origin and gives a random heading to each turtle
create-turtles #
Example: create-turtles 1
Asks the turtles to perform the instructions (commands) given in square brackets. Each
turtle performs the instructions one at a time.
ask turtles […..]
Example: ask turtles
[forward 1]
Tells an agent to move forward a number of patches
forward #
Example: forward 1
Tells an agent to move back a number of patches
back #
Example: back 1
pen-down Tells an agent to put its pen down draw a trail
pen-up Tells an agent to pick up its pen and stop drawing a trail
Move the turtle to the x-coordinate and y-coordinate given
setxy # #
Example: setxy 10 20
Sets heading or direction that the agent faces in degrees relative to the Netlogo coordinate
set heading # system
Example: set heading 90
Asks the turtle to turn right a certain number of degrees relative to the direction the turtle is
right # facing
Example: right 90
Asks the turtle to turn left a certain number of degrees relative to the direction the turtle is
left # facing
Example: left 90

Netlogo_Commands_week1.docx
CS151L Fall 2013
Week 2: Netlogo Command Cheat Sheet

Command Command Description


clear-turtles Removes all the turtles without erasing the Netlogo surface or changing the
patches
set color color_name Specifies or changes the color of the turtles to the specified color
(color_name). Some possible color names include red, blue, green, orange,
yellow, pink, magenta, lime, gray, and violet.
Example: set color red
set size # Specifies or changes the size of the turtles to a specified number (#). The
default is 1.
Example: set size 3
set pen-size # Specifies or changes the size of the pen the turtles carry to a specified
number (#). The default is 1.
Example: set pen-size 3
Ask turtle # [ ] Asks a specific turtle to perform the commands in brackets.

Netlogo_Commands_week2.docx
CS151L Fall 2013
Week 3: Netlogo Command Cheat Sheet
Command Command Description
set color # Changes an agent’s color to # (see Netlogo Programming guide for color
values). There are 140 colors in Netlogo.
Example: set color to 15 sets the color to bright red
set color R# G# B# Changes an agent’s color to R# G# B# (RGB notation) where R# stands for
how much red is in the color, G# stands for how much green is in the color
and B# stands for how much blue is in the color. The R# G# and B# can
range between 0 and 255.
Example: set color 255 0 0 sets the color to bright red
repeat # [commands] Repeats the set of commands in the square bracket a certain number (#) of
times.
Example: repeat 20
[
right 5
forward 1
]
the turtle turn right 5o and steps forward 1 step 20 times
while [condition] [commands] Repeats the set of commands in the second set of square brackets while the
condition in the first set of square brackets is true. When the condition within
the first set of square brackets is false, the loop is exited.
let local_variable_name value Creates a new local variable called local_variable_name and gives it the
given value. A local variable is one that exists only within the enclosing block
of commands such as a procedure or within the ask turtles brackets.
Example: let num1 10
This creates a local variable num1 and gives it an initial value of 10
set variable_name value Sets variable (variable_name) to the given value.
Example : set num1 25
Changes the value of the local variable num1 to 25

Netlogo_Commands_week3.docx
CS151L Fall 2013
Week 4: Netlogo Command Cheat Sheet
Command Command Description
reset-ticks Resets the tick counter to zero. Normally reset-ticks goes at the end of a setup
procedure
tick Advances the tick counter by one. If the tick counter has not been started yet with
reset-ticks, an error results. Normally tick goes at the end of a go procedure.
ticks Reports the current value of the tick counter. The result is always a positive number and
never negative. If the tick counter has not been started yet with reset-ticks, an error
results.
random # If number is positive, reports a random integer greater than or equal to 0, but strictly
less than the number #.
If number is negative, reports a random integer less than or equal to 0, but strictly
greater than number.
If number is zero, the result is always 0 as well.
Examples:
show random 3
;; prints 0, 1, or 2
show random -3
;; prints 0, -1, or -2
distancexy # # Reports the distance from this agent to the point (xcor, ycor).
The distance from a patch is measured from the center of the patch. Turtles and
patches use the wrapped distance (around the edges of the world) if wrapping is
allowed by the topology and the wrapped distance is shorter.

Netlogo_Commands_week4.docx
CS151L Fall 2013
Week 6: NetLogo Command Cheat Sheet
Command Command Description
reset-ticks Resets the tick counter. Put in the Setup Procedure after clear-all
tick Increments the tick counter for the program (for use in update view). Put in
the go procedure
globals [variable_names] A keyword that can only be used at the beginning of a program, before any
functions or procedures. It defines new global variables. Global variables are
"global" because they are accessible by all agents and can be used
anywhere in a model. Most often, globals is used to define variables or
constants that need to be used in many parts of the program.
Example: globals [NumTurtles ColorTurtles] ;; declares two global
variables NumTurtles and ColorTurtles.
turtles-own [variable_names] A keyword that can only be used at the beginning of a program, before any
functions or procedures. It defines new turtle variables unique to each turtle
Example: turtles-own [eyes legs] ;; declares two turtle variables eyes
and legs.
patches-own [variable_names] A keyword that can only be used at the beginning of a program, before any
functions or procedures. It defines new patch variables unique to each patch
Example: patches-own [patchColor] ;; declares one patch variable
patchColor.
links-own [variable_namse] A keyword that can only be used at the beginning of a program, before any
functions or procedures. It defines new link variables unique to each link
Example: links-own [traffic];; declares one link variable traffic.
[reporter] of agent Reports the value of the reporter for that agent (turtle or patch)
Example: show [pxcor] of patch 3 5 ;; prints 3

Netlogo_Commands_week6.docx
neighbors Reports an agentset containing the 8 surrounding patches (neighbors).
Example: show count turtles-on neighbors ;;prints the total number of
turtles on the eight patches around this turtle or patch
Example: ask neighbors [ set pcolor red ] ;; turns the eight neighboring
patches red
scale-color color number range1 range2 Reports a shade of color proportional to the value of number.
 Typically number is an agent variable, but may be any numeric reporter.
 If range1 is less than range2, then the larger the number, the lighter the
shade of color. But if range2 is less than range1, the color scaling is
inverted.
 If number is less than range1, then the darkest shade of color is chosen.
 If number is greater than range2, then the lightest shade of color is
chosen.
 Note: for color shade is irrelevant, e.g. green and green + 2 are
equivalent, and the same spectrum of colors will be used.
Example: set pcolor scale-color green bugs 0 maxBugsPerPatch
;; sets the patch color to a green color that ranges from 0 to the value of
maxBugsPerPatch that is proportional to how many bugs are on the patch.

Netlogo_Commands_week6.docx
CS151L Fall 2013
Week 7: NetLogo Command Cheat Sheet
Command Command Description
breed [ plural_name singular_name ] This keyword defines a breed. It is used at the beginning of the Code tab, before any
procedure definitions. The first input defines the name of the agentset associated with the
breed (the group of all member of that breed)– it is given the plural name of the breed. The
second input defines the name of a single member of the breed.
Example: breed [frogs frog] ;; defines the breed frogs, with the individual member of the
breed being called frog
create-<breeds> number 
 Creates number new breed_agents at the origin. New breed_agents have random integer
create-<breeds> number [ commands ] headings and the color is randomly selected from the 14 primary colors. If commands are
supplied, the new breed_agents immediately run them. This is useful for giving the agents a
different color, heading, or whatever. (The new agents are created all at once then run one at
a time, in random order.)
Example: create-snakes 10 ;; creates 10 snakes
create-frogs 15 [set color green] ;; creates 15 green frogs
set-default-shape turtles “string” Specifies a default initial shape for all turtles or, or for a particular breed of turtles. When a
set-default-shape <breeds> “string” turtle or a breed is created, or it changes breeds, it shape is set to the given shape. This
command doesn't affect existing agents, only agents you create afterwards. The given breed
must be already defined breed. The given string must be the name of a currently defined
shape, which can be created in the Turtle Shapes Editor or chosen from the Turtles Shapes
Editor library. In new models, the default shape for all turtles is "default". Note that specifying
a default shape does not prevent you from changing an agent's shape later. Agents don't
have to be stuck with their breed's default shape.
Example: set-default-shape cars “car” ;;sets the default shape of the breed cars to car
shape available in the Turtle Shape Editor Library
set shape “ShapeName” Used in the ask turtles or ask <breeds> command brackets to set the shape of a turtle or a
breed.
Example: ask turtles [ set shape "wolf" ] ;;sets turtle shape to wolf
ask <breed> # [set attribute #] Tells the given agent or group of agents (agentset) to set a specific attribute (NEtLogo
ask <breeds> [set attribute #] specified agent variable) to a particular value
Example: ask frog 4 [set color red] ;; sets frog 4’s color to red
ask frogs [set size 5]

Netlogo_Commands_week7.docx
ask <breeds> [commands…] Tells the given agent or group of agents (agentset) to set a run the given commands
ask <breed> # [commands…]

Examples: ask frogs [forward 10] ;; asks all the frogs to move forward 10 steps
ask <breeds> with [ ask frog 2 [right 90] ;; has frog 2 turn right 90 degrees
condition][commands] ask frogs with [color = red] [hatch 1] ;; tells all RED frogs to hatch 1 baby
<breeds>-own [var1 ...] The <breeds>-own keyword defines the variables belonging to each that breed and can only
be used at the beginning of a program, before any procedures. It you specify turtles-own
variables then all breeds of turtles will have that variable.
Example: frogs-own [energy] ;; defines a variable energy for the breed frogs
hatch # The hatch # is used to create number new turtles from EXISITING turtles. Each new turtle
hatch # [commands] inherits of all its variables, including its location, from its parent. (Exceptions: each new turtle
hatch-<breeds> # will have a new who number (agent ID number), and it may be of a different breed than its
hatch-<breeds> # [commands] parent if the hatch-<breeds> form is used.) If the hatch-<breeds> form is used, the new
turtles are created as members of the given breed. Otherwise, the new turtles are the same
breed as their parent
The new turtles then run commands. You can use the commands to give the new turtles
different colors, headings, locations, or whatever. (The new turtles are created all at once,
then run one at a time, in random order.)
Examples: hatch 1 [ left 45 forward 1 ] ;; creates one new turtle and the child turns and
moves away
hatch-sheep 1 [ set color black ] ;;this turtle creates a new turtle of the sheep breed and
sets its color to black
sprout # Creates number new turtles on a patch. The new turtles have random integer headings and
sprout # [commands] the color is randomly selected from the 14 primary colors. The turtles immediately run
sprout-<breeds> # commands. This is useful for giving the new turtles different colors, headings, or whatever.
sprout-<breeds> # [commands] (The new turtles are created all at once then run one at a time, in random order.)
If the sprout-<breeds> form is used, the new turtles are created as members of the given
breed.
Examples:
sprout 5 ;; sprouts 5 turtles
sprout-wolves 10 ;; sprouts 10 wolves
sprout 1 [ set color red ] ;; sprouts 1 turtles colored red
sprout-sheep 1 [ set color black ] ;; sprouts one black sheep

Netlogo_Commands_week7.docx
CS151L Fall 2013
Week 9: NetLogo Command Cheat Sheet
Command Command Description
while [conditional reporter] If the conditional reporter (reporter is something that returns a value) reports false, exit the loop.
[ commands Otherwise run commands and repeat. The reporter may have different values for different
] agents, so some agents may run commands a different number of times than other agents.
Example:
while [pcolor = black] ;; while the turtles are on black patches
[ forward 1
] ;; move forward 1
let listname [# # # # # ...] Usually a variable holds only one piece of information. Lists let you store multiple pieces of
set listname [# # # # #...] information in a single variable name (listname) by collecting that information in a list. Each value
in the list can be any type of value: a number, or a string, an agent or agentset, or even another
list.
One way to make a list is by simply putting the values you want in the list between brackets, after
the let or set commands Note that the individual values are separated by spaces. If you want to
create a local list, the list must be created with the let command and can be changes with the set
command. If you want to create a global list or an agent list, then you would define it at the
beginning of the program using either the globals command or a turtles-own, <breeds>-own,
patches-own or links-own keyword.
Example:
let mylist [ 1 5 8 10] ;; creates a local list called mylist containing [1 5 8 10]
set mylist [2 4 6 8] ;; changes the value of the local list mylist to [2 4 6 8]
sentence value1 value2 … Makes a list out of the values. If any value is a list, its items are included in the result directly,
rather than being included as a sublist.
Example:
set xx [1 2 3 4] ;; creates a list xx containing [1 2 3 4]
set xx sentence xx [5 6 7] ;;creates a new list xx containing [1 2 3 4 5 6 7]
show sentence “xx=” xx ;;very useful in debugging.
;;The show command can only takes one argument. If we want to
;; display two things (in the above example, both the variable name
;; and its current value), then we need to make the two things into one
;; thing. The sentence reporter does exactly that.

Netlogo_Commands_week9.docx
to-report procedure-name Used to begin a reporter procedure (procedure-name). Can input values into the
to-report procedure-name [input1 ...] procedure by using square brackets after the procedure-name. The body of the
procedure should use report to report a value for the procedure. The procedure must
end with the end command.
Example:
to-report average [a b]
report (a + b) / 2
end ;; a reporter procedure that reports out the average of two numbers
report value Immediately exits from the current to-report procedure and reports value as the result of
that procedure. report and to-report are always used in conjunction with each other.

Netlogo_Commands_week9.docx
CS151L Fall 2013
Week 10: NetLogo Command Cheat Sheet
Command Command Description
to-report procedure-name 
 Used to begin a reporter procedure (procedure-name). Input parameters for the
to-report procedure-name [input1 ...] procedure can be specified by using square brackets after procedure-name. The
body of the procedure must use report to report a value for the procedure. The
procedure must end with the end command.
Example:
to-report average [a b]
report (a + b) / 2
end
;; a reporter procedure that reports out the average of two numbers
report value Immediately exits from the current to-report procedure and reports value as the
result of that procedure. report and to-report are always used in conjunction with
each other. See example above.
face agent Set the caller turtle’s heading towards agent. If wrapping is allowed by the
topology and the wrapped distance (around the edges of the world) is shorter,
face will use the wrapped path. If the caller turtle and the other agent are at the
exact same position, the caller's heading won't change.
Example:
face turtle 1 ;; set the caller turtle heading facing turtle 1
towards agent Reports the heading from this agent to the given agent. If wrapping is allowed by
the topology and the wrapped distance (around the edges of the world) is shorter,
towards will use the wrapped path. Note: asking for the heading from an agent to
itself, or an agent on the same location, will cause a runtime error.
EXAMPLE:
set heading towards turtle 1 ;; same as "face turtle 1

Netlogo_Commands_week10.docx
LIST PRIMITIVES
let listname [ value1 value2 …] Creates a new local list variable and initializes it to contain the literal values in the
list. A local variable is one that exists only within the enclosing block of
commands. If you want to change the value afterwards, use set.
Example:
let mylist [ 10 2 5 7 ] ;; creates a local list called rlist which contains the values
10, 2, 5, and 7, in that order.
set listname [ value1 value2 …] Sets variable listname, to the given list of literal values.
Example:
set mylist [ 10 2 5 7 ]
;; sets the values in the existing variable mylist to 10, 2, 5, 7.
(list value1 value2 ...) Reports a list of values, which may include literals as well as more complicated
reporter expressions. (Parentheses are not required if exactly two values are
specified; otherwise, they are required, with opening parenthesis before list, and
closing parenthesis after the final value to be included in the list.)
Example:
set rlist (list random 10 random 20 random 30) ;; sets the values in the existing
variable mylist to three random numbers.
fput item listname A reporter (results in or reports a value) that creates and reports a new list from
lput item listname an existing list by adding the specified value to the beginning or end of the
existing list, listname.
Example:
set mylist fput 20 mylist ;; if mylist initial contains 10,2,5,7; the new mylist
contains 20,10, 2, 5, 7
but-first listname A reporter that creates and reports a new list containing all but the first or the last
but-last listname items of an existing list.
Example:
set mylist but-last mylist ;; if mylist initially contained 20, 10, 2, 5, 7, the new
mylist contains 20, 10, 2, 5
one-of listname Randomly selects and reports items from a list.
n-of # listname Example:
show one-of mylist ;; if mylist contains 20, 10, 2, 5, then one of the numbers in
the list will be printed

Netlogo_Commands_week10.docx
AGENTSET PRIMITIVES
let agentsetname agentset-specification Creates a new local agentset variable, containing the specified agentset. A
local variable is one that exists only within the enclosing block of commands.
If you want to change the value afterwards, use set.
Example:
let redturtles turtles-here with [color = red] ;; creates an agentset,
redturtles, containing the agents on the same patch as the caller turtle that
are red.
set agentsetname agentset-specification Sets variable agentsetname to the given list of values.
Example:
set nturtles turtles-on neighbors4 ;;set the agentset nturtles to the set of
turtles standing on my neighboring four patches
ask agentset Executes a command block iteratively for each one of the agents in an
[…] agentset, after randomizing the order. Only the agents that are in the
agentset at the time the ask begins run the commands.
Example:
Ask nturtles […]
;; asks the turtles in the agentset nturtles to do the given commands
agentset with [reporter] Creates and reports a new agentset by filtering an existing agentset
according to a specific condition
Example:
show count myturtles with [color = red]
;; prints the number of red turtles in the agentset myturtles
[reporter] of agentset Reports a list of values (in a random order) created by iteratively evaluating a
reporter (results in or reports a value) for each member of an agentset
Example:
show [who] of myturtles
;; prints out the list of who numbers of all the turtles in the agentset myturtles
one-of agentset Reporters that return randomly selected agents from the agentset
n-of # agentset Example:
ask n-of 3 myturtles [ set color green ]
;; sets 3 turtles in the agents set myturtles to the color green

Netlogo_Commands_week10.docx
turtles-here 
 Reports an agentset containing all the turtles on the caller's patch (including the
<breeds>-here caller itself if it's a turtle).
Example:
crt 10
ask turtle 0 [ show count turtles-here ]
;; prints 10
turtles-on agent Reports an agentset containing all the turtles that are on the given patch or
turtles-on agentset 
 patches, or standing on the same patch as the given turtle or turtles. If the name of
<breeds>-on agent 
 a breed is substituted for "turtles", then only turtles of that breed are included.
<breeds>-on agentset Example:
ask turtles [
if not any? turtles-on patch-ahead 1
[ fd 1 ]
]
;; lets the turtles move forward one step if there are no turtles on the patch ahead

Netlogo_Commands_week10.docx
CS151L Fall 2013
Week 10: NetLogo Command Cheat Sheet
Command Command Description
hide-turtle The turtles makes itself invisible
show-turtle The turtle becomes visible again.

Netlogo_Commands_week11.docx

S-ar putea să vă placă și