mercoledì 17 febbraio 2010



globals [
percent-similar ;; on the average, what percent of a turtle's neighbors
;; are the same color as that turtle?
percent-unhappy ;; what percent of the turtles are unhappy?
]

turtles-own [
happy? ;; for each turtle, indicates whether at least %-similar-wanted percent of
;; that turtles' neighbors are the same color as the turtle
similar-nearby ;; how many neighboring patches have a turtle with my color?
other-nearby ;; how many have a turtle of another color?
total-nearby ;; sum of previous two variables: morale delle 2 varibili: chi ti dˆ fastidio
]


to setup
clear-all
if number > count patches
[ user-message (word "This pond only has room for " count patches " turtles.")
stop ]

;; create turtles on random patches.
ask n-of number patches
[ sprout 1
[ set color red ] ]
;; turn half the turtles green
ask n-of (number / 2) turtles
[ set color green ]
update-variables
do-plots
end

to go
if all? turtles [happy?] [ stop ]
move-unhappy-turtles
update-variables
tick
do-plots
end

to move-unhappy-turtles
ask turtles with [ not happy? ]
[ find-new-spot ]
end

to find-new-spot
rt random-float 360 ;; girano a caso a 360 gradi
fd random-float 10 ;; si muovono random di 10
if any? other turtles-here
[ find-new-spot ] ;; keep going until we find an unoccupied patch. procedura ricorsiva che richiama se stessa.
move-to patch-here ;; move to center of patch
end

to update-variables
update-turtles
update-globals
end

to update-turtles
ask turtles [
;; in next two lines, we use "neighbors" to test the eight patches
;; surrounding the current patch
set similar-nearby count (turtles-on neighbors)
with [color = [color] of myself]
set other-nearby count (turtles-on neighbors)
with [color != [color] of myself]
set total-nearby similar-nearby + other-nearby
set happy? similar-nearby >= ( %-similar-wanted * total-nearby / 100 )
]
end

to update-globals
let similar-neighbors sum [similar-nearby] of turtles
let total-neighbors sum [total-nearby] of turtles
set percent-similar (similar-neighbors / total-neighbors) * 100
set percent-unhappy (count turtles with [not happy?]) / (count turtles) * 100
end

to do-plots
set-current-plot "Percent Similar"
plot percent-similar
set-current-plot "Percent Unhappy"
plot percent-unhappy
end

0 commenti: