Most NetLogo programs follow a simple framework.
The framework begins with a section of declarations of agent attributes and global variables:
;++++++++++++++++++++++++++++
; initialization procedures
;++++++++++++++++++++++++++++
patches-own []
turtles-own []
globals [halt]
Note that the term "Agent" collectively refers to patches and turtles.
Note that a semicolon marks the beginning of a comment and is ignored by NetLogo.
Any agent can stop the program by setting halt to true:
set halt true
Next come the main procedures:
;++++++++++++++++++++++++++++
; main procedures
;++++++++++++++++++++++++++++
;initialize all patches and turtles
to init
ca ; clear all patches
& kill all turtles
crt 10 ; create 10 turtles
ask patches [init-patch]
ask turtles [init-turtle]
init-globals
end
; start button resumes & pauses iterations of this procedure
to update
if halt [stop] ; stop the program if halt = true
tick ; advance clock by 1
second
ask patches [update-patch]
ask turtles [update-turtle]
update-globals
end
Back on the interface tab we will create two buttons, The first one will call the init procedure when it is clicked:
The second one will call the update procedure repeatedly when it is clicked. When it is clicked again, it will stop calling update. When it is clicked for a third time it will resume repeatedly calling update, and so on. This behavior is accomplished by checking the forever box in the Button panel:
After calling ca (clear-all) and creating some turtles (crt), the init procedure asks each patch to execute the init-patch procedure and every turtle to execute the init-turtle procedure. These procedures will initialize the properties of the patches and turtles, for example, we could set the color of each patch or the position of each turtle.
The update procedure calls tick, this advances the clock. It isn't necessary but is useful in many programs. Update asks each patch to execute the update-patch procedure and each turtle to execute the update-turtle procedure. Remember, these procedures will be called again and again until the start button is clicked.
Finally, the procedures for initiating individual patches and turtles are defined. The actual instructions shown below are just for show.
;++++++++++++++++++++++++++++
; initialization procedures
;++++++++++++++++++++++++++++
to init-patch
set pcolor green
end
to init-turtle
set color yellow
pd ; pen down
end
to init-globals
set halt false
end
;++++++++++++++++++++++++++++
; update procedures
;++++++++++++++++++++++++++++
to update-patch
end
to update-turtle
fd 1
rt 1
end
to update-globals
end
Give each patch an altitude attribute. Create a landscape so that the altitude of a patch is
500 – (pxcor * pxco + pycor *& pycor)
In other words, the closer to the origin (0, 0), the higher.
Create 50 turtles positioned randomly throughout the landscape. Each turtle moves to the neighboring patch with the highest altitude. The program ends when all turtles reach the summit. (Hint: try using the uphill procedure.)