New Zealand Birds 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: NZ-Birds.nlogo

WHAT IS IT?

This model shows how to create a decision tree in NetLogo and then animate it. The decision tree consists of a series of questions that can be used to help guess whether a bird a person is thinking of is a certain type of New Zealand bird or not (much in the same manner as the parlour game Twenty Questions).


THE INTERFACE

The Interface buttons are defined as follows:

- setup: This sets up and displays the entire decision tree.
- go once: A single question is asked based on the current point in the decision tree.
- go: The questioning process continues until an answer is found.


HOW IT WORKS

The states of the decision tree are represented by turtle agents (called "points" in the model) and these contain a question variable that is the question to be asked of the walker agent at a decision point. The walker agents are represented by turtle agents (called "agents" in the model) and these store the location variable which is set to the point in the decision tree which the agent can currently be found. Answers are stored as variables associated with the links between the nodes in the decision tree (i.e. the points). The walker agents move to the child node depending on the answer that the user responds with to the question.


HOW TO USE IT

Ask someone to think of a New Zealand bird, then run the model by pressing the setup followed by the go buttons to see if the model can guess which bird they were thinking of.


WHAT IS ITS PURPOSE?

Its purpose is to illustrate how you can create a decision tree in NetLogo and animate it. A secondary purpose is to show that decision-making of an agent is analogous to movement in an environment.


THINGS TO TRY

See what happens when the person you are playing with chooses a non-New Zealand bird to play with.

See what happens when the person you are playing with gives an incorrect answer or does not know the answer to the question.


EXTENDING THE MODEL

Try creating a different decision tree.

Try adding a "don't know" option to the possible answers to the questions.


CREDITS AND REFERENCES

To refer to this model in publications, please use:

Teahan, W. J. (2010). NZ Birds NetLogo model.
Artificial Intelligence. Ventus Publishing Aps.


PROCEDURES

; NZ Birds model.
;
; Creates a decision tree concerning birds of New Zealand.
;
; Copyright 2010 William John Teahan. All Rights Reserved.
;
breed [agents agent]
breed [points point]
directed-link-breed [straight-paths straight-path]

