📄 ---model7-grafiek.nlogo
字号:
;;globalvars: no-things, ;; ;; number of turtles;; p-communicators ;; the chance that a turtle will be a comunicator;; p-knowhow ;; the chance that a turtle will have know-how;; knowledge-transfer ;; for every item, the probability that the parent will teach the childglobals [ extra-list regular no lifespan p-communicators step-timer ;born ;died show-knowledge max-en no-turtles p-knowhow no-things knowledge-transfer different? ;silent-reg ;silent-spec ;talker-reg ;talker-spec ];;extra-list - holds the values for the different types of credit;;regular - holds the value for the regular type of credit, accesible to all;;no - the number of special types of credit in the environment;;lifespan - the maximum turtle age;;step-timer - global time ;;born - number of turtles born this round;;died - number of turtles died this round;;max-en - maximum energy, the turtles cannot keep collecting credit forever patches-own [ here-list ] ;;hold a value describing the type of credit, -1 of emptyturtles-own [ age ;bd ;;this is a sloppy variable, the birthdate. energy knowhow ;hatchlings ;parent ;id ] breeds [ talker silent ]to setup ca setup-globals setup-patches setup-agents setup-plot setup-fileendto setup-globalsset lifespan 50 ;;;maximum age, in stepsset no-things 7set no (no-things - 1) ;;;this is more intuitive set regular 5 ;;;default credit, this could also be user defined.set different? true;;; sets the credit a turtle gets for each unit, if different? is off, every credit unit has the same energy value;ifelse different? [set extra-list n-values no [ regular * 2 ] ] [ set extra-list n-values no [regular] ] set extra-list (n-values no [ (regular * 2) ])set extra-list (fput regular extra-list)set step-timer 0set max-en 200;set born 0;set died 0set show-knowledge 0set no-turtles 1.5 * (((1265 * replace-rate-regular) / 16)* (field / 40401))set p-knowhow 0.05set knowledge-transfer 0;set silent-reg 0;set silent-spec 0;set talker-reg 0;set talker-spec 0set p-communicators 0endto setup-filefile-open "runs.txt"file-print (word "number of things:" " " no-things)file-print (word "replace rate (regular, special)" ": " replace-rate-regular ", " replace-rate-special) file-print (word "probability of a turtle being a communicator" ": " p-communicators)file-print (word "probability of a turtle knowing any one of n things" ": " p-knowhow)file-print (word "initial number of turtles" ": " no-turtles)file-print (word "value of regular credit" ": " regular)file-print (word "values of special credit" ": " extra-list)file-print (word "number of talker-turtles" ": " (count talker))file-print "end of initial values"file-print "turtle id, age, birthdate, breed, knowhow, parent, offspring,"endto close-filefile-closeend;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;patches;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;to setup-patches;oorspronkelijk aantal herhalingen: 27ask patches [ set here-list (n-values no-things [ 0 ]) ;;all patches are empty repeat 27 [fill-patches-regular fill-patches-special] update-patches ] endto fill-patches-regularif (random-int-or-float 1000 < replace-rate-regular) [set here-list (n-values no-things [ 0 ]) ;;empty whatever is on the patch set here-list (add-credit 0 here-list)] ;; add regular creditendto fill-patches-specialif (add-up here-list) = 0 ;; if the patch is empty [ if (no != 0 ) and ((random-int-or-float 1000 ) < replace-rate-special) [ set here-list (n-values no-things [ 0 ]) ;;empty whatever is on the patch set here-list (add-credit ((random no) + 1) here-list)] ] ;add one of the food typesendto update-patchesset pcolor (40 + (first here-list * 3) + (add-up (butfirst here-list) * 5)) end;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;turtles;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;to setup-agents set-default-shape talker "loud" set-default-shape silent "silent" crt no-turtles ;; create given number of turtles ask turtles [set age random lifespan ;; set age to hatchlings (fput kind hatchlingsrandom number < lifespan setxy (random-int-or-float screen-size-x) ;; randomize the turtle locations (random-int-or-float screen-size-y) set energy (random-normal 18 0.9 ) init-vars set color 97 ] endto init-vars ;; roll dice for breed ifelse ((random 100) < (p-communicators * 100)) [ set breed talker ] [ set breed silent ] ;; roll dice for every knowledge-slot set knowhow n-values no [round (((random 100) / 100) - (0.50 - p-knowhow))] ;set hatchlings [] ;set parent "no-one" ;set bd 0 ;set id ((remove "turtle " (word self)) + bd)endto go ask turtles [ if energy < max-en [take-credit]] ask patches [ update-patches ] ask turtles [ if (random energy) > 30 [offspring ] if (show-knowledge != 0) [ update-looks-knowhow ] set energy (energy - 1) levi-flight set age (age + 1) live-or-die if (breed = talker) [ communicate ] ] ask patches [ fill-patches-special fill-patches-regular update-patches ] update-plot if (count turtles = 0) [(show (word "turtles became extinct at:" step-timer)) stop] ; if (step-timer = 1200) [(show "time's up!") stop] reset-globalsendto take-credit; here-list is taken to refer to a value of patch-here. This is good if (first here-list = 1) ;first check for regular food [set here-list (n-values no-things [ 0 ]) set energy (energy + regular) ;ifelse breed = silent ; [set silent-reg (silent-reg + 1)] [set talker-reg (talker-reg + 1)] ] if (add-up (map [?1 * ?2] knowhow ( butfirst here-list)) = 1) ;check for the spec. stuff [set here-list (n-values no-things [ 0 ]) set energy (energy + (regular * 2)) ; ifelse breed = silent ; [set silent-spec (silent-spec + 1)] [set talker-spec (talker-spec + 1)] ]endto communicate locals [n] if (no != 0) [ ;;pick an item that is 1: first make a list of all the items that are 1 ;;then randomly pick 1 to tell neighbours if (add-up knowhow) > 0 [set n (random-one-of (non-zero knowhow 0)) ask turtles in-radius 1 [ set knowhow (replace-item n knowhow 1)]] ] ;;and i know there must be an easier way.....endto live-or-die if (energy < 0) or (age > lifespan) [ ;(file-print (word id ", " age ", " bd ", " breed ", " knowhow ", " parent ", " hatchlings)) ;set died (died + 1) die ]endto offspringlocals [kind] hatch 1 [set kind (remove "turtle " (word self)) set age 0 ;set hatchlings [] set energy (energy * 0.2) ;;talker kids ( of talker parents) get some part of their knowledge ifelse ( breed = talker) [set knowhow (map [tweak ? knowledge-transfer] knowhow) ;; first get x% of parents know-how set knowhow (map [max (list ?1 ?2) ] knowhow (n-values no [round (((random 100) / 100) - (0.50 - p-knowhow))]))] ;; then see if you are born with something of your own [set knowhow n-values no [round (((random 100) / 100) - (0.50 - p-knowhow))]] ;set parent (remove "turtle " (word myself)) ;set bd step-timer ;set id ((remove "turtle " (word self)) + bd) ] set energy (energy * 0.8) ; keep the overall energy the same ;set hatchlings (fput kind hatchlings) ;set born (born + 1) endto reset-globalsset step-timer (step-timer + 1);set born 0;set died 0;set silent-reg 0;set silent-spec 0;set talker-reg 0;set talker-spec 0end;;;;;;;;;; DISPLAY ;;;;;;;;;;;;;;;;;;;;;;;; every time the flip-button is pressed;; the value of show-knowledge is incremented by one untill its greater then the ;; number of different things in the environment, then it is set back to 1;; the value of show-knowledge corresponds to the turtles' knowledge-slotsto flip-colorif (no != 0) [ set show-knowledge (show-knowledge + 1) if show-knowledge > no [set show-knowledge 1] ask turtles [ update-looks-knowhow ] ]endto update-looks-knowhowset color (((item (show-knowledge - 1) knowhow) * 10 * show-knowledge) + 14 )end;to update-patches-credit;set color (((only-one (item (show-knowledge - 1) (butfirst (herelist)))) * 10 * show-knowledge) + 14 );endto color-offset show-knowledge 0set color 97 end;;;;;;;;;;HOW TO MOVE;;;;;;;;;;;;;;;;;;;;;;;;;;;;to levi-flightlocals [n]rt random-int-or-float 360set n (1 - (random-float 1))fd (round (( 0.7 / n) ^ 0.3))end;;;;;;;;;;;;;;;AUXILLARY REPORTERS AND PROCEDURES;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;takes 2 lists and outputs one list that is 'adjusted';;the list describing the credit available at a certain patch is ;;adjusted according to the list describing turtle knowhowto-report adjust-here-list [hrlst knwhw]if hrlst = [] [report []]ifelse (first knwhw = 1) [ report (fput 0 (adjust-here-list (butfirst hrlst) (butfirst knwhw))) ] [ report (fput (first hrlst) (adjust-here-list (butfirst hrlst) (butfirst knwhw))) ]end;;adds 1 to item n of list lto-report add-credit [n l]report (replace-item n l ((item n l) + 1 ) )end;;changes a 1 into a 0, with probability 1 - p. to-report tweak [k p] ifelse ((random 100) < (p * 100)) [report k] [ report 0] end;;takes a list and returns the list of non-zero item-numbers;n is the counterto-report non-zero [l n]if l = [] [report []]ifelse (first l = 1) [report fput n (non-zero butfirst l (n + 1))] [report non-zero butfirst l (n + 1)]end;;does exactely the same as sum, only it can handle an empty list.to-report add-up [lijst]ifelse (lijst = []) [report 0 ][ report reduce [ ?1 + ?2 ] lijst ]end;;sum over lots of liststo-report sum-list [lijst-van-lijsten]if (lijst-van-lijsten = []) [report []]report sum2 (first lijst-van-lijsten) (sum-list (but-first lijst-van-lijsten))end ;;sum over 2 liststo-report sum2 [list1 list2]if (list2 = []) [report list1]report (map [?1 + ?2] list1 list2)end to-report field report (screen-size-x * screen-size-y)end;reports 1 if n > 0to-report only-one [n]ifelse (n > 0) [report 1] [report 0]end;returns some information on the knowledge spread and the amount of foodto show-valueslocals [n t]set n 0set t ((count turtles) / 100)print (word "time: " step-timer ", agents: " round (t * 100))repeat no [ print ( word count turtles with [ item n knowhow = 1] " agents know item " (n + 1) " (" precision ((count turtles with [ item n knowhow = 1]) / t) 3 "%)") print ( word count turtles with [ add-up knowhow = (n + 1) ] " agents know " (n + 1) " items" " (" precision ((count turtles with [ add-up knowhow = (n + 1) ]) / t) 3 "%)") print ( word count patches with [ item (n + 1) here-list = 1 ] "patches have food type " (n + 1)) set n (n + 1) ]print (word (count patches with [first here-list = 1]) " patches with regular food")print (word (count turtles with [add-up knowhow = 0]) " agents know nothing")print (word "total food = " (((count patches with [first here-list = 1]) * 5) + ((count patches with [add-up butfirst here-list = 1]) * 10)))print (word "patches with regular food " (count patches with [first here-list = 1]) " (" precision (((count patches with [first here-list = 1]) / field) * 100) 2 "%)")print (word "patches with special food " (count patches with [add-up butfirst here-list = 1]) " (" precision (((count patches with [add-up butfirst here-list = 1]) / field) * 100) 2 "%)") if (any? talker) [ print (word "avarage knowhow talker: " (precision (mean (values-from talker [sum knowhow])) 2) ) print (word "standard deviation: " (precision (standard-deviation (values-from talker [sum knowhow])) 2) ) ]if (any? silent) [ print (word "avarage knowhow silent: " (precision (mean (values-from silent [sum knowhow])) 2) ) print (word "standard deviation: " (precision (standard-deviation (values-from silent [sum knowhow])) 2) ) ] endto get-silentslocals [c k]set c (0.5 * (count turtles))set k ((mean (values-from talker [sum knowhow])) / no)ask random-n-of c talker [die] ;kill off half the talkerscreate-silent c ;; create given number of silentsask silent [set age random lifespan ;; set age to hatchlings (fput kind hatchlingsrandom number < lifespan setxy (random-int-or-float screen-size-x) ;; randomize the turtle locations (random-int-or-float screen-size-y) set energy (random-normal 18 0.9 ) set knowhow n-values no [round (((random 100) / 100) - (0.50 - k))] ;set hatchlings [] ;set parent "no-one" ;set bd step-timer set color 97 ] end
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -