changeDetector

changeDetector preview image

1 collaborator

Army_coat_pix R. Wade Schuette (Author)

Tags

(This model has yet to be categorized with any tags)
Visible to everyone | Changeable by the author
Model was written in NetLogo 6.2.0 • Viewed 228 times • Downloaded 10 times • Run 0 times
Download the 'changeDetector' 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?

Demo program showing how to detect the time the first change occurs in a summary variable. In this example a turtle wanders around eating food from patches and the program computes the total food remaining in the cyan patches. When that total changes, the program notes and remembers the time.

HOW IT WORKS

The patches are randomly assigned food and color. The only color we care about is cyan. The sum of food on cyan patches is computed. The turtle then wanders around eating all the food it finds, and as it does the sum of food on cyan patches is recomputed and if it just changed, that "tick" is assigned to a variable and saved. As the turtle continues to wander around eating food, further changes in the sum are noted, but we confirm that the FIRST time it changed is correctly no longer changing.

HOW TO USE IT

  • clear the "command center" with the button in its upper right corner.
  • click SETUP
  • click GO

THINGS TO NOTICE

  • in the listing in the command center, the first time the sum changes it is correctly noticed and grabbed. As the sum changes later, the right things happen.

THINGS TO TRY

EXTENDING THE MODEL

NETLOGO FEATURES

  • illustrates detecting the time a variable was first changed.

RELATED MODELS

CREDITS AND REFERENCES

Copyright 2021 R. Wade Schuette ( wade.schuette@gmail.com )

CC BY-NC-SA 3.0

This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike 3.0 License. To view a copy of this license, visit https://creativecommons.org/licenses/by-nc-sa/3.0/ or send a letter to Creative Commons, 559 Nathan Abbott Way, Stanford, California 94305, USA.

Comments and Questions

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

Click to Run Model

globals [  old-sum  sum-change-tick muncherID  running-old-sum]

patches-own [ food ]

to setup
  clear-all
  ask patches [ set food random 10  set pcolor gray]
  ask N-of 100 patches [ set pcolor cyan]
  make-muncher

  set old-sum sum [food] of patches with [pcolor = cyan]
  set sum-change-tick -1
  set running-old-sum old-sum

  say-status
  reset-ticks
end 

to go

  ;;  This demo program should stop shortly after the sum changes
  ;;   but not instantly afterwards so we can also confirm that as the sum
  ;;   continues to change later the "sum-change-tick" variable remains unchanged
  ;;   so we randomly run it 25 more ticks

  if ((sum-change-tick  > 0) and ( ticks > sum-change-tick + 25)) [
         print (word "All done!  The time the sum first changed was " sum-change-tick)
         stop
       ]


   ;; move
   ask turtle muncherID  [ set heading random 360 forward 1]

   ;; eat the food on that patch
   munch

   ;; analyze
   let new-sum sum [food] of patches with [pcolor = cyan]

   if ((old-sum != new-sum ) and ( sum-change-tick < 0)) [
        set sum-change-tick ticks
        print (word ".......................................sum changed first at tick = " sum-change-tick " !\n")
    ]

   if ( running-old-sum != new-sum) [ set running-old-sum new-sum print "------------------------------sum changed "]

   tick
end 



;;===========  helper routines

to say-status
    print (word "current old-sum is " old-sum ", sum = " sum [food] of patches with [pcolor = cyan])
end 

to make-muncher
  create-turtles 1 [
    setxy 0 0
    set size 3
    set shape "turtle"
    set color red
    set muncherID who
    pen-down ]
end 

to munch
     ask turtle muncherID [ ask patch-here [ set food 0]];
     let new-sum sum [food] of patches with [pcolor = cyan]

     ;; verbose way to confirm by inspection that this is doing what we want
     print ( word "At tick: " ticks  "  old-sum: " old-sum  " new-sum: "  new-sum  "  sum-change-tick: " sum-change-tick)
end 

There is only one version of this model, created almost 4 years ago by R. Wade Schuette.

Attached files

File Type Description Last updated
changeDetector.png preview Preview for 'changeDetector' almost 4 years ago, by R. Wade Schuette Download

This model does not have any ancestors.

This model does not have any descendants.