State Machine Example 2 NetLogo Model

Produced for the book series "Exercises for Artificial Intelligence";

Author: W. J. Teahan; Publisher: Ventus Publishing Aps, Denmark.

powered by NetLogo

view/download model file: State-Machine-Example-2.nlogo


This model shows how to make your turtles into "state machines" using a turtle variable and the RUN command. A state machine consists of a collection of states with a different action associated with each state.

The model itself is an alternate version of the Termites model located in the Biology section of Sample Models. Termites is an unusual model in that it uses a turtle forever button; each turtle runs its code, out of synch with all the others. This version of the model uses an observer forever button instead. The state machine technique is necessary in order to code this model using an observer forever button. (For more on turtle forever buttons see the Programming Guide in the NetLogo User Manual.)

This model has been slightly modified by Bill Teahan from the State Machine Example model in NetLogo's Models Library. It has added a plot to graph the growth in the number of piles, or equivalently, the number of spaces, during the simulation. This is in order to estimate the progress of the self-organisation of the system as a whole.


The purpose of the modified model is to ascertain whether a function based on global features can be used to measure the state of the simulation (i.e. how self-organised the system is). If the graph of the function shows a smooth upward curve, then it might be useful (for example) as a fitness function for an evolutionary algorithm in order to evolve the behaviour of the termites automatically rather than being explicitly programmed.


First select a function to plot using the pileness-measure chooser. Then press the setup button to reset the simulation, then press the go button.


The buttons in the Interface are defined as follows:

- setup: This resets the simulation so that the wood chips and termites are placed randomly.

- go: This runs the simulation.

The sliders, chooser, monitor and plot in the Interface are defined as follows:

- number: This sets the number of termites that will be created at the start of the simulation.

- density: This sets the percentage of patches that will be created as wood chips at the start of the simulation.

- pileness-measure: These are the functions that can be used to measure the self-organisation of the system i.e. how much of the wood chips have been organised into piles. There are two possible functions:
"Number of crowded spaces": This counts the number of patches which have more than 7 yellow-coloured neighbours.
"Number of empty spaces": This counts the number of patches which have more than 7 black-coloured neighbours.

- pileness: This reports the value of the pileness measure as computed by the function chosen by the pileness-measure chooser.

- pileness plot: This plots the pileness value for each tick of the simulation.


Compare the code to the code in the main Termites model. Which way of coding the model do you prefer...?

Note that the behavior of this version of the model isn't absolutely identical to the behavior of the original model. In this version, note that all the turtles begin white, then all turn red, then all turn back to white, and so forth. Gradually the turtles drift out of synch with each other until the coordinated flashing no longer happens. The main model exhibits the same effect, but in the main model, the turtles get out of synch with each other somewhat faster. That's because the way time passes in the two models is a little different. In this version, a turtle's "turn" consists of moving a step or changing tasks. In the original version, a turtle's "turn" is smaller, and consists only of executing a single NetLogo command.


Try devising your own pileness measures, or alternative functions to measure the self-organisation of the system.


Note the use of the RUN command to make each termite run a different procedure, depending on what task that termites is in the middle of. Since NetLogo 4.0, the RUN command is fast when you use it over and over again on the same strings.


See the Termites model in the Models Library.


This model was modified by William John Teahan.

To refer to this model in publications, please use:

State Machine Example 2 NetLogo model.
Teahan, W. J. (2010). Artificial Intelligence. Ventus Publishing Aps.


turtles-own [
  task       ;; procedure name (a string) the turtle will run during this tick
  steps      ;; ...unless this number is greater than zero, in which
             ;; case this tick, the turtle just moves forward 1

to setup
  set-default-shape turtles "bug"
  ;; randomly distribute wood chips
  ask patches [
    if random-float 100 < density
      [ set pcolor yellow ]
  ;; randomly distribute termites
  crt number [
    set color white
    setxy random-xcor random-ycor
    set task "search-for-chip"
    set size 5  ;; easier to see

to go
  ask turtles
    [ ifelse steps > 0
        [ set steps steps - 1 ]
        [ run task
          wiggle ]
      fd 1 ]


to do-plot
  plot pileness

to wiggle  ;; turtle procedure
  rt random 50
  lt random 50

to search-for-chip   ;; turtle procedure -- "picks up chip" by turning orange
  if pcolor = yellow
    [ set pcolor black
      set color orange
      set steps 20
      set task "find-new-pile" ]

to find-new-pile  ;; turtle procedure -- look for yellow patches
  if pcolor = yellow
    [ set task "put-down-chip" ]

to put-down-chip  ;; turtle procedure -- finds empty spot & drops chip
  if pcolor = black
   [ set pcolor yellow
     set color white
     set steps 20
     set task "get-away" ]

to get-away  ;; turtle procedure -- get out of yellow pile
  if pcolor = black
    [ set task "search-for-chip" ]

to-report pileness
  if (pileness-measure = "Number of crowded spaces")
    [ report count patches with [count neighbors with [pcolor = yellow] > 7] ]

  if (pileness-measure = "Number of empty spaces")
    [ report count patches with [count neighbors with [pcolor = black] > 7] ]
  report 0   

; Copyright 2007 Uri Wilensky. This code may be freely copied, distributed,
; altered, or otherwise used by anyone for any legal purpose.