1. Defining variables
globals [time randomize-initial-state?] patches-own [state resource-level]Remember, agents have built-in variables: xcor, ycor, color, heading for turtles; pxcor, pycor, pcolor for patches.
2. Defining procedures
to update
command-1
command-2
command-3
end
3. Commands
-
You must specify whom you're commanding:
ask patches [
task-1
task-2
]
-
You can't ask anything of a turtle in the middle of an ask-patches block.
4. Things you can do
-
Change values of variables using set:
set state (random 5) ; assigns random integer 0 to 4. set time (time + 1) ; increments variable time set pcolor blue ; turns patch blue
-
Move turtles:
right random 360 ; changes turtle's heading a random number of degrees forward 2 ; moves turtle in direction of heading, two distance units
-
Create turtles, sprout turtles
5. How do you specify whom you're asking?
-
All patches or turtles: ask patches [ ]
-
One patch: ask patch 0 0 [ ]
-
One patch northeast of the one you're already asking: ask patch-at 1 -1 [ ]
-
Moore neighbors or von Neumann neighbors (patches only): ask neighbors [ ] or ask neighbors4 [ ]
-
Agents that have some property:
ask turtles with [TRUE/FALSE TEST] [TASK] ask patches with [TRUE/FALSE TEST] [TASK] ask breeds with [TRUE/FALSE TEST] [TASK] ask neighbors4 with [resource-level > 3] [set resource resource + (random 2) + 1]
6. Loops
while [TRUE/FALSE TEST] [
task-1
task-2
]
So, when do you use loops?
-
When you want the model to keep running until something happens.
-
When you want something to happen a set number of times. Use an index variable:
set i 0
while [i < 5][
task-1
task-2
set i (i + 1)
]
7. Conditionals
-
Make a procedure dependent on something.
-
How?
if (TRUE/FALSE TEST) [
task-1
task-2
]
or
ifelse (SOME TRUE/FALSE TEST) [
THINGS TO DO IF TRUE
]
[
THINGS TO DO IF FALSE
]
8. What are all these TRUE/FALSE TESTs?
-
Comparisons
-
=, !=, <, <=, etc.
-
T/F variables: mouse-down?, randomize-initial-states?
-
Trailing question-mark is a useful reminder.
-
Booleans (like all variables) initialize to zero, not false.
-
What can be compared with what?
-
Variables
-
reporters
-
count turtles
-
count patches with [pcolor = 4]
-
Mathematical expressions
-
count neighbors with [state = 2] mod 2
9. Interface stuff
-
Use buttons to trigger procedures, sliders to set variables, switches to set booleans.
-
Use monitors to track the value of global variables. The value can be a string.
-
Buttons can be regular or "forever." Forever buttons repeat their code until reclicked.
-
If you define a variable in the interface, don't define it in the code.
-
To edit, resize, or move an interface element, drag the cursor in, across border. Then move or resize, or click Edit.
-
The lattice resized in two ways or in combination:
-
Change number of cells.
-
Change size of cells.
-
I usually set cell number first, then resize using the cursor.
