Communication T-T Example 2 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: Communication-T-T-Example-2.nlogo

WHAT IS IT?

This code example is a simple demo of turtle-turtle communications. One turtle starts out with a message (the red turtle) and she spreads the message to other turtles.

The monitor keeps track of how many turtles have the message by reporting:

  count turtles with [color = red]

Note that if you put a procedure inside an "ask turtles [ ]" bracket, then everything in that procedure will be executed by all of the turtles.


HOW TO USE IT

Select the number of people you want in the simulation using the number-of-people slider, press the setup button, then press the go button.


THE INTERFACE

The buttons in the Interface are defined as follows:

- setup: This clears the variables, and resets the simulation creating persons at random locations whose number is specified by the number-of-people slider.

- go: This starts the simulation.

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

- count turtles with [color = red]: This counts the number of turtles who know the message.

- number-of-people: This is the number of persons who will be created next time the setup button is pressed.

- Percentage of people knowing message: This plots the percentage of the people who know the message against the number of ticks.


THINGS TO NOTICE

Notice that when the number of people is set to maximum, the message literally spreads like fire.


THINGS TO TRY

Try running the simulation with different number of people. Notice what happens in the plot when the number of people is small rather than large. It takes a lot longer to spread the message when the population size is small. Why is this (since the number of people do not know the message at the beginning is a lot less)?


EXTENDING THE MODEL

Try changing the model so multiple messages are being spread. Alternatively, try having two different breeds of people - those that spread messages, and those that don't.

Try a further variation where there is a random probability (as defined by a slider value) that the message may mutate when it is passed on. Set various conditions, such as how many times does the message need to be heard from different people, that determine when the message is finally "believed". How long does it now take before the message is spread?


CREDITS AND REFERENCES

This model has been modified by Bill Teahan from the original model provided by Uri Wilensky.


PROCEDURES

; This code written by Uri Wilensky (see copyright message below)
; has been slightly modified by Bill Teahan for the following
; book:
;
; Teahan, W. J. (2009). Artificial Intelligence. Ventus Publishing Aps.
;
;
turtles-own [
  message?      ;; true or false: has this turtle gotten the message yet?
]

globals
[
  people-who-know-message  ; The number of people who know the message i.e. are coloured red. 
]

to setup
  clear-all
  set people-who-know-message 0
  crt number-of-people [
    set shape "Person"
    set size 2
    set message? false
    setxy random-xcor random-ycor
  ]
  ask one-of turtles [ set message? true ]   ;; give the message to one of the turtles
  ask turtles [ recolor ]
end

to go
  ask turtles [ move ]
  ask turtles [ communicate ]
  ask turtles [ recolor ]

  set people-who-know-message count turtles with [color = red]
  if (people-who-know-message = number-of-people)
    [ user-message "Everybody now knows the message."
      stop ]

  update-plot

  tick
end

;; move randomly
to move  ;; turtle procedure
  fd random 4
  ;; turn a random amount between -40 and 40 degrees,
  ;; keeping the average turn at 0
  rt random 40
  lt random 40
end

;; the core procedure!
to communicate  ;; turtle procedure
  if any? other turtles-here with [message?]
    [ set message? true ]
end

; color turtles with message red, and those without message blue
to recolor  ;; turtle procedure
  ifelse message?
    [ set color red ]
    [ set color blue ]
end

; updates the plot
to update-plot
  plot 100 * (people-who-know-message / number-of-people)
end

; *** NetLogo 4.0.4 Code Example Copyright Notice ***
;
; (C) 2004 Uri Wilensky.  This code may be freely copied, distributed,
; altered, or otherwise used by anyone for any legal purpose.
;
; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
; "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
; LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
; A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT
; OWNERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
; SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
; LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
; DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
; THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
; (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
; OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
;
; *** End of NetLogo 4.0.4 Code Example Copyright Notice ***