Java StarLogo
2.0
english
`turtle`
to setup
  setxy random screen-width random screen-height
  setc red
  setchemical chemical + 2
end

to go
  uphill 
  rt random 40 
  lt random 40
  grid-step
  setchemical chemical + 2
end

to grid-step
  fd 1
  setxy round xcor round ycor      ;round your coordinates to the nearest whole number
end

;the next two procedures use local variables 
;in order to allow the turtles to move along the gradient of chemical
;more about local variables can be found in the StarLogo online documentation
to uphill
  let [:dir sniff]
  if :dir >= -45 [seth heading + :dir]
end

to sniff
  let [:dir -45
       :best-val chemical
       :best-dir -100]
  let [:current chemical-towards :dir 1]
  if :current > :best-val 
    [set :best-val :current
     set :best-dir :dir]
  set :dir :dir + 45
  set :current chemical-towards :dir 1
  if :current > :best-val 
    [set :best-val :current
     set :best-dir :dir]
  set :dir :dir + 45
  set :current chemical-towards :dir 1
  if :current > :best-val 
    [set :best-val :current
     set :best-dir :dir]
  output :best-dir
end


`observer`
patches-own [chemical]

to setup
  ca
  crt number 
  ask-patches [setchemical 0]
  ask-turtles [setup]
end

to go
  ask-turtles [go]
  diffuse chemical 1     ;disperse the chemical to surrounding patches
  ask-patches [set chemical chemical * 0.9
               scale-pc green chemical 0.1 3]
end


`information`
Slime 

This project is inspired by the aggregation behavior of slime-mold cells. It shows how creatures can aggregate into clusters using a  very decentralized strategy, without any "leader" involved. 

In this example, each creature drops a chemical pheromone (shown in  green). The creatures also "sniff" ahead, trying to follow the gradient of the chemical. Meanwhile, the patches diffuse and evaporate the chemical. Following these simple, decentralized rules, the creatures aggregate into clusters.   

Click the "setup" button to set up a collection of slime-mold cells. Click the "go" button to start the simulation. The "number" slider controls the number of slime mold cells. Changes in the slider do not have any effect until the next setup command.  

In the uphill procedure, the turtle "follows the gradient" of the  chemical. That is, it "sniffs" in three directions, then turns in  the direction where the chemical is strongest. The uphill procedure can be useful in many other projects. You might want to try variants of the uphill procedure, changing the number and  placement of "turtle sniffs."   

With 100 creatures, not much happens. The creatures wander around,  dropping chemical. But the chemical evaporates and diffuses fairly quickly, and the creatures do not aggregate. With 500 creatures,  following the exact same rules, the result is qualitatively different. When a few creatures happen (by chance) to wander near one another, they create a "puddle" of chemical. The creatures sniff the chemical and try to stay nearby. They then deposit more chemical in the puddle, so the puddle expands and attracts more creatures. 

This process is a good example of positive feedback: the more creatures, the larger the puddle, and the larger the  puddle, the more likely it is to attract more creatures.


`interface`
SLCanvas
top-left 14 255

SLButton
turtle-or-observer? observer
top-left 28 52
width-height 77 32
name "button1"
line-to-run "setup"
forever? false
button-number 2
show-name? false
whichip 7

SLButton
turtle-or-observer? observer
top-left 127 52
width-height 77 32
name "button2"
line-to-run "go"
forever? true
button-number 3
show-name? false
whichip 8

SLSlider
top-left 80 52
width-height 77 25
name "slider0"
variable "number"
min-value 0
max-value 700
current-value 500
slider-number 0
show-name? false


`turtle shapes`
[encoded data, 5330 characters]
qqqqqqqaoR8DO5wb3dkOFQAAFQ7Z

`template`
null

`settings`
patch-size 4
num-shapes 256
screen-half-width 37
screen-half-height 37
interface-window-xcor 262
interface-window-ycor 165
interface-window-size 573 336
output-window-xcor 0
output-window-ycor 0
output-window-width 300
output-window-height 219
info-window-xcor 345
info-window-ycor 301
info-window-width 500
info-window-height 419
control-center-xcor 0
control-center-ycor 85
control-center-width 285
control-center-height 415
turtle-command-center-height 150
observer-command-center-height 150

`string table`
H4sIAAAAAAAAAGNgYGAAABzfRCEEAAAAAAAABA==

`symbol table`
H4sIAAAAAAAAAGNgYGAAABzfRCEEAAAAAAAABA==

`double table`
H4sIAAAAAAAAAGNgYOBwYIAABwEIbf/mDAicdfBugPB3zgSBWQ56EPkDbhDxA5EQ
PgD/lQLMRAAAAAAAAEQ=

`list table`
H4sIAAAAAAAAAG2NUQ7AIAhDqzin8SL+7P7Xs11q4ocEfIVIAdAAROKzi9Gtk5iZ
ZpiFDMpC/fhv9q766Trn8vpH3KnkEO33Hve+m5bBAtCQq7SsAAAAAAAArA==

`bytecodes`
H4sIAAAAAAAAALVU207DMAx1eklXNtFKXcVuD+Mi8bQf4AnxJROsG5WmdWLdA/8I
fwQDJ7GTtvA6S62T2D4+tptCf/n8Xhcv1ao4LB/hQ8knAMQQHor6uMflGKzc4CPM
UtzhaxQAeGon6VyQxsMMbT4ub9knBIjIxqLtGJsoI1o2Fb7vnb0V5/+NIy4297Wk
mKHlITLm5INIPVoHhp8gfGFqcL4R6R5CSVdHYpxta4aWkZhhqhQhZr7WkDfo5aml
KDJw1FKw1CDzCd6jNsxcuRHR5Fp5BDrP2GHr/YT8AxdvWwGNVkCjFZzbh3jzVq4W
h7pQ5fU7EKqUAYhcpZ6i9rTWperwAORx/1put+h45QIj010jgVr+oKiKjLblTOzX
JPKptWuq+chMIfEoV4hT2JXrNZ5VDpppyiY0dV2fndwHqvffbgItOgHGDMjni2ys
T0gpJ6qxszE+4zA+027hh6asFk/Z8YnOzKH3D4e443NxJg64TwSNUsLl07Guq93D
fM43iy99S/Rh3/rqaxJ3/hLCueuEko2/no9lTeoEAAAAAATq

`observer world`
H4sIAAAAAAAAAGMQSMhPKk4tKkstUiguSSxJTXBggAMrBq6CnPyS8syU9NQSIJ+F
gS0nNS+9JAPI5mBgB8kVpOaBlXKDNSPJQgAjCAMA+OWZc2QAAAAAAABk

`patch world`
[encoded data, 20085 characters]
FwMAAAMXjw==

`turtle world`
[encoded data, 7800 characters]
OmxfgtJ/P5BvwZ8dec+7/6/++y8cELmnK24AAAAAbis=