agents-own [location] ;; holds a point
points-own [question] ;; this is the question associated with the node
straight-paths-own [answer] ;; the transition paths define answers to the question
to setup
  clear-all ;; clear everything

  set-default-shape points "circle 2"

  create-points 1 [setxy   5  35 set question "Does the bird fly?"]   ;; point  0, level 0 (root)
  create-points 1 [setxy -25  25 set question "Is it a parrot?"]      ;; point  1, level 1
  create-points 1 [setxy  35  25 set question "Is the bird extinct?"] ;; point  2, level 1
  create-points 1 [setxy -40  15 set question "Alpine bird?"]         ;; point  3, level 2
  create-points 1 [setxy -10  15 set question "White throat?"]        ;; point  4, level 2
  create-points 1 [setxy  20  15 set question "Is it large?"]         ;; point  5, level 2
  create-points 1 [setxy  50  15 set question "Long beak?"]           ;; point  6, level 2
  create-points 1 [setxy -50   5 set question "Kea?"]                 ;; point  7, level 3
  create-points 1 [setxy -30   5 set question "Kaka?"]                ;; point  8, level 3
  create-points 1 [setxy -20   5 set question "Tui?"]                 ;; point  9, level 3
  create-points 1 [setxy   0   5 set question "Pukeko?"]              ;; point 10, level 3
  create-points 1 [setxy  10   5 set question "Moa?"]                 ;; point 11, level 3
  create-points 1 [setxy  30   5 set question "Huia?"]                ;; point 12, level 3
  create-points 1 [setxy  40   5 set question "Kiwi?"]                ;; point 13, level 3
  create-points 1 [setxy  60   5 set question "Weka?"]                ;; point 14, level 3
           
  ask patches [
    ;; don't allow the viewer to see these patches; they are only for
    ;; displaying labels on separate lines
    if (pxcor =   1 and pycor =  35) [set plabel [question] of point  0]
    if (pxcor = -29 and pycor =  25) [set plabel [question] of point  1]
    if (pxcor =  30 and pycor =  25) [set plabel [question] of point  2]
    if (pxcor = -44 and pycor =  15) [set plabel [question] of point  3]
    if (pxcor = -14 and pycor =  15) [set plabel [question] of point  4]
    if (pxcor =  16 and pycor =  15) [set plabel [question] of point  5]
    if (pxcor =  46 and pycor =  15) [set plabel [question] of point  6]
            
    if (pxcor = -48 and pycor =   0) [set plabel [question] of point  7]
    if (pxcor = -27 and pycor =   0) [set plabel [question] of point  8]
    if (pxcor = -18 and pycor =   0) [set plabel [question] of point  9]
    if (pxcor =   4 and pycor =   0) [set plabel [question] of point 10]
    if (pxcor =  12 and pycor =   0) [set plabel [question] of point 11]
    if (pxcor =  32 and pycor =   0) [set plabel [question] of point 12]
    if (pxcor =  42 and pycor =   0) [set plabel [question] of point 13]
    if (pxcor =  63 and pycor =   0) [set plabel [question] of point 14]
  ]
        
  ask points [set size 5 set color blue] 

  ask point  0 [create-straight-path-to point  1]
  ask point  0 [create-straight-path-to point  2]
  ask point  1 [create-straight-path-to point  3]
  ask point  1 [create-straight-path-to point  4]
  ask point  2 [create-straight-path-to point  5]
  ask point  2 [create-straight-path-to point  6]
  ask point  3 [create-straight-path-to point  7]
  ask point  3 [create-straight-path-to point  8]
  ask point  4 [create-straight-path-to point  9]
  ask point  4 [create-straight-path-to point 10]
  ask point  5 [create-straight-path-to point 11]
  ask point  5 [create-straight-path-to point 12]
  ask point  6 [create-straight-path-to point 13]
  ask point  6 [create-straight-path-to point 14]

  ask straight-paths [set label-color lime]  
  ask straight-path  0  1 [set answer "Yes" set label answer]
  ask straight-path  0  2 [set answer "No"  set label answer]
  ask straight-path  1  3 [set answer "Yes" set label answer]
  ask straight-path  1  4 [set answer "No"  set label answer]
  ask straight-path  2  5 [set answer "Yes" set label answer]
  ask straight-path  2  6 [set answer "No"  set label answer]
  ask straight-path  3  7 [set answer "Yes" set label answer]
  ask straight-path  3  8 [set answer "No"  set label answer]
  ask straight-path  4  9 [set answer "Yes" set label answer]
  ask straight-path  4 10 [set answer "No"  set label answer]
  ask straight-path  5 11 [set answer "Yes" set label answer]
  ask straight-path  5 12 [set answer "No"  set label answer]
  ask straight-path  6 13 [set answer "Yes" set label answer]
  ask straight-path  6 14 [set answer "No"  set label answer]
                
  ;; ask points [ create-path-with one-of other points ]
  ;; lay it out so links are not overlapping

  ask straight-paths [ set thickness 0.5 ]

  create-agents 1 [
    set color lime
    set size 5
    set location point 0 ;; start at point 0
    move-to location
  ]
  
  ask links [ set thickness 0.5 ]
end

to go
  ask agents [
    let neighbours [out-link-neighbors] of location
    let user-question [question] of location
    let answers [] ;; all the answers for current node
    let answers-points [] ;; the points where the agent should move to depending on the answer
    let parent-point location

    ask [out-link-neighbors] of location [
      let this-link in-link-from parent-point
      let this-answer [answer] of this-link
      set answers fput this-answer answers
      set answers-points fput self answers-points
    ]

    ifelse empty? answers
    ;; we are at a leaf node
      [ let user-answer user-one-of user-question ["No" "Yes"]
        ifelse user-answer = "Yes"
          [ user-message "Excellent! Let's try another bird."]
          [ user-message "Sorry, I cannot guess what the bird is."
            user-message "Let's try another bird."]

        set location point 0 ;; Go back to the beginning
        ask links [ set thickness 0.5 ] ;; reset transitions so it doesn't show path taken
      ]
  ;;else we are at an internal node  
      [ let user-answer user-one-of user-question answers
        let pos position user-answer answers
        let new-location item pos answers-points
        ;; change the thickness of the link I will cross over to show path taken
        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).  NZ Birds NetLogo model.
;   Artificial Intelligence. Ventus Publishing Aps.
;