Java StarLogo
2.0
english
`turtle`
turtles-own [excitation countdown neighbors-light neighbor]

to setup
setc white
seth random 360
fd sqrt random 80
set excitation 1 + random threshhold
set countdown -1
end

to go1
ifelse (excitation >= threshhold) or (countdown = 0)
  [setc green]
  [setc white]
end

to go2
ifelse (excitation >= threshhold) or
       (trigger < neighbors-light)
  [if (excitation >= (threshhold - buffer))
     [set countdown threshhold - excitation]
   setexcitation 1]
  [set excitation excitation + 1]
if countdown >= 0
  [set countdown countdown - 1]
end

to tally-neighbors-light
   set neighbor 1
   set neighbors-light 0
   repeat (length flashing)
      [let [:partner item neighbor flashing]
       set neighbors-light neighbors-light +
         (100 / (1 + ((distance (xcor-of :partner) (ycor-of :partner)) ^ 2)))
       set neighbor neighbor + 1]
end


`observer`
globals [current flashing]

to setup
ca
crt number
ask-turtles [setup]
end

to go
ask-turtles [go1]
tally-light
;; We do this with two ask-turtles to wait for all fireflies
;; to finish go1 before starting go2.
ask-turtles [go2]
end

to tally-light
; build a list of green turtles into "flashing"
set flashing []
ask-turtles [if (color = green) [set flashing fput who flashing]]
; each turtle computes its illumination from those that are flashing
ask-turtles [tally-neighbors-light]
end


`information`
WHAT IS IT? -----------  
This program simulates the self-organized synchronization of firefly flashing.  In the region from India east to the Philippines and New Guinea, enormous aggregations of fireflies gather in trees and flash in near-perfect synchrony.  While different species have slightly different methods for flashing in rhythm, the behavior that is modeled here is governed by the following rules:       
- each firefly has an intrinsic flashing frequency, and when left alone it will flash at periodic intervals.       
- the flashes are timed by the progressive excitation of a chemical within each firefly; the excitation increases until it reaches a  certain threshold, at which point a flash is emitted and the excitation is reset to zero.       
- if a firefly senses a certain amount of luminescence from its neighbors, it will reset its excitation to zero in order to flash simultaneously with those neighbors in the future; however, if the excitation is close enough to the flashing threshold, the flash has already been started and will proceed as planned even though the excitation is reset to zero.  (The signal to flash is initiated when the excitation is 80% of the way to the threshold value.)   

HOW TO USE IT -------------  
The SETUP button arranges the fireflies randomly within a circle, assigns them random excitation levels, configures the plot to monitor the flashing, and displays a color key. The GO button cycles through time, executing the rules stated above. The NUMBER slider controls the number of fireflies in the simulation. The THRESHOLD slider sets the excitation threshold at which point a firefly will flash and reset its excitation to zero. Because excitation is incremented once per time step, this slider also represents the number of time steps between flashes of an (uninfluenced) firefly. The TRIGGER slider controls the amount of luminescence required for a firefly to reset its excitation prematurely. Each firefly's flash is counted as 100 units of light, and the program assumes that perceived light decreases with the inverse square of the distance from source. The BUFFER slider controls how many time steps are necessary for the flashing signal to evolve and terminate in a flash; if a firefly is triggered to reset its excitation when the excitation is within "buffer" of the threshold, the flash will proceed as planned despite the resetting.   

THINGS TO NOTICE ----------------  
The cluster of fireflies will often establish two phases of flashing before synchronizing completely; this phenomenon is also observed in the field. If there is a rift of empty space between the two groups (due to a random density differnece), they might never completely coordinate. However, when they do move to complete synchronization, the group flashing a step later often prevails because its flashes have been started by the time it perceives the other group's luminescence. Each of the slider variables affects the simulation; generally, faster synchronization is favored by high numbers, low thresholds, intermediate triggers, and low buffers.


