📄 aaauser---emars.nlogo
字号:
set n-sum n-new while [n-sum > 0] [ set XX random-int-or-float length list-sum ; select an item location at random from list-sum set i item XX list-sum ; value of item determines type of turtle to be added set list-sum remove -99 (replace-item XX list-sum -99) ; drop used item from list. Can't use ; "remove XX list-sum" as it removes _all_ i's from list. ; this trick only works if -99 isn't already in the list. cct 1 [ ; procedure to add next (real) turtle ht set heading 0 ; compute turtle's personal adjustments ; for rate- and type- effects: set-rand-wgts w-span r-span set-type-id i ; type is based on value of item removed from list-sum ifelse (no-rates = 1) [ set own-rate .5 recolor-turtle ] ;; Rates == 1 in "no-rates" mode, else [adjust-rate 0] ;; need "0" for new turtle not yet moved onto board ifelse (item own-type target-type = 1) ; if 1, prefer more nbrs of own-type ; if 0, prefer more nbrs of any-type [find-new-patch this-region own-type (item own-type n-search)] [find-new-patch this-region N-types (item own-type n-search)] ; move turtle to new random location, and add-to-history ; add initial state of turtle to history list ; should get item 0 = initial [xcor,ycor,rate,type] ;; Update "type-totals" list set type-totals replace-item own-type type-totals (1 + (item own-type type-totals)) set type-totals replace-item N-types type-totals (1 + (item N-types type-totals)) ;; Update "rate-totals" list if no-rates != 1 [ set rate-totals replace-item own-type rate-totals (own-rate + (item own-type rate-totals)) set rate-totals replace-item N-types rate-totals (own-rate + (item N-types rate-totals)) ] adjust-type ; now adjust type and rate based on nbrs if no-rates != 1 [ adjust-rate 2 ; "2" forces standard adjustments (based on nbrs and own past). ] repeat (n-histories - 1) [add-to-history] ; add current (adjusted) state of turtle to turtle's history list ; make additional copies if necessary to insure that all turtles ; have same length t-history ; should get item 1 = current [xcor,ycor,rate,type] st ;; Update "nearby" type stats for self and nbrs set turtle-type own-type ask patch-here [update-nearby turtle-type 1] ] set n-sum (n-sum - 1) ; ready for next item ] ;; Update population set population replace-item 0 population (n-new + (item 0 population)) set population replace-item this-region population (n-new + (item this-region population)) endto add-turtles-manually [type-id] ; Button must be set to cycle "forever" ; Add type-i turtle if mouse button is pressed ; and mouse is over an empty patch on the real side of board locals [ i n-histories turtle-type] set i type-id if (mouse-down?) [ ask patch-at mouse-xcor mouse-ycor [ if (not any? turtles-here) and (region-id > 0) [ ifelse any? turtles [ask one-of turtles [set n-histories length t-history] ] [set n-histories 2] ;; determine number of prior state-saves (i.e., length of t-history) ; it should suffice to check any single turtle ; ifelse needed to avoid error when board is empty. every 5 [copy-from-sliders] ;; periodically update adjustment parameters from sliders sprout 1 [ ht set heading 0 set breed turtles ; compute turtle's personal adjustments for rate- and type- effects: set-rand-wgts w-span r-span set-type-id type-id ifelse (no-rates = 1) [ set own-rate .5 recolor-turtle ] ;; Rates == 1 in "no-rates" mode, else [adjust-rate 1] ;; need "1" when new turtle is dropped on board add-to-history ;; record turtle's "birth state" repeat (n-histories - 1) [add-to-history] ;; record turtle's rate-adjusted "initial state" ; repeated as needed to match other t-history lengths. st ;; Update regional "populations" list set population replace-item 0 population (1 + (item 0 population)) set population replace-item region-id population (1 + (item region-id population)) ;; Update "type-totals" list set type-totals replace-item own-type type-totals (1 + (item own-type type-totals)) set type-totals replace-item N-types type-totals (1 + (item N-types type-totals)) ;; Update "rate-totals" list if no-rates != 1 [ set rate-totals replace-item own-type rate-totals (own-rate + (item own-type rate-totals)) set rate-totals replace-item N-types rate-totals (own-rate + (item N-types rate-totals)) ] ;; Update "nearby" stats for self & nbrs: without-interruption [ set turtle-type own-type ask patch-here [update-nearby turtle-type 1] ] ] ] ] ]end;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Methods that calculate and plot overall statistics:to do-plots ;; Bug: Plot of color-X doesn't start until number of type-X's on board > 4. locals [i x] ;; 1t) Percentage Markers ; Place a bar every N time periods (where N = total number of turtles) set-current-plot-pen "time-tick" ifelse (precision ((period - (plot-period) / 2) / item 0 population) 0) != (precision ((period + (plot-period) / 2) / item 0 population) 0) [plot 5 plot-pen-down] [plot-pen-up plot 0] ; To also plot N/10 ticks, replace last line with the following: ; [ ; ifelse (precision ((period - (plot-period) / 2) / item 0 population) 1) != ; (precision ((period + (plot-period) / 2) / item 0 population) 1) ; [plot 2 plot-pen-down] ; [plot-pen-up plot 0] ; ] ;; 1a) Share plots set-current-plot "Shares" foreach types-list1 [ ;; Careful! The pen names here must match the names in type-names ;; Plot share-stats for type i only if number of type-i turtles >= 5. set-current-plot-pen item ? type-names ifelse item ? type-totals > 4 [plot (100 * (item ? type-totals) / (item N-types type-totals)) plot-pen-down] [plot-pen-up plot 0] ] ;; 1b) Overall similiarity statistic - plotted on same graph as "Shares" ;; "similar" = mean share of nbrs same type as self. set-current-plot-pen "similar" plot similarity-all; ;; Alternative to "similarity" reporter - same result (and faster?):; ifelse (any? patches with [(region-id > 0) and (any? turtles-here) and ; (item N-types nearby-types) > 0]; ); [set percent-similar mean values-from ; patches with [(region-id > 0) and (any? turtles-here) ; and (item N-types nearby-types) > 0]; [100 * (item (own-type-of one-of turtles-here) nearby-types) / (item N-types nearby-types) ]; ]; [set percent-similar 0]; set-current-plot-pen "similar"; plot similarity-all ;; No Rate Plots for this version; ;; 2a) Compute and plot mean rates for each type (and all together); ; Note: Use pen-up/down to suppress plot for any type with fewer than five cases ; if no-rates != 1 [; set-current-plot "Rates"; ; Plot separate line for each type of turtle; foreach types-list1 [; set-current-plot-pen item ? type-names; ifelse (item ? type-totals > 4); [plot (item ? rate-totals / item ? type-totals) plot-pen-down]; [plot-pen-up plot 0 ]; ]; ; 2b) Plot a line for all types of turtles; set-current-plot-pen "all"; plot (item N-types rate-totals / item N-types type-totals); ];; No Grimson Plots for this version; ; 3) Compute & Plot Grimson Clustering Stats:; set-current-plot "Clustering"; calculate-grimson-stats; foreach types-list1 [; set-current-plot-pen item ? type-names; ifelse item ? g-stats > 0 [plot item ? g-stats plot-pen-down] [plot-pen-up plot 0]; ]end;; Similiarity statistic for all typesto-report similarity-all ifelse any? patches with [(region-id > 0) and (any? turtles-here) and (item N-types nearby-types) > 0] [report mean values-from patches with [(region-id > 0) and (any? turtles-here) and (item N-types nearby-types) > 0] [100 * (item (own-type-of one-of turtles-here) nearby-types) / (item N-types nearby-types) ]] [report -1]end ;; Similiarity statistic for specific typesto-report similarity-x [ x-type ] locals [x-list ] ;; "similar" = mean share of nbrs same type as self. show x-type set zz1 values-from turtles with [ (own-type = x-type) and (value-from patch-here [item N-types nearby-types] > 0 )] [100 * value-from patch-here [(item x-type nearby-types) / (item N-types nearby-types) ]] ifelse (empty? zz1) [report -1] [report mean zz1]end ;; No Grimson Plots for this version;to calculate-grimson-stats; ;; ** Grimson's clustering statistic (web reference ...geomed/stats/Grimson); ;; ** Get a separate G-stat for each type ; locals [i a exp-a var-a y var-y n x actual-gi expected-gi ]; set g-stats zeroN ;; initialize the list of g-statistic ratios (one for each turtle type); ; def: x = total number of turtles ("items") of all types; set x item N-types type-totals; ; def: y = average number of neighbors (= "occupied borders") per item; set y mean values-from turtles [item N-types nearby-types]; ; def: Var-y = variance of y; set var-y variance values-from turtles [item N-types nearby-types]; ; Testing: type "type-" + i + ": y=" + precision y 1 + " x=" + x print " "; ; For each item type i = 1, 2, ..., N-types:; ; def: n_i = number of type i items; ; def: a_i = number of "borders" shared by type i items ( = 1/2 number of adjacent type i's); ; def actual-g_i = a_i; ; expected-g_i = yn(n-1)/2(x-1); ; variance-g_i = var-a_i = ... see below; foreach types-list1 [; ifelse (item i type-totals < 4) [set g-stats ( replace-item i g-stats 0 )] [; set n item i type-totals; set a (1 / 2 ) * ( sum values-from turtles with [own-type = i]; [item i nearby-types ] ); set exp-a (y * n * (n - 1) / (2 * (x - 1)) ); set var-a exp-a * ( 1 + ; ( (2 * (y - 1) * (n - 2) ) / (x - 2) ) + ; ( (x * y - 4 * y + 2) * (n - 2) * (n - 3)) / (2 * (x - 2) * (x - 3 ) ) - exp-a ) + ; var-y * ; ( ((x - n) * n * (n - 1) * (n - 2)) / ((x - 1) * (x - 2) * (x - 3)) ); ; ( (n * (n - 1) * (n - 2)) / ((x - 1) * (x - 2)) ) - ; ; ( (n * (n - 1) * (n - 2) * (n - 3)) / ((x - 1) * (x - 2) * (x - 3)) ) ); set g-stats ( replace-item i g-stats ((a - exp-a ) / (var-a )) ); ; Testing: type "type-" + i + ": n=" + n + " actual-g=" + precision a 1 + ; ; " expected-g=" + precision exp-a 1 + " g-stat=" + precision (item i g-stats) 1 print ""; ]; ];end;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Turtle Methods (invoked via "ask turtles"- type commands )to find-new-patch [ target-region target-id n-patches ] locals [i value-max xcor-max ycor-max value-list patch-set maxnear steps initial-pcolor] ; Find a new location in {target-region}. ; Search over {n-patches} randomly selected patches, and ; Select patch with highest nearby population of {target-id}-type turtles. ; If {target-id} = N-types, count all types of turtles population. ifelse (n-patches < 2) ; skip search if only 1 patch is to be checked. [ ask random-one-of patches with [(not any? turtles-here) and (region-id = target-region)] [ set xcor-max pxcor set ycor-max pycor] ] [ ; Don't search over more patches than remain open in region set n-patches min list n-patches (item target-region region-max - item target-region population) set patch-set random-n-of n-patches patches with [(region-id = target-region) and (not any? turtles-here)] set maxnear max values-from patch-set [item target-id nearby-types] ask random-one-of patch-set with [ item target-id nearby-types = maxnear] [set xcor-max pxcor set ycor-max pycor ] ] ; Note: Can't use "ask max-one-of patch-set [item target-id nearby-types] ; [set xcor-max pxcor set ycor-max pycor ]" in place of the two lines above
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -