The Basic Agent Model (BAM1) can be found here:
Open this model, use the File/SaveAs option to rename it, then customize it by filling in the missing procedure definitions.
Procedures in the basic agent model are divided into three groups: initializers, updaters, and helpers.
There are four initializers:
init-model
init-patch
init-turtle
init-globals
The init-model procedure is called when the setup button is pressed. It clears the world, creates some turtles (or not), then calls the other three initializers:
to init-model
ca
; create turtles here
init-globals
ask patches[init-patch]
ask turtles [init-turtle]
end
For example, a model that had 10 turtles at the start of a simulation would modify this procedure as follows:
to init-model
ca
;
create turtles here
crt 10
init-globals
ask patches[init-patch]
ask turtles [init-turtle]
end
The init-patch procedure initializes any patch attributes, the init-turtle procedure initializes any turtle attributes, and the init-globals procedure initializes any global variables. There are no attributes, so the first two procedures are empty. It is the programmers job to fill these procedures with code:
to init-patch
end
to init-turtle
end
There is a single global defined in the basic model:
globals [halt?]
Any agent desiring to halt the simulation only needs to set this variable to true. Initially it must be set to false:
to init-globals
set halt? false
end
For example, if the 10 turtles created above needed to be red, distributed to random locations on the screen and have their pens down, then the init-turtle procedure would need to be filled in like this:
to init-turtle
set
color red
setxy random-xcor random-ycor
pd
end
Note: we can use the setxy command to set the xcor and
ycor of a turtle in one step.
There are five updaters:
update-model
update-patch
update-turtle
update-globals
update-plots
The update-model procedure is called repeatedly when the go button is pressed. It calls the other four updaters:
to update-model
if halt? [stop]
tick
ask patches [update-patch]
ask turtles [update-turtle]
update-globals
update-plots
end
Notice that it initially checks the halt? global and stops if this has been set to true by some agent. Since this procedure is tied to the go button, this will halt the simulation.
Each time the update-model procedure is called is one update cycle. The tick procedure automatically advances the world clock. Therefore it keeps track of the number of update cycles.
The update-turtle procedure updates turtle attributes, the update-patch procedure updates patch attributes, the update-globals procedure updates global variables. The update-plots procedure updates any plots (these are special controls on the interface.)
These update procedures are empty. For example:
to update-globals
end
Programmers must customize the updaters. For example:
to update-globals
set halt? (0 = count turtles)
end
to update-turtle
move
eat
mate
end
Helpers are any additional procedures programmers must define that are needed by updaters and initializers, such as move, eat, and die in the example above.
Formally, the lifecycle of the basic model can be viewed as a UML statechart machine:
Initially, the model is in the ready state. Clicking on the update button causes it to transition to the “executing init-model” state. When finished, the model transitions back to the ready state.
Clicking on the go button causes the model to transition into the “executing update-model” state. While in this state, clicking on the go button causes the model to transition back to the ready state.
When the update-model procedure finishes, the model transitions to the final state if the halt? flag has been set, otherwise the update-model procedure is executed again.
The update-model and init-model are statechart states. In other words, each contains a mini statechart machine. For example, here is the statechart machine contained in the update-model state: