Two States NetLogo Model

Produced for the book series "Artificial Intelligence";

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

powered by NetLogo

view/download model file: Two-States.nlogo

WHAT IS IT?

This model shows how to draw a simple two-state Finite State Automata (FSA) that represents the process of turning a light switch off or on.


THE INTERFACE

The Interface buttons are defined as follows:

- setup: Sets up the FSA. (Note: sometimes this needs to be pressed twice in a row in order that the links between nodes become curved rather than straight).
- go-once: Moves the agent from one state to the other.
- go: The agent repeatedly moves back and forth between the states.


HOW IT WORKS

A single agent performs the animation. To simulate movement, the agent moves itself to the location of the adjacent state, and then increases the thickness of the link it is crossing over.


HOW TO USE IT

This model can be used as a starting point for drawing more complicated FSAs which have many more states and many agents moving around them.


WHAT IS ITS PURPOSE?

Its purpose is to show how you can animate FSAs in NetLogo.


EXTENDING THE MODEL

Try adding more states and more agents.

Try adding a state with a self-link back to itself. Hint: One way of doing this is to use a hidden state.


CREDITS AND REFERENCES

To refer to this model in publications, please use:

Teahan, W. J. (2010). Two States NetLogo model.
Artificial Intelligence. Ventus Publishing Aps.


PROCEDURES

; Two States model.
;
; Creates a simple two state FSA and animates it.
;
; Copyright 2010 William John Teahan. All Rights Reserved.
;
breed [agents agent]
breed [points point]
directed-link-breed [curved-paths curved-path]

agents-own [location]  ;; holds a point

to setup
  clear-all ;; clear everything

  set-default-shape points "circle 2"

  create-points 1 [setxy -15  0 set label "Light off     "] ;; point 0
  create-points 1 [setxy  15  0 set label "Light on     "]  ;; point 1
        
  ask points [set size 5 set color blue] 

  ask point 0 [create-curved-path-to point 1]
  ask point 1 [create-curved-path-to point 0]

  ask curved-paths [set thickness 0.5]
  set-default-shape curved-paths "curved path"

  ask patches [
    ;; don't allow the viewer to see these patches; they are only for
    ;; displaying labels on separate lines
    set plabel-color brown
    if (pxcor = 11 and pycor =  8) [set plabel "Move light switch down"]
    if (pxcor = 11 and pycor = -8) [set plabel "Move light switch up"]
  ]
  
  create-agents 1 [
    set color lime
    set size 12
    set location point 0 ;; start at point 0
    set heading 55
    move-to location
  ]
end

to go
  ask links [ set thickness 0.5 ]
  ask agents [
    let new-location one-of [out-link-neighbors] of location
    ;; change the thickness of the link I will cross over
    ask [link-with new-location] of location [ set thickness 1.1 ]
    face new-location
    move-to new-location
    set location new-location
  ]
  tick
end
;
; Copyright 2010 by William John Teahan.  All rights reserved.
;
; Permission to use, modify or redistribute this model is hereby granted,
; provided that both of the following requirements are followed:
; a) this copyright notice is included.
; b) this model will not be redistributed for profit without permission
;    from William John Teahan.
; Contact William John Teahan for appropriate licenses for redistribution for
; profit.
;
; To refer to this model in publications, please use:
;
; Teahan, W. J. (2010).  Two States NetLogo model.
;   Artificial Intelligence. Ventus Publishing Aps.
;