📄 aaauser---emars.nlogo
字号:
;; Step 1b: Count patches (max potential population) in each region and place total in last item set region-max n-values (N-types + 1) [0] ; initialize list if N-regions = 0 [set N-regions 1] ;; trick to make "zero" region case work. set i 1 while [i <= N-regions] [ set region-max replace-item i region-max (count patches with [region-id = i]) set i i + 1 ] set region-max replace-item 0 region-max sum but-first region-max ;; Step 3: Color patches (based on region-id's) ;; and initialize nearby-types and nearby-rates = [0 0 ... 0] ask patches [ ifelse (region-id = 0) [set pcolor border-color] [ ifelse (region-id < 1)[set pcolor divider-color] [ set pcolor main-board-color ;; "nearby" stats only relevant on main board regions set nearby-types (sentence zeroN 0) ] ] ]end;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Pass parameter values to/from sliders;; (there MUST be a better way to do this!)to get-type-percents ;; Calculates percentages from shares (each of which may range between 0 and 100). locals [ i ] ; 1) Re-read shares list from sliders ; (Use "run" to create commands: "set shares replace-item 0 shares r_num", ; "set shares replace-item 1 shares g_num", etc.) set i 0 while [i < N-types] [ run "set shares replace-item " + i + " shares " + item i c-list + "_num" set i (i + 1) ] if shares = zeroN [ set shares replace-item 0 shares 1 set shares replace-item 1 shares 1 ] ; error trap: if all shares are zero, then reset first two to 1. ; 2) Calculate percentages foreach types-list1 [ set type-percents replace-item ? type-percents ((item ? shares) / (sum shares)) ]endto copy-to-sliders ;; Re-set sliders to display type-matrix and rate-matrix parameters of type i locals [i j] set i 0 while [i < N-types] [ run "set " + (item i c-list) + "_num " + " (item " + i + " shares)" ; equivalent to "set r_num (item 0 shares)", "set g_num (item 1 shares)", etc. set j 0 while [j < 7] [ run "set " + (item i c-list) + "_" + (item j t-sliders) + " (item " + j + " (item " + i + " t-pull))" ; equivalent to "set r_pull2 (item 1 (item 0 t-pull))" iterated over all types and type-sliders run "set " + (item i c-list) + "_" + (item j r-sliders) + " (item " + j + " (item " + i + " r-copy))" ; equivalent to "set r_copy3 (item 2 (item 0 t-copy))" iterated over all types and rate-sliders set j (j + 1) ] set i (i + 1) ]endto copy-from-sliders ;; Re-set type i's type-list and rate-list parameters from sliders locals [i j] set i 0 ; Loop through each type i (red, green, etc.) while [i < N-types] [ ; 1) Copy values from i's share slider: run "set shares (replace-item " + i + " shares " + item i c-list + "_num)" ; equivalent to "set shares (replace-item 0 shares r_num)" etc. ; 2) Copy values from i's type and rate sliders: ; 2a) Always copy from i's basic type sliders ("_type", "_self", and "_pull") ; and basic rate sliders ("_rate", "_habit", and "_copy"). set j N-types while [j < N-types + 3] [ run "set t-pull (replace-matrix-element " + i + " " + j + " t-pull " + item i c-list + "_" + item j t-sliders + ") " run "set r-copy (replace-matrix-element " + i + " " + j + " r-copy " + item i c-list + "_" + item j r-sliders + ") " set j (j + 1) ] ; 2b) Copy full matrix of type-interactions from separate sliders ; only if "_pull" slider is set to "pull-flag-value", otherwise set ; all interactions (uniformly) equal to "_pull" slider. ; Likewise for matrix of rate-interactions" set j 0 while [j < N-types] [ run "ifelse (" + item i c-list + "_pull = pull-flag-value) " + "[set t-pull (replace-matrix-element " + i + " " + j + " t-pull " + item i c-list + "_" + item j t-sliders + ") ] " + "[set t-pull (replace-matrix-element " + i + " " + j + " t-pull " + item i c-list + "_" + item (N-types + 2) t-sliders + ") ] " run "ifelse (" + item i c-list + "_copy = copy-flag-value) " + "[set r-copy (replace-matrix-element " + i + " " + j + " r-copy " + item i c-list + "_" + item j r-sliders + ") ] " + "[set r-copy (replace-matrix-element " + i + " " + j + " r-copy " + item i c-list + "_" + item (N-types + 2) r-sliders + ") ] " set j (j + 1) ] set i (i + 1) ]end;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Setup invoked at start of program (and reinvoked via "setup" button):to setup ;; (Re-)initialize board layout and other key key parameters ca clear-output initialize-parameters initialize-shapes-and-colors set N-regions regions ;; Set number of regions (until next "setup") initialize-board ifelse no-rates = 1 [set color-mode 1] [set color-mode 2] ;; initially display type-colored (and rate-shaded unless suppressing rates). set color-state 1 ;; initially, ready to "toggle" state back to original color, rate, and shape. set current-region 1 ;; Initialize to region #1. copy-from-sliders ; get initial parameter valuesend;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Set basic run-time optionsto options-menu-select ;; set various options via queries to user ;; good way to simplify interface without eliminating options ;; Other choices to add: ; default values (for all options: regions, shares, etc.) ; move-rate? ; % or # to move when run is pressed (escape via stop), ; rand-wgts? ; scenarios!!! ; maintain regional densities vs. choose any open location? locals [choice1 choice2 choice3 choice4 Ntype] set choice1 (user-choice "Option to alter?" [ "Show" "Search" "Display" "Replot" "Skip Ahead-%" "Skip Ahead-n" "Quit" ]) if choice1 = "Quit" [stop] if choice1 = "Show" [ set choice2 (user-choice "Show Regional-Shares or Search-Methods" [ "Shares" "Search" "Quit"]) if choice2 = "Quit" [stop] if choice2 = "Shares" [output-regional-shares] if choice2 = "Search" [ output-print " SEARCH METHODS:" output-type " " output-print list n-search target-type ] ] if choice1 = "Skip Ahead-%" [ set choice2 (user-choice "Percent to move?" ["5" "10" "15" "20" "Quit"]) if choice2 = "Quit" [stop] make-x-percent-move (read-from-string choice2) / 100 ] if choice1 = "Skip Ahead-n" [ set choice2 (user-choice "number to move?" ["10" "100" "200" "500" "1000" "2000" "Quit"]) if choice2 = "Quit" [stop] make-x-move (read-from-string choice2) ] if choice1 = "Display" [ set choice2 (user-choice "(1) Return to initial layout? (2) Restore types?" [ "(1) Return" "(2) Restore" "Quit"]) if choice2 = "(1) Return" [restore-turtles-all] if choice2 = "(2) Restore" [restore-turtles-types] if choice2 = "Quit" [stop] ] if choice1 = "Replot" [clear-all-plots] if choice1 = "Search" [ set choice2 (user-choice "Alter whose method?" ["Blue" "Red" "Green" "Yellow" "Quit"]) if choice2 = "Quit" [stop] set Ntype position (first choice2) "BRGY" set choice3 (user-choice "Number of locations to search each move?" [ "1" "2" "4" "8" "32" "Quit"]) if choice3 = "Quit" [stop] set n-search (replace-item Ntype n-search (read-from-string choice3)) set choice3 (user-choice "Search for own-type or any-type?" ["Own-type" "Any-type" "Quit"]) if choice3 = "Quit" [stop] if choice3 = "Own-type" [set target-type (replace-item Ntype target-type 1)] if choice3 = "Any-type" [set target-type (replace-item Ntype target-type 0)] output-print " SEARCH METHODS:" output-type " " output-print list n-search target-type ]end;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Go/Pause Methods:to populate-region ;; This method cycles through the regions, 1 through N-regions get-type-percents add-turtles current-region ;; invoke for current region only set current-region 1 + (remainder current-region N-regions ) ;; Cycle to next regionendto go ;; Move a turtle (attached to "forever" button) ; Key parameters governing move are set via sliders, button, or parameters section of program locals [i r cum-pop xx yy old-type old-rate old-patch new-patch new-type] if item 0 population = 0 [stop] ;; Exit if this button before any turtles are present. every 2 [copy-from-sliders] ;; Copy from sliders at least every 2 seconds ask random-one-of turtles [ ;; Randomly select random turtle to update if speed < slow-speeds ;; Blink turtle at slow speeds. [blink-turtle blink-period blink-interval shape color white] set old-patch patch-here set old-type own-type if random-int-or-float 100 < move-rate [ ;; Decide to move or merely adjust type & rate set r random-int-or-float 1.0 ;; Choose target region for jump, so as to set i 1 ;; maintain regional population densities set cum-pop (item 1 population) while [r > (cum-pop / (item 0 population))] [ set i (i + 1) ;; To ignore population densities, set cum-pop (cum-pop + (item i population)) ;; skip regional-choice routine ] ;; and simply use: "find-new-patch 0" 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 i own-type (item own-type n-search)] [find-new-patch i N-types (item own-type n-search)] ] if speed < slow-speeds ;; blink turtle at slow speeds [blink-turtle blink-period blink-interval shape color color] without-interruption [ if patch-here != old-patch [ ;; update "nearby" stats if turtle moved ask old-patch [update-nearby old-type -1] ] adjust-type ;; re-evaluate type if patch-here != old-patch [ ;; update "nearby" stats if turtle moved set new-type own-type ask patch-here [update-nearby new-type 1] ] ] adjust-rate 3 ;; re-evaluate rate ] if period mod plot-period < 1 [ ;; For speed, do stats and plots only 1/Nth of the time. do-plots ;; plot-period currently = 10 ] set period (period + 1) ;; increment counter, one per move if (speed < max-speed) [wait max-wait / (2 ^ speed)] ;; set length of pause per loop (10 skips pause).endto make-x-percent-move [x-percent] ;; Move N times, then pause ; where N = x-percent of total number of turtles. locals [n] set n x-percent * count turtles repeat n [ go ]endto make-x-move [x] ;; Move x times, then pause ; where x= number of turtles. repeat x [ go ]end;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Methods that alter types or locations of *all* turtlesto restore-turtles-types ;; Restore each turtle to its original type ask turtles [restore-type] ; Recompute global type-totals list and rate-totals list recompute-totals ; Restore each patches' "nearby-types" list ask patches with [ region-id >= 1 ] [recompute-nearby]endto show-original-states ; Attached to a "forever" button, this continuously displays turtles original (rate and type) states. ; Attached to a regular button, this displays original states for 2 seconds, then reverts to regular. ; In either case, the turtles' underlying rate and type values are not affected. ask turtles [display-original-color]
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -