State Machine Example

State Machine Example preview image

1 collaborator

Uri_dolphin3 Uri Wilensky (Author)

Tags

(This model has yet to be categorized with any tags)
Model group CCL | Visible to everyone | Changeable by group members (CCL)
Model was written in NetLogo 5.0.4 • Viewed 447 times • Downloaded 88 times • Run 0 times
Download the 'State Machine Example' modelDownload this modelEmbed this model

Do you have questions or comments about this model? Ask them here! (You'll first need to log in.)


WHAT IS IT?

This 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.)

THINGS TO NOTICE

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.

NETLOGO FEATURES

Note the use of the task primitive to create a command task to be run later with the run primitive.

RELATED MODELS

Termites

Comments and Questions

Please start the discussion about this model! (You'll first need to log in.)

Click to Run Model

turtles-own [
  next-task  ;; task 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
  clear-all
  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 next-task task search-for-chip
    set size 5  ;; easier to see
  ]
  reset-ticks
end 

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

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

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 next-task task find-new-pile ]
end 

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

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

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


; Public Domain:
; To the extent possible under law, Uri Wilensky has waived all
; copyright and related or neighboring rights to this model.

There are 10 versions of this model.

Uploaded by When Description Download
Uri Wilensky over 12 years ago Updated to NetLogo 5.0.4 Download this version
Uri Wilensky almost 13 years ago Updated version tag Download this version
Uri Wilensky over 13 years ago Updated to NetLogo 5.0 Download this version
Uri Wilensky about 15 years ago Updated from NetLogo 4.1 Download this version
Uri Wilensky about 15 years ago Updated from NetLogo 4.1 Download this version
Uri Wilensky about 15 years ago Updated from NetLogo 4.1 Download this version
Uri Wilensky about 15 years ago Updated from NetLogo 4.1 Download this version
Uri Wilensky about 15 years ago Model from NetLogo distribution Download this version
Uri Wilensky about 15 years ago State Machine Example Download this version
Uri Wilensky about 15 years ago State Machine Example Download this version

Attached files

File Type Description Last updated
State Machine Example.png preview Preview for 'State Machine Example' over 12 years ago, by Uri Wilensky Download

This model does not have any ancestors.

This model does not have any descendants.