`interface`
SLCanvas
top-left 11 208

SLButton
turtle-or-observer? observer
top-left 76 13
width-height 67 30
name " go"
line-to-run "go wait 0.05"
forever? true
button-number 3
show-name? true
whichip 7

SLSlider
top-left 107 105
width-height 90 25
name "slider2"
variable "trigger"
min-value 0
max-value 40
current-value 20
slider-number 2
show-name? false

SLSlider
top-left 31 103
width-height 90 25
name "slider0"
variable "threshhold"
min-value 0
max-value 50
current-value 31
slider-number 0
show-name? false

SLSlider
top-left 144 106
width-height 90 25
name "slider3"
variable "buffer"
min-value 0
max-value 5
current-value 2
slider-number 3
show-name? false

SLSlider
top-left 70 104
width-height 90 25
name "slider1"
variable "number"
min-value 0
max-value 99
current-value 50
slider-number 1
show-name? false

SLButton
turtle-or-observer? observer
top-left 32 13
width-height 65 30
name "button1"
line-to-run "setup"
forever? false
button-number 2
show-name? false
whichip 8


`turtle shapes`
[encoded data, 5200 characters]
DhUAABUO2Q==

`template`
null

`settings`
patch-size 8
num-shapes 256
screen-half-width 23
screen-half-height 23
interface-window-xcor 403
interface-window-ycor 145
interface-window-size 601 412
output-window-xcor 0
output-window-ycor 0
output-window-width 300
output-window-height 219
info-window-xcor 204
info-window-ycor 199
info-window-width 500
info-window-height 419
control-center-xcor 42
control-center-ycor 132
control-center-width 345
control-center-height 465
turtle-command-center-height 140
observer-command-center-height 133

`string table`
H4sIAAAAAAAAAGNgYGAAABzfRCEEAAAAAAAABA==

`symbol table`
H4sIAAAAAAAAAGNgYGAAABzfRCEEAAAAAAAABA==

`double table`
H4sIAAAAAAAAADXOMQ5AQBRF0V9agEIxhYhiIgqCMIKZ3qZsgx1YhS1ZgvCv25zq
JU9ETMjk67rVINhhijNG2OKo+n+/oMMCPU6YYI4WK4xxwAZL7LFGg+umf8797XgA
x8Sog+QAAAAAAADk

`list table`
H4sIAAAAAAAAAE1NQQ6AMAgrkC3uaOIu+ikf4cH/v0AYECGBAm0BwABApAmP1gBh
3WuetmfXWGw6DwK9ije5bxn51/XcJR93jDsKx5E5S7k16u3SV8x/e/Q9vBy8BNr8
rJ6mlrn+hk7SC8frAwDJBAQQAQAAAAABEA==

`bytecodes`
H4sIAAAAAAAAAJVU204bMRCd2Ws2t/UGQlClQlTy2krlkaeKX+gPhEAUKkUsapYH
/pF+Uzu2x55ZCg9YWnv2zPH4+Hi9MFpvnrvtbXu3Pax/wIttfwCggvyw7Z4eKTQQ
2xd60Ie4ou40A0jsWw5Q2jEVrg0vCoDaJom2a6lfSL43L/t/XhlJb/Dzd9ZBGHU3
+/3z1/2v3X1HmZUQKx6Ruybhog3gfMp4AmgWjFeEHzOeAjYI+DnhdxLcpH7xOhER
PZGFLNgTmYi3ZyKPfYW5iRLR0JT5qZJW0NMQNo6yghx7Hj4fatFKM+S44LH0mJOR
Qbprv78pAmdDEeEKfxIRMZc6r9Cc874z8cZuIoVXeC54nbKI3Iq4JOTnx0SsQEQQ
D82JEnIursQauZpTcD30Nd2cUXRIeAOuHVysvHvh0B0W6o8kl2h8rDRM1OlMVa1w
irX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